Skip to content

[WIP][2] Client / HttpClient decoupling for Guzzle integration #80

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
wants to merge 16 commits into from
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ php:

before_script:
- composer install --dev --prefer-source
- composer require "kriswallsmith/buzz:>=0.7"

script:
- phpunit --coverage-text
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
"require": {
"php": ">=5.3.2",
"ext-curl": "*",
"kriswallsmith/buzz": ">=0.7"
"guzzle/guzzle": "3.7.*"
},
"require-dev": {
"phpunit/phpunit": ">=3.6.0"
},
"suggests": {
"kriswallsmith/buzz": "You may use old Buzz(>=0.7) adapter for your project"
},
"autoload": {
"psr-0": { "Github\\": "lib/" }
},
Expand Down
2 changes: 1 addition & 1 deletion doc/result_pager.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $client = new Github\Client();

$organizationApi = $client->api('organization');

$paginator = new Github\ResultPager( $client );
$paginator = new Github\ResultPager($client);
$parameters = array('github');
$result = $paginator->fetch($organizationApi, 'repositories', $parameters);
```
Expand Down
10 changes: 5 additions & 5 deletions lib/Github/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
if (null !== $this->perPage && !isset($parameters['per_page'])) {
$parameters['per_page'] = $this->perPage;
}
$response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);
$response = $this->client->executeRequest('GET', $path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -73,7 +73,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 @@ -83,7 +83,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 @@ -93,7 +93,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 @@ -103,7 +103,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
32 changes: 30 additions & 2 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Github\Api\ApiInterface;
use Github\Exception\InvalidArgumentException;
use Github\HttpClient\HttpClient;
use Github\HttpClient\Adapter\Guzzle\HttpClient;
use Github\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -161,7 +161,35 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
$password = null;
}

$this->getHttpClient()->authenticate($tokenOrLogin, $password, $authMethod);
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->getHttpClient()->authenticate($authMethod, $tokenOrLogin, $password);
}

public function executeRequest($method, $path, $parameters, $headers)
{
return $this->getHttpClient()->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,9 +1,10 @@
<?php

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

use Github\HttpClient\Cache\CacheInterface;
use Github\HttpClient\Cache\FilesystemCache;
use Github\HttpClient\Adapter\Buzz\Message\Response;

/**
* Performs requests on GitHub API using If-Modified-Since headers.
Expand Down Expand Up @@ -44,16 +45,16 @@ public function setCache(CacheInterface $cache)
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
{
$response = parent::request($path, $parameters, $httpMethod, $headers);
$buzzResponse = parent::request($path, $parameters, $httpMethod, $headers);

$key = trim($this->options['base_url'].$path, '/');
if (304 == $response->getStatusCode()) {
if (304 == $buzzResponse->getStatusCode()) {
return $this->getCache()->get($key);
}

$this->getCache()->set($key, $response);
$this->getCache()->set($key, new Response($buzzResponse));

return $response;
return $buzzResponse;
}

/**
Expand Down
Loading