Skip to content

Commit c90c84c

Browse files
committed
Migrate from Buzz to Guzzle
1 parent ce011c3 commit c90c84c

19 files changed

+258
-394
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,16 +17,19 @@
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
"autoload": {
2528
"psr-0": { "Github\\": "lib/" }
2629
},
2730
"extra": {
2831
"branch-alias": {
29-
"dev-master": "1.2.x-dev"
32+
"dev-master": "1.3.x-dev"
3033
}
3134
}
3235
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ protected function createRequest($httpMethod, $url)
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 & 92 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 Github\HttpClient\Listener\AuthListener;
6+
use Github\HttpClient\Listener\ErrorListener;
7+
use Guzzle\Http\Client as GuzzleClient;
8+
use Guzzle\Http\ClientInterface;
9+
use Guzzle\Http\Message\Request;
710

811
use Github\Exception\ErrorException;
912
use Github\Exception\RuntimeException;
10-
use Github\HttpClient\Listener\AuthListener;
11-
use Github\HttpClient\Listener\ErrorListener;
12-
use Github\HttpClient\Message\Request;
1313
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,17 @@ 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->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $requestBody);
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 = Response::fromMessage(
139+
$this->client->send($request)
140+
);
186141
} catch (\LogicException $e) {
187142
throw new ErrorException($e->getMessage());
188143
} catch (\RuntimeException $e) {
@@ -192,51 +147,32 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
192147
$this->lastRequest = $request;
193148
$this->lastResponse = $response;
194149

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

204153
/**
205-
* @return Request
206-
*/
207-
public function getLastRequest()
208-
{
209-
return $this->lastRequest;
210-
}
211-
212-
/**
213-
* @return Response
154+
* {@inheritDoc}
214155
*/
215-
public function getLastResponse()
156+
public function authenticate($tokenOrLogin, $password = null, $method)
216157
{
217-
return $this->lastResponse;
158+
$this->addListener('request.before_send', array(
159+
new AuthListener($tokenOrLogin, $password, $method), 'onRequestBeforeSend'
160+
));
218161
}
219162

220163
/**
221-
* @param string $httpMethod
222-
* @param string $url
223-
*
224164
* @return Request
225165
*/
226-
protected function createRequest($httpMethod, $url)
166+
public function getLastRequest()
227167
{
228-
$request = new Request($httpMethod);
229-
$request->setHeaders($this->headers);
230-
$request->fromUrl($url);
231-
232-
return $request;
168+
return $this->lastRequest;
233169
}
234170

235171
/**
236172
* @return Response
237173
*/
238-
protected function createResponse()
174+
public function getLastResponse()
239175
{
240-
return new Response();
176+
return $this->lastResponse;
241177
}
242178
}

0 commit comments

Comments
 (0)