2
2
3
3
namespace Github \HttpClient \Message ;
4
4
5
- use Guzzle \Http \Message \Response ;
6
5
use Github \Exception \ApiLimitExceedException ;
6
+ use Psr \Http \Message \ResponseInterface ;
7
7
8
8
class ResponseMediator
9
9
{
10
- public static function getContent (Response $ response )
10
+ public static function getContent (ResponseInterface $ response )
11
11
{
12
- $ body = $ response ->getBody (true );
13
- if (strpos ($ response ->getContentType ( ), 'application/json ' ) === 0 ) {
12
+ $ body = $ response ->getBody ()-> __toString ( );
13
+ if (strpos ($ response ->getHeaderLine ( ' Content-Type ' ), 'application/json ' ) === 0 ) {
14
14
$ content = json_decode ($ body , true );
15
15
if (JSON_ERROR_NONE === json_last_error ()) {
16
16
return $ content ;
@@ -20,14 +20,14 @@ public static function getContent(Response $response)
20
20
return $ body ;
21
21
}
22
22
23
- public static function getPagination (Response $ response )
23
+ public static function getPagination (ResponseInterface $ response )
24
24
{
25
- $ header = (string ) $ response ->getHeader ('Link ' );
26
-
27
- if (empty ($ header )) {
25
+ if ($ response ->hasHeader ('Link ' )) {
28
26
return null ;
29
27
}
30
28
29
+ $ header = $ response ->getHeader ('Link ' );
30
+ $ header = array_shift ($ header );
31
31
$ pagination = array ();
32
32
foreach (explode (', ' , $ header ) as $ link ) {
33
33
preg_match ('/<(.*)>; rel="(.*)"/i ' , trim ($ link , ', ' ), $ match );
@@ -40,9 +40,10 @@ public static function getPagination(Response $response)
40
40
return $ pagination ;
41
41
}
42
42
43
- public static function getApiLimit (Response $ response )
43
+ public static function getApiLimit (ResponseInterface $ response )
44
44
{
45
- $ remainingCalls = (string ) $ response ->getHeader ('X-RateLimit-Remaining ' );
45
+ $ remainingCalls = $ response ->getHeader ('X-RateLimit-Remaining ' );
46
+ $ remainingCalls = array_shift ($ remainingCalls );
46
47
47
48
if (null !== $ remainingCalls && 1 > $ remainingCalls ) {
48
49
throw new ApiLimitExceedException ($ remainingCalls );
0 commit comments