22
33namespace Rezzza \RestApiBehatExtension \Rest ;
44
5- use Ivory \HttpAdapter \HttpAdapterFactory ;
6- use Ivory \HttpAdapter \HttpAdapterInterface as HttpClient ;
7- use Ivory \HttpAdapter \HttpAdapterException ;
8- use Ivory \HttpAdapter \Message \Request ;
9- use Zend \Diactoros \Stream ;
5+ use Http \Client \HttpClient ;
6+ use Http \Discovery \HttpClientDiscovery ;
7+ use Http \Discovery \MessageFactoryDiscovery ;
108use Psr \Http \Message \RequestInterface ;
119use Psr \Http \Message \ResponseInterface ;
1210
@@ -27,23 +25,28 @@ class RestApiBrowser
2725 /** @var ResponseStorage */
2826 private $ responseStorage ;
2927
28+ /** @var string */
29+ private $ host ;
30+
31+ /** @var MessageFactoryDiscovery */
32+ private $ messageFactory ;
33+
3034 /**
31- * @param string $base_url
32- * @param string|null $adaptor_name
33- * @throws HttpAdapterException
35+ * @param string $host
3436 */
35- public function __construct ($ base_url , $ adaptor_name , HttpClient $ httpClient = null )
37+ public function __construct ($ host , HttpClient $ httpClient = null )
3638 {
37- if (!is_null ($ httpClient ) && $ httpClient instanceof HttpClient) {
38- $ this ->httpClient = $ httpClient ;
39- } else {
40- if (is_string ($ adaptor_name ) && HttpAdapterFactory::capable ($ adaptor_name )) {
41- $ this ->httpClient = HttpAdapterFactory::create ($ adaptor_name );
42- } else {
43- $ this ->httpClient = HttpAdapterFactory::guess ();
44- }
45- $ this ->httpClient ->getConfiguration ()->setBaseUri ($ base_url );
46- }
39+ $ this ->host = $ host ;
40+ $ this ->httpClient = $ httpClient ?: HttpClientDiscovery::find ();
41+ $ this ->messageFactory = MessageFactoryDiscovery::find ();
42+ }
43+
44+ /**
45+ * Allow to override the httpClient to use yours with specific middleware for example
46+ */
47+ public function useHttpClient (HttpClient $ httpClient )
48+ {
49+ $ this ->httpClient = $ httpClient ;
4750 }
4851
4952 /**
@@ -54,7 +57,6 @@ public function enableResponseStorage(ResponseStorage $responseStorage)
5457 $ this ->responseStorage = $ responseStorage ;
5558 }
5659
57-
5860 /**
5961 * @return ResponseInterface
6062 */
@@ -81,78 +83,46 @@ public function getRequestHeaders()
8183 return $ this ->requestHeaders ;
8284 }
8385
84- /**
85- * @return HttpClient
86- */
87- public function getHttpClient ()
88- {
89- return $ this ->httpClient ;
90- }
91-
9286 /**
9387 * @param string $method
94- * @param string $url
88+ * @param string $uri
9589 * @param string|array $body
9690 */
97- public function sendRequest ($ method , $ url , $ body = null )
91+ public function sendRequest ($ method , $ uri , $ body = null )
9892 {
99- try {
100- $ this ->send ($ method , $ url , $ body );
101- } catch (HttpAdapterException $ e ) {
102- if ($ e ->hasResponse ()) {
103- $ this ->response = $ e ->getResponse ();
104- }
105-
106- if (null === $ this ->response ) {
107- throw $ e ;
108- }
93+ if (false === $ this ->hasHost ($ uri )) {
94+ $ uri = rtrim ($ this ->host , '/ ' ).'/ ' .ltrim ($ uri , '/ ' );
10995 }
11096
111- if (null !== $ this ->responseStorage ) {
112- $ this ->responseStorage ->writeRawContent ($ this ->response ->getBody ()->getContents ());
113- }
114- }
97+ $ this ->request = $ this ->messageFactory ->createRequest ($ method , $ uri , $ this ->requestHeaders , $ body );
98+ $ this ->response = $ this ->httpClient ->sendRequest ($ this ->request );
11599
116- /**
117- * @param string $method
118- * @param string $uri With or without host
119- * @param string|array $body
120- */
121- private function send ($ method , $ uri , $ body = null )
122- {
123- if (!$ this ->hasHost ($ uri )) {
124- $ uri = rtrim ($ this ->httpClient ->getConfiguration ()->getBaseUri (), '/ ' ) . '/ ' . ltrim ($ uri , '/ ' );
125- }
126- $ body = is_array ($ body ) ? http_build_query ($ body ) : $ body ;
127- $ stream = new Stream ('php://memory ' , 'rw ' );
128- if (is_scalar ($ body )) {
129- $ stream ->write ($ body );
100+ if (null !== $ this ->responseStorage ) {
101+ $ this ->responseStorage ->writeRawContent ((string ) $ this ->response ->getBody ());
130102 }
131-
132- $ this ->request = new Request ($ uri , $ method , $ stream , $ this ->requestHeaders );
133- $ this ->response = $ this ->httpClient ->sendRequest ($ this ->request );
134- // Reset headers used for the HTTP request
135- $ this ->requestHeaders = [];
136103 }
137104
138105 /**
139- * @param string $uri
140- *
141- * @return bool
106+ * @param string $name
107+ * @param string $value
142108 */
143- private function hasHost ( $ uri )
109+ public function setRequestHeader ( $ name , $ value )
144110 {
145- return strpos ($ uri , ':// ' ) !== false ;
111+ $ this ->removeRequestHeader ($ name );
112+ $ this ->addRequestHeader ($ name , $ value );
146113 }
147114
148115 /**
149116 * @param string $name
150117 * @param string $value
151118 */
152- public function setRequestHeader ($ name , $ value )
119+ public function addRequestHeader ($ name , $ value )
153120 {
154- $ this ->removeRequestHeader ($ name );
155- $ this ->addRequestHeader ($ name , $ value );
121+ if (isset ($ this ->requestHeaders [$ name ])) {
122+ $ this ->requestHeaders [$ name ] .= ', ' .$ value ;
123+ } else {
124+ $ this ->requestHeaders [$ name ] = $ value ;
125+ }
156126 }
157127
158128 /**
@@ -166,18 +136,12 @@ private function removeRequestHeader($headerName)
166136 }
167137
168138 /**
169- * @param string $name
170- * @param string $value
139+ * @param string $uri
140+ *
141+ * @return bool
171142 */
172- public function addRequestHeader ( $ name , $ value )
143+ private function hasHost ( $ uri )
173144 {
174- if (isset ($ this ->requestHeaders [$ name ])) {
175- if (!is_array ($ this ->requestHeaders [$ name ])) {
176- $ this ->requestHeaders [$ name ] = [$ this ->requestHeaders [$ name ]];
177- }
178- $ this ->requestHeaders [$ name ][] = $ value ;
179- } else {
180- $ this ->requestHeaders [$ name ] = $ value ;
181- }
145+ return strpos ($ uri , ':// ' ) !== false ;
182146 }
183147}
0 commit comments