Skip to content

Commit e0e6aae

Browse files
committed
Merge pull request KnpLabs#81 from KnpLabs/feature/guzzle
[1.2] Migrate from Buzz to Guzzle
2 parents c740117 + 390e21d commit e0e6aae

24 files changed

+348
-533
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Uses [GitHub API v3](http://developer.github.com/v3/). The object API is very si
1515
## Requirements
1616

1717
* PHP >= 5.3.2 with [cURL](http://php.net/manual/en/book.curl.php) extension,
18-
* [Buzz](https://github.com/kriswallsmith/Buzz) library,
18+
* [Guzzle](https://github.com/guzzle/guzzle) library,
1919
* (optional) PHPUnit to run tests.
2020

2121
## Autoload

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=5.3.2",
21-
"ext-curl": "*",
22-
"kriswallsmith/buzz": ">=0.7"
20+
"php": ">=5.3.2",
21+
"ext-curl": "*",
22+
"guzzle/guzzle": ">=3.7"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": ">=3.7"
2326
},
2427
"require-dev": {
2528
"phpunit/phpunit": ">=3.6.0"
@@ -29,7 +32,7 @@
2932
},
3033
"extra": {
3134
"branch-alias": {
32-
"dev-master": "1.2.x-dev"
35+
"dev-master": "1.3.x-dev"
3336
}
3437
}
3538
}

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/Api/ApiInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
namespace Github\Api;
44

5+
use Github\Client;
6+
57
/**
68
* Api interface
79
*
810
* @author Joseph Bielawski <stloyd@gmail.com>
911
*/
1012
interface ApiInterface
1113
{
14+
public function __construct(Client $client);
15+
16+
public function getPerPage();
17+
18+
public function setPerPage($perPage);
1219
}

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/CachedHttpClient.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
6161
*
6262
* {@inheritdoc}
6363
*/
64-
protected function createRequest($httpMethod, $url)
64+
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
6565
{
66-
$request = parent::createRequest($httpMethod, $url);
66+
$request = parent::createRequest($httpMethod, $path, $requestBody, $headers = array());
6767

68-
if ($modifiedAt = $this->getCache()->getModifiedSince($url)) {
68+
if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {
6969
$modifiedAt = new \DateTime('@'.$modifiedAt);
7070
$modifiedAt->setTimezone(new \DateTimeZone('GMT'));
7171

72-
$request->addHeader(sprintf('If-Modified-Since: %s GMT', $modifiedAt->format('l, d-M-y H:i:s')));
72+
$request->addHeader(
73+
'If-Modified-Since',
74+
sprintf('%s GMT', $modifiedAt->format('l, d-M-y H:i:s'))
75+
);
7376
}
7477

7578
return $request;

lib/Github/HttpClient/HttpClient.php

Lines changed: 28 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
namespace Github\HttpClient;
44

5-
use Buzz\Client\ClientInterface;
6-
use Buzz\Listener\ListenerInterface;
5+
use Guzzle\Http\Client as GuzzleClient;
6+
use Guzzle\Http\ClientInterface;
7+
use Guzzle\Http\Message\Request;
8+
use Guzzle\Http\Message\Response;
79

810
use Github\Exception\ErrorException;
911
use Github\Exception\RuntimeException;
1012
use Github\HttpClient\Listener\AuthListener;
1113
use Github\HttpClient\Listener\ErrorListener;
12-
use Github\HttpClient\Message\Request;
13-
use Github\HttpClient\Message\Response;
14-
use Buzz\Client\Curl;
1514

1615
/**
1716
* Performs requests on GitHub API. API documentation should be self-explanatory.
@@ -20,27 +19,18 @@
2019
*/
2120
class HttpClient implements HttpClientInterface
2221
{
23-
/**
24-
* @var array
25-
*/
2622
protected $options = array(
2723
'base_url' => 'https://api.github.com/',
2824

2925
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
3026
'timeout' => 10,
3127

3228
'api_limit' => 5000,
33-
'api_version' => 'beta',
29+
'api_version' => 'v3',
3430

3531
'cache_dir' => null
3632
);
37-
/**
38-
* @var array
39-
*/
40-
protected $listeners = array();
41-
/**
42-
* @var array
43-
*/
33+
4434
protected $headers = array();
4535

4636
private $lastResponse;
@@ -52,32 +42,14 @@ class HttpClient implements HttpClientInterface
5242
*/
5343
public function __construct(array $options = array(), ClientInterface $client = null)
5444
{
55-
$client = $client ?: new Curl();
56-
$timeout = isset($options['timeout']) ? $options['timeout'] : $this->options['timeout'];
57-
$client->setTimeout($timeout);
58-
$client->setVerifyPeer(false);
59-
6045
$this->options = array_merge($this->options, $options);
46+
$client = $client ?: new GuzzleClient($options['base_url'], $this->options);
6147
$this->client = $client;
6248

63-
$this->addListener(new ErrorListener($this->options));
64-
49+
$this->addListener('request.error', array(new ErrorListener($this->options), 'onRequestError'));
6550
$this->clearHeaders();
6651
}
6752

68-
public function authenticate($tokenOrLogin, $password, $authMethod)
69-
{
70-
$this->addListener(
71-
new AuthListener(
72-
$authMethod,
73-
array(
74-
'tokenOrLogin' => $tokenOrLogin,
75-
'password' => $password
76-
)
77-
)
78-
);
79-
}
80-
8153
/**
8254
* {@inheritDoc}
8355
*/
@@ -105,24 +77,17 @@ public function clearHeaders()
10577
);
10678
}
10779

108-
/**
109-
* @param ListenerInterface $listener
110-
*/
111-
public function addListener(ListenerInterface $listener)
80+
public function addListener($eventName, $listener)
11281
{
113-
$this->listeners[get_class($listener)] = $listener;
82+
$this->client->getEventDispatcher()->addListener($eventName, $listener);
11483
}
11584

11685
/**
11786
* {@inheritDoc}
11887
*/
11988
public function get($path, array $parameters = array(), array $headers = array())
12089
{
121-
if (0 < count($parameters)) {
122-
$path .= (false === strpos($path, '?') ? '?' : '&').http_build_query($parameters, '', '&');
123-
}
124-
125-
return $this->request($path, array(), 'GET', $headers);
90+
return $this->request($path, $parameters, 'GET', $headers);
12691
}
12792

12893
/**
@@ -162,27 +127,15 @@ public function put($path, array $parameters = array(), array $headers = array()
162127
*/
163128
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
164129
{
165-
if (!empty($this->options['base_url']) && 0 !== strpos($path, $this->options['base_url'])) {
166-
$path = trim($this->options['base_url'].$path, '/');
167-
}
130+
$requestBody = count($parameters) === 0
131+
? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0)
132+
;
168133

169-
$request = $this->createRequest($httpMethod, $path);
134+
$request = $this->createRequest($httpMethod, $path, $requestBody, $headers);
170135
$request->addHeaders($headers);
171-
if (count($parameters) > 0) {
172-
$request->setContent(json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0));
173-
}
174-
175-
$hasListeners = 0 < count($this->listeners);
176-
if ($hasListeners) {
177-
foreach ($this->listeners as $listener) {
178-
$listener->preSend($request);
179-
}
180-
}
181-
182-
$response = $this->createResponse();
183136

184137
try {
185-
$this->client->send($request, $response);
138+
$response = $this->client->send($request);
186139
} catch (\LogicException $e) {
187140
throw new ErrorException($e->getMessage());
188141
} catch (\RuntimeException $e) {
@@ -192,15 +145,19 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
192145
$this->lastRequest = $request;
193146
$this->lastResponse = $response;
194147

195-
if ($hasListeners) {
196-
foreach ($this->listeners as $listener) {
197-
$listener->postSend($request, $response);
198-
}
199-
}
200-
201148
return $response;
202149
}
203150

151+
/**
152+
* {@inheritDoc}
153+
*/
154+
public function authenticate($tokenOrLogin, $password = null, $method)
155+
{
156+
$this->addListener('request.before_send', array(
157+
new AuthListener($tokenOrLogin, $password, $method), 'onRequestBeforeSend'
158+
));
159+
}
160+
204161
/**
205162
* @return Request
206163
*/
@@ -217,26 +174,8 @@ public function getLastResponse()
217174
return $this->lastResponse;
218175
}
219176

220-
/**
221-
* @param string $httpMethod
222-
* @param string $url
223-
*
224-
* @return Request
225-
*/
226-
protected function createRequest($httpMethod, $url)
227-
{
228-
$request = new Request($httpMethod);
229-
$request->setHeaders($this->headers);
230-
$request->fromUrl($url);
231-
232-
return $request;
233-
}
234-
235-
/**
236-
* @return Response
237-
*/
238-
protected function createResponse()
177+
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
239178
{
240-
return new Response();
179+
return $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $requestBody);
241180
}
242181
}

0 commit comments

Comments
 (0)