Skip to content

Commit 390e21d

Browse files
committed
Replace Response with ResponseMediator
1 parent 9b55adb commit 390e21d

14 files changed

+143
-195
lines changed

lib/Github/Api/AbstractApi.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Github\Api;
44

55
use Github\Client;
6+
use Github\HttpClient\Message\ResponseMediator;
67

78
/**
89
* Abstract class for Api classes
@@ -65,7 +66,7 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
6566
}
6667
$response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);
6768

68-
return $response->getContent();
69+
return ResponseMediator::getContent($response);
6970
}
7071

7172
/**
@@ -75,7 +76,7 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
7576
{
7677
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
7778

78-
return $response->getContent();
79+
return ResponseMediator::getContent($response);
7980
}
8081

8182
/**
@@ -85,7 +86,7 @@ protected function patch($path, array $parameters = array(), $requestHeaders = a
8586
{
8687
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);
8788

88-
return $response->getContent();
89+
return ResponseMediator::getContent($response);
8990
}
9091

9192
/**
@@ -95,7 +96,7 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
9596
{
9697
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);
9798

98-
return $response->getContent();
99+
return ResponseMediator::getContent($response);
99100
}
100101

101102
/**
@@ -105,6 +106,6 @@ protected function delete($path, array $parameters = array(), $requestHeaders =
105106
{
106107
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
107108

108-
return $response->getContent();
109+
return ResponseMediator::getContent($response);
109110
}
110111
}

lib/Github/HttpClient/Cache/CacheInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Github\HttpClient\Cache;
44

5-
use Github\HttpClient\Message\Response;
5+
use Guzzle\Http\Message\Response;
66

77
/**
88
* Caches github api responses

lib/Github/HttpClient/Cache/FilesystemCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Github\HttpClient\Cache;
44

5-
use Github\HttpClient\Message\Response;
5+
use Guzzle\Http\Message\Response;
66

77
class FilesystemCache implements CacheInterface
88
{

lib/Github/HttpClient/HttpClient.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Github\HttpClient;
44

5-
use Github\HttpClient\Listener\AuthListener;
6-
use Github\HttpClient\Listener\ErrorListener;
75
use Guzzle\Http\Client as GuzzleClient;
86
use Guzzle\Http\ClientInterface;
97
use Guzzle\Http\Message\Request;
8+
use Guzzle\Http\Message\Response;
109

1110
use Github\Exception\ErrorException;
1211
use Github\Exception\RuntimeException;
13-
use Github\HttpClient\Message\Response;
12+
use Github\HttpClient\Listener\AuthListener;
13+
use Github\HttpClient\Listener\ErrorListener;
1414

1515
/**
1616
* Performs requests on GitHub API. API documentation should be self-explanatory.
@@ -135,7 +135,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
135135
$request->addHeaders($headers);
136136

137137
try {
138-
$response = $this->createResponse($this->client->send($request));
138+
$response = $this->client->send($request);
139139
} catch (\LogicException $e) {
140140
throw new ErrorException($e->getMessage());
141141
} catch (\RuntimeException $e) {
@@ -178,9 +178,4 @@ protected function createRequest($httpMethod, $path, $requestBody, array $header
178178
{
179179
return $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $requestBody);
180180
}
181-
182-
protected function createResponse($response)
183-
{
184-
return Response::fromMessage($response);
185-
}
186181
}

lib/Github/HttpClient/Listener/ErrorListener.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Github\HttpClient\Listener;
44

5+
use Github\HttpClient\Message\ResponseMediator;
56
use Guzzle\Common\Event;
6-
use Guzzle\Http\Message\Response as GuzzleResponse;
7+
use Guzzle\Http\Message\Response;
78

8-
use Github\HttpClient\Message\Response;
99
use Github\Exception\ApiLimitExceedException;
1010
use Github\Exception\ErrorException;
1111
use Github\Exception\RuntimeException;
@@ -36,7 +36,7 @@ public function onRequestError(Event $event)
3636
{
3737
/** @var $request \Guzzle\Http\Message\Request */
3838
$request = $event['request'];
39-
$response = $this->createResponse($request->getResponse());
39+
$response = $request->getResponse();
4040

4141
if ($response->isClientError() || $response->isServerError()) {
4242
$remaining = (string) $response->getHeader('X-RateLimit-Remaining');
@@ -45,7 +45,7 @@ public function onRequestError(Event $event)
4545
throw new ApiLimitExceedException($this->options['api_limit']);
4646
}
4747

48-
$content = $response->getContent();
48+
$content = ResponseMediator::getContent($response);
4949
if (is_array($content) && isset($content['message'])) {
5050
if (400 == $response->getStatusCode()) {
5151
throw new ErrorException($content['message'], 400);
@@ -83,13 +83,4 @@ public function onRequestError(Event $event)
8383
throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
8484
};
8585
}
86-
87-
protected function createResponse(GuzzleResponse $response)
88-
{
89-
if (!($response instanceof Response)) {
90-
return Response::fromMessage($response);
91-
} else {
92-
return $response;
93-
}
94-
}
9586
}

lib/Github/HttpClient/Message/Response.php

Lines changed: 0 additions & 96 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Github\HttpClient\Message;
4+
5+
use Guzzle\Http\Message\Response;
6+
use Github\Exception\ApiLimitExceedException;
7+
8+
class ResponseMediator
9+
{
10+
public static function getContent(Response $response)
11+
{
12+
$body = $response->getBody(true);
13+
$content = json_decode($body, true);
14+
15+
if (JSON_ERROR_NONE !== json_last_error()) {
16+
return $body;
17+
}
18+
19+
return $content;
20+
}
21+
22+
public static function getPagination(Response $response)
23+
{
24+
$header = $response->getHeader('Link');
25+
26+
if (empty($header)) {
27+
return null;
28+
}
29+
30+
$pagination = array();
31+
foreach (explode(',', $header) as $link) {
32+
preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match);
33+
34+
if (3 === count($match)) {
35+
$pagination[$match[2]] = $match[1];
36+
}
37+
}
38+
39+
return $pagination;
40+
}
41+
42+
public static function getApiLimit(Response $response)
43+
{
44+
$remainingCalls = $response->getHeader('X-RateLimit-Remaining');
45+
46+
if (null !== $remainingCalls && 1 > $remainingCalls) {
47+
throw new ApiLimitExceedException($remainingCalls);
48+
}
49+
}
50+
}

lib/Github/ResultPager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Github;
44

55
use Github\Api\ApiInterface;
6+
use Github\HttpClient\Message\ResponseMediator;
67

78
/**
89
* Pager class for supporting pagination in github classes
@@ -88,7 +89,7 @@ public function fetchAll(ApiInterface $api, $method, array $parameters = array()
8889
*/
8990
public function postFetch()
9091
{
91-
$this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination();
92+
$this->pagination = ResponseMediator::getPagination($this->client->getHttpClient()->getLastResponse());
9293
}
9394

9495
/**
@@ -156,7 +157,7 @@ protected function get($key)
156157
$result = $this->client->getHttpClient()->get($this->pagination[$key]);
157158
$this->postFetch();
158159

159-
return $result->getContent();
160+
return ResponseMediator::getContent($result);
160161
}
161162
}
162163
}

test/Github/Tests/HttpClient/Cache/FilesystemCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Github\Tests\HttpClient\Cache;
44

5-
use Github\HttpClient\Message\Response;
5+
use Guzzle\Http\Message\Response;
66
use Github\HttpClient\Cache\FilesystemCache;
77

88
class FilesystemCacheTest extends \PHPUnit_Framework_TestCase

0 commit comments

Comments
 (0)