Skip to content

Adjusted code to use changes introduced by Buzz 0.7, also fixed call of AuthListener. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"require": {
"php": ">=5.3.2",
"ext-curl": "*",
"kriswallsmith/buzz": "0.6"
"kriswallsmith/buzz": "0.7"
},
"autoload": {
"psr-0": { "Github": "lib/" }
}
}
}
58 changes: 34 additions & 24 deletions lib/Github/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
);

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

/**
* @var Buzz\Browser
* @var Browser
*/
protected $browser;

Expand All @@ -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;
}
Expand All @@ -91,39 +101,39 @@ public function setOption($name, $value)
}

/**
* {@inheridoc}
* {@inheritDoc}
*/
public function get($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'GET', $options);
}

/**
* {@inheridoc}
* {@inheritDoc}
*/
public function post($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'POST', $options);
}

/**
* {@inheridoc}
* {@inheritDoc}
*/
public function patch($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'PATCH', $options);
}

/**
* {@inheridoc}
* {@inheritDoc}
*/
public function delete($path, array $parameters = array(), array $options = array())
{
return $this->request($path, $parameters, 'DELETE', $options);
}

/**
* {@inheridoc}
* {@inheritDoc}
*/
public function put($path, array $options = array())
{
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion lib/Github/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ function setOption($name, $value);
*
* @param array
*/
function setHeaders($headers);
function setHeaders(array $headers);
}
27 changes: 16 additions & 11 deletions lib/Github/HttpClient/Listener/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -48,22 +52,23 @@ 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));
break;
}
}

public function postSend(Message\Request $request, Message\Response $response)
/**
* {@inheritDoc}
*/
public function postSend(RequestInterface $request, MessageInterface $response)
{
}
}