diff --git a/composer.json b/composer.json index 2a3ed373853..940bf452c6f 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,9 @@ "require": { "php": ">=5.3.2", "ext-curl": "*", - "kriswallsmith/buzz": "0.6" + "kriswallsmith/buzz": "0.7" }, "autoload": { "psr-0": { "Github": "lib/" } } -} \ No newline at end of file +} diff --git a/lib/Github/HttpClient/HttpClient.php b/lib/Github/HttpClient/HttpClient.php index c15e965d15c..33fd2b86697 100644 --- a/lib/Github/HttpClient/HttpClient.php +++ b/lib/Github/HttpClient/HttpClient.php @@ -4,7 +4,9 @@ use Buzz\Browser; use Buzz\Client\Curl; -use Buzz\Message\Response; +use Buzz\Message\MessageInterface; + +use Github\HttpClient\Listener\AuthListener; /** * Performs requests on GitHub API. API documentation should be self-explanatory. @@ -19,16 +21,18 @@ class HttpClient implements HttpClientInterface * @var array */ protected $options = array( - 'url' => 'https://api.github.com/:path', - 'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)', - 'http_port' => 443, - 'timeout' => 10, + 'url' => 'https://api.github.com/:path', + 'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)', + 'http_port' => 443, + 'timeout' => 10, + + 'api_limit' => 5000, - 'api_limit' => 5000, + 'auth_method' => null, - 'login' => null, - 'password' => null, - 'token' => null, + 'login' => null, + 'password' => null, + 'token' => null, ); /** @@ -42,7 +46,7 @@ class HttpClient implements HttpClientInterface protected $headers = array(); /** - * @var Buzz\Browser + * @var Browser */ protected $browser; @@ -60,17 +64,23 @@ public function __construct(array $options = array(), Browser $browser = null) $this->browser->getClient()->setTimeout($this->options['timeout']); $this->browser->getClient()->setVerifyPeer(false); - if ($this->options['login']) { + if (null !== $this->options['login'] || null !== $this->options['token']) { + if (null !== $this->options['token']) { + $options = array($this->options['token']); + } else { + $options = array($this->options['login'], $this->options['password']); + } + $this->browser->addListener( - new Listener\AuthListener( - $this->options['auth_method'], - array($this->options['login'], $this->options['password']) - ) + new AuthListener($this->options['auth_method'], $options) ); } } - public function setHeaders($headers) + /** + * @param array $headers + */ + public function setHeaders(array $headers) { $this->headers = $headers; } @@ -91,7 +101,7 @@ public function setOption($name, $value) } /** - * {@inheridoc} + * {@inheritDoc} */ public function get($path, array $parameters = array(), array $options = array()) { @@ -99,7 +109,7 @@ public function get($path, array $parameters = array(), array $options = array() } /** - * {@inheridoc} + * {@inheritDoc} */ public function post($path, array $parameters = array(), array $options = array()) { @@ -107,7 +117,7 @@ public function post($path, array $parameters = array(), array $options = array( } /** - * {@inheridoc} + * {@inheritDoc} */ public function patch($path, array $parameters = array(), array $options = array()) { @@ -115,7 +125,7 @@ public function patch($path, array $parameters = array(), array $options = array } /** - * {@inheridoc} + * {@inheritDoc} */ public function delete($path, array $parameters = array(), array $options = array()) { @@ -123,7 +133,7 @@ public function delete($path, array $parameters = array(), array $options = arra } /** - * {@inheridoc} + * {@inheritDoc} */ public function put($path, array $options = array()) { @@ -164,7 +174,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET', * @param string $httpMethod HTTP method to use * @param array $options Request options * - * @return string HTTP response + * @return array HTTP response */ protected function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array()) { @@ -203,11 +213,11 @@ protected function decodeResponse($response) /** * Report to user he reached his GitHub API limit. * - * @param Response $response + * @param MessageInterface $response * * @throws \RuntimeException */ - protected function checkApiLimit(Response $response) + protected function checkApiLimit(MessageInterface $response) { $limit = $response->getHeader('X-RateLimit-Remaining'); diff --git a/lib/Github/HttpClient/HttpClientInterface.php b/lib/Github/HttpClient/HttpClientInterface.php index 137911d8fa4..fbc415499c0 100644 --- a/lib/Github/HttpClient/HttpClientInterface.php +++ b/lib/Github/HttpClient/HttpClientInterface.php @@ -79,5 +79,5 @@ function setOption($name, $value); * * @param array */ - function setHeaders($headers); + function setHeaders(array $headers); } diff --git a/lib/Github/HttpClient/Listener/Auth.php b/lib/Github/HttpClient/Listener/Auth.php index b1ebe731d42..82cbccd6d58 100644 --- a/lib/Github/HttpClient/Listener/Auth.php +++ b/lib/Github/HttpClient/Listener/Auth.php @@ -4,8 +4,9 @@ use Github\Client; -use Buzz\Message; use Buzz\Listener\ListenerInterface; +use Buzz\Message\MessageInterface; +use Buzz\Message\RequestInterface; use Buzz\Util\Url; class AuthListener implements ListenerInterface @@ -28,14 +29,17 @@ class AuthListener implements ListenerInterface public function __construct($method, array $options) { if (!isset($options['token']) || (!isset($options['login'], $options['password']))) { - throw new \InvalidArgumentException('You need to set OAuth token, or username + password!'); + throw new \InvalidArgumentException('You need to set OAuth token, or username with password!'); } $this->method = $method; $this->options = $options; } - public function preSend(Message\Request $request) + /** + * {@inheritDoc} + */ + public function preSend(RequestInterface $request) { switch ($this->method) { case Client::AUTH_HTTP_PASSWORD: @@ -48,14 +52,12 @@ public function preSend(Message\Request $request) default: $url = $request->getUrl(); - $parameters = array( - 'access_token' => $this->options['token'] - ); - - $queryString = utf8_encode(http_build_query($parameters, '', '&')); - if ('GET' === $request->getMethod()) { - $url .= '?'.$queryString; + $parameters = array( + 'access_token' => $this->options['token'] + ); + + $url .= '?'.utf8_encode(http_build_query($parameters, '', '&')); } $request->fromUrl(new Url($url)); @@ -63,7 +65,10 @@ public function preSend(Message\Request $request) } } - public function postSend(Message\Request $request, Message\Response $response) + /** + * {@inheritDoc} + */ + public function postSend(RequestInterface $request, MessageInterface $response) { } }