1010use Http \Client \Common \Plugin \HeaderDefaultsPlugin ;
1111use Http \Client \Common \Plugin \RedirectPlugin ;
1212use Http \Client \Common \PluginClient ;
13- use Http \Client \HttpClient ;
14- use Http \Discovery \HttpClientDiscovery ;
15- use Http \Discovery \MessageFactoryDiscovery ;
13+ use Http \Discovery \Psr17FactoryDiscovery ;
1614use Http \Discovery \Psr18ClientDiscovery ;
17- use Http \Discovery \StreamFactoryDiscovery ;
18- use Http \Discovery \UriFactoryDiscovery ;
19- use Http \Message \MessageFactory ;
2015use Http \Message \MultipartStream \MultipartStreamBuilder ;
21- use Http \Message \StreamFactory ;
22- use Http \Message \UriFactory ;
16+ use Psr \Http \Message \RequestFactoryInterface ;
2317use Psr \Http \Message \RequestInterface ;
2418use Psr \Http \Message \ResponseInterface ;
19+ use Psr \Http \Message \StreamFactoryInterface ;
2520use Psr \Http \Message \StreamInterface ;
21+ use Psr \Http \Message \UriFactoryInterface ;
2622
2723
2824class HttpTransport
2925{
3026
3127 protected $ options ;
3228 protected $ httpClient ;
33- protected $ messageFactory ;
3429
3530
3631 /**
@@ -48,9 +43,9 @@ public function __construct($options)
4843 /**
4944 * Creates a new instance of the HTTP client.
5045 *
51- * @return HttpClient
46+ * @return PluginClient
5247 */
53- protected function createHttpClientInstance (): HttpClient
48+ protected function createHttpClientInstance (): PluginClient
5449 {
5550
5651 $ httpClient = $ this ->options ['http_client ' ] ?? Psr18ClientDiscovery::find ();
@@ -82,35 +77,35 @@ public function getSyncBaseUri(): string
8277 }
8378
8479 /**
85- * @return HttpClient
80+ * @return PluginClient
8681 */
87- public function getHttpClient (): HttpClient
82+ public function getHttpClient (): PluginClient
8883 {
8984 return $ this ->httpClient ;
9085 }
9186
9287 /**
93- * @return MessageFactory
88+ * @return RequestFactoryInterface
9489 */
95- public function getMessageFactory (): MessageFactory
90+ public function getRequestFactory (): RequestFactoryInterface
9691 {
97- return $ this ->options ['message_factory ' ] ?? MessageFactoryDiscovery:: find ();
92+ return $ this ->options ['request_factory ' ] ?? Psr17FactoryDiscovery:: findRequestFactory ();
9893 }
9994
10095 /**
101- * @return UriFactory
96+ * @return UriFactoryInterface
10297 */
103- public function getUriFactory (): UriFactory
98+ public function getUriFactory (): UriFactoryInterface
10499 {
105- return $ this ->options ['uri_factory ' ] ?? UriFactoryDiscovery:: find ();
100+ return $ this ->options ['uri_factory ' ] ?? Psr17FactoryDiscovery:: findUriFactory ();
106101 }
107102
108103 /**
109- * @return StreamFactory
104+ * @return StreamFactoryInterface
110105 */
111- public function getStreamFactory (): StreamFactory
106+ public function getStreamFactory (): StreamFactoryInterface
112107 {
113- return $ this ->options ['stream_factory ' ] ?? StreamFactoryDiscovery:: find ();
108+ return $ this ->options ['stream_factory ' ] ?? Psr17FactoryDiscovery:: findStreamFactory ();
114109 }
115110
116111 /**
@@ -127,7 +122,7 @@ public function get(string $path, array $query = []): ResponseInterface
127122 }
128123
129124
130- return $ this ->sendRequest ($ this ->getMessageFactory ()->createRequest ('GET ' , $ path , [
125+ return $ this ->sendRequest ($ this ->getRequestFactory ()->createRequest ('GET ' , $ path , [
131126 'accept-encoding ' => 'application/json '
132127 ]));
133128 }
@@ -140,10 +135,20 @@ public function get(string $path, array $query = []): ResponseInterface
140135 */
141136 public function download (string $ url )
142137 {
143- return $ this ->sendRequest ($ this ->getMessageFactory ()->createRequest ('GET ' , $ url ), false )->getBody ();
138+ return $ this ->sendRequest ($ this ->getRequestFactory ()->createRequest ('GET ' , $ url ), false )->getBody ();
144139 }
145140
146141
142+ /**
143+ * @param array<string, mixed>|string $body
144+ */
145+ protected function buildBody ($ body ): StreamInterface
146+ {
147+ $ stringBody = is_array ($ body ) ? json_encode ($ body , JSON_THROW_ON_ERROR ) : $ body ;
148+
149+ return $ this ->getStreamFactory ()->createStream ($ stringBody );
150+ }
151+
147152 /**
148153 * @param $path
149154 * @param $body
@@ -152,10 +157,12 @@ public function download(string $url)
152157 */
153158 public function post (string $ path , array $ body ): ResponseInterface
154159 {
155- return $ this ->sendRequest ($ this ->getMessageFactory ()->createRequest ('POST ' , $ path , [
156- 'content-type ' => 'application/json ' ,
157- 'accept-encoding ' => 'application/json '
158- ], json_encode ($ body )));
160+ return $ this ->sendRequest (
161+ $ this ->getRequestFactory ()->createRequest ('POST ' , $ path )
162+ ->withHeader ('content-type ' , 'application/json ' )
163+ ->withHeader ('accept-encoding ' , 'application/json ' )
164+ ->withBody ($ this ->buildBody ($ body ))
165+ );
159166 }
160167
161168 /**
@@ -166,10 +173,12 @@ public function post(string $path, array $body): ResponseInterface
166173 */
167174 public function put (string $ path , array $ body ): ResponseInterface
168175 {
169- return $ this ->sendRequest ($ this ->getMessageFactory ()->createRequest ('PUT ' , $ path , [
170- 'content-type ' => 'application/json ' ,
171- 'accept-encoding ' => 'application/json '
172- ], json_encode ($ body )));
176+ return $ this ->sendRequest (
177+ $ this ->getRequestFactory ()->createRequest ('POST ' , $ path )
178+ ->withHeader ('content-type ' , 'application/json ' )
179+ ->withHeader ('accept-encoding ' , 'application/json ' )
180+ ->withBody ($ this ->buildBody ($ body ))
181+ );
173182 }
174183
175184 /**
@@ -179,7 +188,7 @@ public function put(string $path, array $body): ResponseInterface
179188 */
180189 public function delete (string $ path ): ResponseInterface
181190 {
182- return $ this ->sendRequest ($ this ->getMessageFactory ()->createRequest ('DELETE ' , $ path , [
191+ return $ this ->sendRequest ($ this ->getRequestFactory ()->createRequest ('DELETE ' , $ path , [
183192 'accept-encoding ' => 'application/json '
184193 ]));
185194 }
@@ -208,12 +217,12 @@ public function upload($path, $file, string $fileName = null, array $additionalP
208217 $ multipartStream = $ builder ->build ();
209218 $ boundary = $ builder ->getBoundary ();
210219
211- $ request = $ this ->getMessageFactory ()->createRequest (
220+ $ request = $ this ->getRequestFactory ()->createRequest (
212221 'POST ' ,
213- $ path,
214- [ ' Content-Type ' => ' multipart/form-data; boundary=" ' . $ boundary . ' " ' ],
215- $ multipartStream
216- );
222+ $ path
223+ )
224+ -> withHeader ( ' Content-Type ' , ' multipart/form-data; boundary=" ' . $ boundary . ' " ' )
225+ -> withBody ( $ multipartStream );
217226
218227 return $ this ->sendRequest ($ request , false );
219228 }
0 commit comments