99use Redmine \Exception ;
1010use Redmine \Exception \SerializerException ;
1111use Redmine \Http \HttpClient ;
12+ use Redmine \Http \Request ;
1213use Redmine \Http \Response ;
1314use Redmine \Serializer \JsonSerializer ;
1415use Redmine \Serializer \PathSerializer ;
@@ -108,9 +109,13 @@ public function lastCallFailed()
108109 */
109110 protected function get ($ path , $ decodeIfJson = true )
110111 {
111- $ this ->lastResponse = $ this ->getHttpClient ()->request ('GET ' , strval ($ path ));
112+ $ this ->lastResponse = $ this ->getHttpClient ()->request ($ this ->createRequest (
113+ 'GET ' ,
114+ strval ($ path ),
115+ $ this ->getContentTypeFromPath (strval ($ path ))
116+ ));
112117
113- $ body = $ this ->lastResponse ->getBody ();
118+ $ body = $ this ->lastResponse ->getContent ();
114119 $ contentType = $ this ->lastResponse ->getContentType ();
115120
116121 // if response is XML, return a SimpleXMLElement object
@@ -139,9 +144,14 @@ protected function get($path, $decodeIfJson = true)
139144 */
140145 protected function post ($ path , $ data )
141146 {
142- $ this ->lastResponse = $ this ->getHttpClient ()->request ('POST ' , strval ($ path ), $ data );
143-
144- $ body = $ this ->lastResponse ->getBody ();
147+ $ this ->lastResponse = $ this ->getHttpClient ()->request ($ this ->createRequest (
148+ 'POST ' ,
149+ strval ($ path ),
150+ $ this ->getContentTypeFromPath (strval ($ path )),
151+ $ data
152+ ));
153+
154+ $ body = $ this ->lastResponse ->getContent ();
145155 $ contentType = $ this ->lastResponse ->getContentType ();
146156
147157 // if response is XML, return a SimpleXMLElement object
@@ -162,9 +172,14 @@ protected function post($path, $data)
162172 */
163173 protected function put ($ path , $ data )
164174 {
165- $ this ->lastResponse = $ this ->getHttpClient ()->request ('PUT ' , strval ($ path ), $ data );
166-
167- $ body = $ this ->lastResponse ->getBody ();
175+ $ this ->lastResponse = $ this ->getHttpClient ()->request ($ this ->createRequest (
176+ 'PUT ' ,
177+ strval ($ path ),
178+ $ this ->getContentTypeFromPath (strval ($ path )),
179+ $ data
180+ ));
181+
182+ $ body = $ this ->lastResponse ->getContent ();
168183 $ contentType = $ this ->lastResponse ->getContentType ();
169184
170185 // if response is XML, return a SimpleXMLElement object
@@ -184,9 +199,13 @@ protected function put($path, $data)
184199 */
185200 protected function delete ($ path )
186201 {
187- $ this ->lastResponse = $ this ->getHttpClient ()->request ('DELETE ' , strval ($ path ));
202+ $ this ->lastResponse = $ this ->getHttpClient ()->request ($ this ->createRequest (
203+ 'DELETE ' ,
204+ strval ($ path ),
205+ $ this ->getContentTypeFromPath (strval ($ path ))
206+ ));
188207
189- return $ this ->lastResponse ->getBody ();
208+ return $ this ->lastResponse ->getContent ();
190209 }
191210
192211 /**
@@ -234,7 +253,7 @@ protected function retrieveAll($endpoint, array $params = [])
234253 try {
235254 $ data = $ this ->retrieveData (strval ($ endpoint ), $ params );
236255 } catch (Exception $ e ) {
237- if ($ this ->getLastResponse ()->getBody () === '' ) {
256+ if ($ this ->getLastResponse ()->getContent () === '' ) {
238257 return false ;
239258 }
240259
@@ -258,7 +277,11 @@ protected function retrieveAll($endpoint, array $params = [])
258277 protected function retrieveData (string $ endpoint , array $ params = []): array
259278 {
260279 if (empty ($ params )) {
261- $ this ->lastResponse = $ this ->getHttpClient ()->request ('GET ' , strval ($ endpoint ));
280+ $ this ->lastResponse = $ this ->getHttpClient ()->request ($ this ->createRequest (
281+ 'GET ' ,
282+ strval ($ endpoint ),
283+ $ this ->getContentTypeFromPath (strval ($ endpoint ))
284+ ));
262285
263286 return $ this ->getResponseAsArray ($ this ->lastResponse );
264287 }
@@ -287,10 +310,11 @@ protected function retrieveData(string $endpoint, array $params = []): array
287310 $ params ['limit ' ] = $ _limit ;
288311 $ params ['offset ' ] = $ offset ;
289312
290- $ this ->lastResponse = $ this ->getHttpClient ()->request (
313+ $ this ->lastResponse = $ this ->getHttpClient ()->request ($ this -> createRequest (
291314 'GET ' ,
292- PathSerializer::create ($ endpoint , $ params )->getPath ()
293- );
315+ PathSerializer::create ($ endpoint , $ params )->getPath (),
316+ $ this ->getContentTypeFromPath ($ endpoint )
317+ ));
294318
295319 $ newDataSet = $ this ->getResponseAsArray ($ this ->lastResponse );
296320
@@ -368,7 +392,7 @@ protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
368392 */
369393 private function getResponseAsArray (Response $ response ): array
370394 {
371- $ body = $ response ->getBody ();
395+ $ body = $ response ->getContent ();
372396 $ contentType = $ response ->getContentType ();
373397 $ returnData = null ;
374398
@@ -400,16 +424,16 @@ public function __construct(Client $client, Closure $responseFactory)
400424 $ this ->responseFactory = $ responseFactory ;
401425 }
402426
403- public function request (string $ method , string $ path , string $ body = '' ): Response
427+ public function request (Request $ request ): Response
404428 {
405- if ($ method === 'POST ' ) {
406- $ this ->client ->requestPost ($ path , $ body );
407- } elseif ($ method === 'PUT ' ) {
408- $ this ->client ->requestPut ($ path , $ body );
409- } elseif ($ method === 'DELETE ' ) {
410- $ this ->client ->requestDelete ($ path );
429+ if ($ request -> getMethod () === 'POST ' ) {
430+ $ this ->client ->requestPost ($ request -> getPath () , $ request -> getContent () );
431+ } elseif ($ request -> getMethod () === 'PUT ' ) {
432+ $ this ->client ->requestPut ($ request -> getPath () , $ request -> getContent () );
433+ } elseif ($ request -> getMethod () === 'DELETE ' ) {
434+ $ this ->client ->requestDelete ($ request -> getPath () );
411435 } else {
412- $ this ->client ->requestGet ($ path );
436+ $ this ->client ->requestGet ($ request -> getPath () );
413437 }
414438
415439 return ($ this ->responseFactory )(
@@ -445,10 +469,65 @@ public function getContentType(): string
445469 return $ this ->contentType ;
446470 }
447471
448- public function getBody (): string
472+ public function getContent (): string
449473 {
450474 return $ this ->body ;
451475 }
452476 };
453477 }
478+
479+ private function createRequest (string $ method , string $ path , string $ contentType , string $ content = '' ): Request
480+ {
481+ return new class ($ method , $ path , $ contentType , $ content ) implements Request {
482+ private $ method ;
483+ private $ path ;
484+ private $ contentType ;
485+ private $ content ;
486+
487+ public function __construct (string $ method , string $ path , string $ contentType , string $ content )
488+ {
489+ $ this ->method = $ method ;
490+ $ this ->path = $ path ;
491+ $ this ->contentType = $ contentType ;
492+ $ this ->content = $ content ;
493+ }
494+
495+ public function getMethod (): string
496+ {
497+ return $ this ->method ;
498+ }
499+
500+ public function getPath (): string
501+ {
502+ return $ this ->path ;
503+ }
504+
505+ public function getContentType (): string
506+ {
507+ return $ this ->contentType ;
508+ }
509+
510+ public function getContent (): string
511+ {
512+ return $ this ->content ;
513+ }
514+ };
515+ }
516+
517+ private function getContentTypeFromPath (string $ path ): string
518+ {
519+ $ tmp = parse_url ($ path );
520+
521+ $ path = strtolower ($ path );
522+
523+ if (false !== strpos ($ path , '/uploads.json ' ) || false !== strpos ($ path , '/uploads.xml ' )) {
524+ return 'application/octet-stream ' ;
525+ } elseif ('json ' === substr ($ tmp ['path ' ], -4 )) {
526+ return 'application/json ' ;
527+ } elseif ('xml ' === substr ($ tmp ['path ' ], -3 )) {
528+ return 'application/xml ' ;
529+ } else {
530+ return '' ;
531+ }
532+ }
454533}
0 commit comments