9
9
use Redmine \Exception ;
10
10
use Redmine \Exception \SerializerException ;
11
11
use Redmine \Http \HttpClient ;
12
+ use Redmine \Http \Request ;
12
13
use Redmine \Http \Response ;
13
14
use Redmine \Serializer \JsonSerializer ;
14
15
use Redmine \Serializer \PathSerializer ;
@@ -108,9 +109,13 @@ public function lastCallFailed()
108
109
*/
109
110
protected function get ($ path , $ decodeIfJson = true )
110
111
{
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
+ ));
112
117
113
- $ body = $ this ->lastResponse ->getBody ();
118
+ $ body = $ this ->lastResponse ->getContent ();
114
119
$ contentType = $ this ->lastResponse ->getContentType ();
115
120
116
121
// if response is XML, return a SimpleXMLElement object
@@ -139,9 +144,14 @@ protected function get($path, $decodeIfJson = true)
139
144
*/
140
145
protected function post ($ path , $ data )
141
146
{
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 ();
145
155
$ contentType = $ this ->lastResponse ->getContentType ();
146
156
147
157
// if response is XML, return a SimpleXMLElement object
@@ -162,9 +172,14 @@ protected function post($path, $data)
162
172
*/
163
173
protected function put ($ path , $ data )
164
174
{
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 ();
168
183
$ contentType = $ this ->lastResponse ->getContentType ();
169
184
170
185
// if response is XML, return a SimpleXMLElement object
@@ -184,9 +199,13 @@ protected function put($path, $data)
184
199
*/
185
200
protected function delete ($ path )
186
201
{
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
+ ));
188
207
189
- return $ this ->lastResponse ->getBody ();
208
+ return $ this ->lastResponse ->getContent ();
190
209
}
191
210
192
211
/**
@@ -234,7 +253,7 @@ protected function retrieveAll($endpoint, array $params = [])
234
253
try {
235
254
$ data = $ this ->retrieveData (strval ($ endpoint ), $ params );
236
255
} catch (Exception $ e ) {
237
- if ($ this ->getLastResponse ()->getBody () === '' ) {
256
+ if ($ this ->getLastResponse ()->getContent () === '' ) {
238
257
return false ;
239
258
}
240
259
@@ -258,7 +277,11 @@ protected function retrieveAll($endpoint, array $params = [])
258
277
protected function retrieveData (string $ endpoint , array $ params = []): array
259
278
{
260
279
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
+ ));
262
285
263
286
return $ this ->getResponseAsArray ($ this ->lastResponse );
264
287
}
@@ -287,10 +310,11 @@ protected function retrieveData(string $endpoint, array $params = []): array
287
310
$ params ['limit ' ] = $ _limit ;
288
311
$ params ['offset ' ] = $ offset ;
289
312
290
- $ this ->lastResponse = $ this ->getHttpClient ()->request (
313
+ $ this ->lastResponse = $ this ->getHttpClient ()->request ($ this -> createRequest (
291
314
'GET ' ,
292
- PathSerializer::create ($ endpoint , $ params )->getPath ()
293
- );
315
+ PathSerializer::create ($ endpoint , $ params )->getPath (),
316
+ $ this ->getContentTypeFromPath ($ endpoint )
317
+ ));
294
318
295
319
$ newDataSet = $ this ->getResponseAsArray ($ this ->lastResponse );
296
320
@@ -368,7 +392,7 @@ protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
368
392
*/
369
393
private function getResponseAsArray (Response $ response ): array
370
394
{
371
- $ body = $ response ->getBody ();
395
+ $ body = $ response ->getContent ();
372
396
$ contentType = $ response ->getContentType ();
373
397
$ returnData = null ;
374
398
@@ -400,16 +424,16 @@ public function __construct(Client $client, Closure $responseFactory)
400
424
$ this ->responseFactory = $ responseFactory ;
401
425
}
402
426
403
- public function request (string $ method , string $ path , string $ body = '' ): Response
427
+ public function request (Request $ request ): Response
404
428
{
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 () );
411
435
} else {
412
- $ this ->client ->requestGet ($ path );
436
+ $ this ->client ->requestGet ($ request -> getPath () );
413
437
}
414
438
415
439
return ($ this ->responseFactory )(
@@ -445,10 +469,65 @@ public function getContentType(): string
445
469
return $ this ->contentType ;
446
470
}
447
471
448
- public function getBody (): string
472
+ public function getContent (): string
449
473
{
450
474
return $ this ->body ;
451
475
}
452
476
};
453
477
}
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
+ }
454
533
}
0 commit comments