Skip to content

[WIP] Client / HttpClient decoupling for Guzzle integration #30

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

Closed
Closed
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
10 changes: 5 additions & 5 deletions lib/Github/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function configure()
*/
protected function get($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);
$response = $this->client->executeRequest('GET', $path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -45,7 +45,7 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
*/
protected function post($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
$response = $this->client->executeRequest('POST', $path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -55,7 +55,7 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
*/
protected function patch($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);
$response = $this->client->executeRequest('PATCH', $path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -65,7 +65,7 @@ protected function patch($path, array $parameters = array(), $requestHeaders = a
*/
protected function put($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);
$response = $this->client->executeRequest('PUT', $path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -75,7 +75,7 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
*/
protected function delete($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
$response = $this->client->executeRequest('DELETE', $path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand Down
45 changes: 30 additions & 15 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace Github;

use Buzz\Client\Curl;
use Buzz\Client\ClientInterface;

use Github\Api\ApiInterface;
use Github\Exception\InvalidArgumentException;
use Github\HttpClient\HttpClient;
use Github\HttpClient\Adapter\Buzz\HttpClient;
use Github\HttpClient\HttpClientInterface;
use Github\HttpClient\Listener\AuthListener;

/**
* Simple yet very cool PHP GitHub client
Expand Down Expand Up @@ -53,7 +49,6 @@ class Client
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
'timeout' => 10,

'api_limit' => 5000,
'api_version' => 'beta',

'cache_dir' => null
Expand Down Expand Up @@ -160,15 +155,35 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
$password = null;
}

$this->httpClient->addListener(
new AuthListener(
$authMethod,
array(
'tokenOrLogin' => $tokenOrLogin,
'password' => $password
)
)
);
switch ($authMethod) {
case Client::AUTH_HTTP_PASSWORD:
if (!$tokenOrLogin || !$password) {
throw new InvalidArgumentException('You need to set username with password!');
}
break;
case Client::AUTH_HTTP_TOKEN:
if (!$tokenOrLogin) {
throw new InvalidArgumentException('You need to set OAuth token!');
}
break;
case Client::AUTH_URL_CLIENT_ID:
if (!$tokenOrLogin || !$password) {
throw new InvalidArgumentException('You need to set client_id and client_secret!');
}
break;
case Client::AUTH_URL_TOKEN:
if (!$tokenOrLogin) {
throw new InvalidArgumentException('You need to set OAuth token!');
}
break;
}

$this->httpClient->authenticate($authMethod, $tokenOrLogin, $password);
}

public function executeRequest($method, $path, $parameters, $headers)
{
return $this->httpClient->request($path, $parameters, $method, $headers)->getContent();
}

/**
Expand Down
19 changes: 19 additions & 0 deletions lib/Github/Exception/InvalidJsonResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Github\Exception;

class InvalidJsonResponse extends RuntimeException
{
private $body;

public function __construct($body, $code = 0, $previous = null)
{
$this->body = $body;
parent::__construct('Invalid Json Response', $code, $previous);
}

public function getBody()
{
return $this->body;
}
}
100 changes: 100 additions & 0 deletions lib/Github/HttpClient/AbstractAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Github\HttpClient;

use Github\HttpClient\RequestInterface;
use Github\HttpClient\ResponseInterface;

abstract class AbstractAdapter implements HttpClientInterface
{
/**
* @var RequestInterface
*/
protected $lastRequest;

/**
* @var ResponseInterface
*/
protected $lastResponse;

/**
* @var array
*/
protected $options = array(
'base_url' => 'https://api.github.com/',
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
'timeout' => 10,
'api_version' => 'beta',
'cache_dir' => null
);
/**
* @var array
*/
protected $headers;

/**
* {@inheritDoc}
*/
public function setOption($name, $value)
{
$this->options[$name] = $value;
}

/**
* {@inheritDoc}
*/
public function setHeaders(array $headers)
{
$this->headers = array_merge($this->headers, $headers);
}

/**
* {@inheritDoc}
*/
public function clearHeaders()
{
$this->headers = array(
sprintf('Accept: application/vnd.github.%s+json', $this->options['api_version'])
);
}

/**
* {@inheritDoc}
*/
public function getLastRequest()
{
return $this->lastRequest;
}

/**
* {@inheritDoc}
*/
public function getLastResponse()
{
return $this->lastResponse;
}

/**
* {@inheritDoc}
*/
public function getAPILimit()
{
if (!$this->getLastRequest()) {
$this->get('/rate_limit');
}

return (int) $this->getLastRequest()->getHeaderAsString('X-RateLimit-Limit');
}

/**
* {@inheritDoc}
*/
public function getAPIRemaining()
{
if (!$this->getLastRequest()) {
$this->get('/rate_limit');
}

return (int) $this->getLastRequest()->getHeaderAsString('X-RateLimit-Remaining');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Github\HttpClient;
namespace Github\HttpClient\Adapter\Buzz;

use Buzz\Client\ClientInterface;

Expand Down
Loading