Skip to content

Commit 578f7d8

Browse files
committed
Merge pull request #4 from KnpLabs/buzz_07
Adjusted code to use changes introduced by Buzz 0.7, also fixed call of AuthListener.
2 parents bec7903 + 1a165de commit 578f7d8

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"require": {
2020
"php": ">=5.3.2",
2121
"ext-curl": "*",
22-
"kriswallsmith/buzz": "0.6"
22+
"kriswallsmith/buzz": "0.7"
2323
},
2424
"autoload": {
2525
"psr-0": { "Github": "lib/" }
2626
}
27-
}
27+
}

lib/Github/HttpClient/HttpClient.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Buzz\Browser;
66
use Buzz\Client\Curl;
7-
use Buzz\Message\Response;
7+
use Buzz\Message\MessageInterface;
8+
9+
use Github\HttpClient\Listener\AuthListener;
810

911
/**
1012
* Performs requests on GitHub API. API documentation should be self-explanatory.
@@ -19,16 +21,18 @@ class HttpClient implements HttpClientInterface
1921
* @var array
2022
*/
2123
protected $options = array(
22-
'url' => 'https://api.github.com/:path',
23-
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
24-
'http_port' => 443,
25-
'timeout' => 10,
24+
'url' => 'https://api.github.com/:path',
25+
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
26+
'http_port' => 443,
27+
'timeout' => 10,
28+
29+
'api_limit' => 5000,
2630

27-
'api_limit' => 5000,
31+
'auth_method' => null,
2832

29-
'login' => null,
30-
'password' => null,
31-
'token' => null,
33+
'login' => null,
34+
'password' => null,
35+
'token' => null,
3236
);
3337

3438
/**
@@ -42,7 +46,7 @@ class HttpClient implements HttpClientInterface
4246
protected $headers = array();
4347

4448
/**
45-
* @var Buzz\Browser
49+
* @var Browser
4650
*/
4751
protected $browser;
4852

@@ -60,17 +64,23 @@ public function __construct(array $options = array(), Browser $browser = null)
6064
$this->browser->getClient()->setTimeout($this->options['timeout']);
6165
$this->browser->getClient()->setVerifyPeer(false);
6266

63-
if ($this->options['login']) {
67+
if (null !== $this->options['login'] || null !== $this->options['token']) {
68+
if (null !== $this->options['token']) {
69+
$options = array($this->options['token']);
70+
} else {
71+
$options = array($this->options['login'], $this->options['password']);
72+
}
73+
6474
$this->browser->addListener(
65-
new Listener\AuthListener(
66-
$this->options['auth_method'],
67-
array($this->options['login'], $this->options['password'])
68-
)
75+
new AuthListener($this->options['auth_method'], $options)
6976
);
7077
}
7178
}
7279

73-
public function setHeaders($headers)
80+
/**
81+
* @param array $headers
82+
*/
83+
public function setHeaders(array $headers)
7484
{
7585
$this->headers = $headers;
7686
}
@@ -91,39 +101,39 @@ public function setOption($name, $value)
91101
}
92102

93103
/**
94-
* {@inheridoc}
104+
* {@inheritDoc}
95105
*/
96106
public function get($path, array $parameters = array(), array $options = array())
97107
{
98108
return $this->request($path, $parameters, 'GET', $options);
99109
}
100110

101111
/**
102-
* {@inheridoc}
112+
* {@inheritDoc}
103113
*/
104114
public function post($path, array $parameters = array(), array $options = array())
105115
{
106116
return $this->request($path, $parameters, 'POST', $options);
107117
}
108118

109119
/**
110-
* {@inheridoc}
120+
* {@inheritDoc}
111121
*/
112122
public function patch($path, array $parameters = array(), array $options = array())
113123
{
114124
return $this->request($path, $parameters, 'PATCH', $options);
115125
}
116126

117127
/**
118-
* {@inheridoc}
128+
* {@inheritDoc}
119129
*/
120130
public function delete($path, array $parameters = array(), array $options = array())
121131
{
122132
return $this->request($path, $parameters, 'DELETE', $options);
123133
}
124134

125135
/**
126-
* {@inheridoc}
136+
* {@inheritDoc}
127137
*/
128138
public function put($path, array $options = array())
129139
{
@@ -164,7 +174,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
164174
* @param string $httpMethod HTTP method to use
165175
* @param array $options Request options
166176
*
167-
* @return string HTTP response
177+
* @return array HTTP response
168178
*/
169179
protected function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
170180
{
@@ -203,11 +213,11 @@ protected function decodeResponse($response)
203213
/**
204214
* Report to user he reached his GitHub API limit.
205215
*
206-
* @param Response $response
216+
* @param MessageInterface $response
207217
*
208218
* @throws \RuntimeException
209219
*/
210-
protected function checkApiLimit(Response $response)
220+
protected function checkApiLimit(MessageInterface $response)
211221
{
212222
$limit = $response->getHeader('X-RateLimit-Remaining');
213223

lib/Github/HttpClient/HttpClientInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,5 @@ function setOption($name, $value);
7979
*
8080
* @param array
8181
*/
82-
function setHeaders($headers);
82+
function setHeaders(array $headers);
8383
}

lib/Github/HttpClient/Listener/Auth.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use Github\Client;
66

7-
use Buzz\Message;
87
use Buzz\Listener\ListenerInterface;
8+
use Buzz\Message\MessageInterface;
9+
use Buzz\Message\RequestInterface;
910
use Buzz\Util\Url;
1011

1112
class AuthListener implements ListenerInterface
@@ -28,14 +29,17 @@ class AuthListener implements ListenerInterface
2829
public function __construct($method, array $options)
2930
{
3031
if (!isset($options['token']) || (!isset($options['login'], $options['password']))) {
31-
throw new \InvalidArgumentException('You need to set OAuth token, or username + password!');
32+
throw new \InvalidArgumentException('You need to set OAuth token, or username with password!');
3233
}
3334

3435
$this->method = $method;
3536
$this->options = $options;
3637
}
3738

38-
public function preSend(Message\Request $request)
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
public function preSend(RequestInterface $request)
3943
{
4044
switch ($this->method) {
4145
case Client::AUTH_HTTP_PASSWORD:
@@ -48,22 +52,23 @@ public function preSend(Message\Request $request)
4852
default:
4953
$url = $request->getUrl();
5054

51-
$parameters = array(
52-
'access_token' => $this->options['token']
53-
);
54-
55-
$queryString = utf8_encode(http_build_query($parameters, '', '&'));
56-
5755
if ('GET' === $request->getMethod()) {
58-
$url .= '?'.$queryString;
56+
$parameters = array(
57+
'access_token' => $this->options['token']
58+
);
59+
60+
$url .= '?'.utf8_encode(http_build_query($parameters, '', '&'));
5961
}
6062

6163
$request->fromUrl(new Url($url));
6264
break;
6365
}
6466
}
6567

66-
public function postSend(Message\Request $request, Message\Response $response)
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function postSend(RequestInterface $request, MessageInterface $response)
6772
{
6873
}
6974
}

0 commit comments

Comments
 (0)