Skip to content

[1.2] Migrate from Buzz to Guzzle #81

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 3 commits into from
Nov 2, 2013
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
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Uses [GitHub API v3](http://developer.github.com/v3/). The object API is very si
## Requirements

* PHP >= 5.3.2 with [cURL](http://php.net/manual/en/book.curl.php) extension,
* [Buzz](https://github.com/kriswallsmith/Buzz) library,
* [Guzzle](https://github.com/guzzle/guzzle) library,
* (optional) PHPUnit to run tests.

## Autoload
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
}
],
"require": {
"php": ">=5.3.2",
"ext-curl": "*",
"kriswallsmith/buzz": ">=0.7"
"php": ">=5.3.2",
"ext-curl": "*",
"guzzle/guzzle": ">=3.7"
},
"require-dev": {
"phpunit/phpunit": ">=3.7"
},
"autoload": {
"psr-0": { "Github\\": "lib/" }
},
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
"dev-master": "1.3.x-dev"
}
}
}
11 changes: 6 additions & 5 deletions lib/Github/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Github\Api;

use Github\Client;
use Github\HttpClient\Message\ResponseMediator;

/**
* Abstract class for Api classes
Expand Down Expand Up @@ -65,7 +66,7 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
}
$response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);

return $response->getContent();
return ResponseMediator::getContent($response);
}

/**
Expand All @@ -75,7 +76,7 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
{
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);

return $response->getContent();
return ResponseMediator::getContent($response);
}

/**
Expand All @@ -85,7 +86,7 @@ protected function patch($path, array $parameters = array(), $requestHeaders = a
{
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);

return $response->getContent();
return ResponseMediator::getContent($response);
}

/**
Expand All @@ -95,7 +96,7 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
{
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);

return $response->getContent();
return ResponseMediator::getContent($response);
}

/**
Expand All @@ -105,6 +106,6 @@ protected function delete($path, array $parameters = array(), $requestHeaders =
{
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);

return $response->getContent();
return ResponseMediator::getContent($response);
}
}
7 changes: 7 additions & 0 deletions lib/Github/Api/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace Github\Api;

use Github\Client;

/**
* Api interface
*
* @author Joseph Bielawski <[email protected]>
*/
interface ApiInterface
{
public function __construct(Client $client);

public function getPerPage();

public function setPerPage($perPage);
}
2 changes: 1 addition & 1 deletion lib/Github/HttpClient/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Github\HttpClient\Cache;

use Github\HttpClient\Message\Response;
use Guzzle\Http\Message\Response;

/**
* Caches github api responses
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/HttpClient/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Github\HttpClient\Cache;

use Github\HttpClient\Message\Response;
use Guzzle\Http\Message\Response;

class FilesystemCache implements CacheInterface
{
Expand Down
11 changes: 7 additions & 4 deletions lib/Github/HttpClient/CachedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
*
* {@inheritdoc}
*/
protected function createRequest($httpMethod, $url)
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
{
$request = parent::createRequest($httpMethod, $url);
$request = parent::createRequest($httpMethod, $path, $requestBody, $headers = array());

if ($modifiedAt = $this->getCache()->getModifiedSince($url)) {
if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {
$modifiedAt = new \DateTime('@'.$modifiedAt);
$modifiedAt->setTimezone(new \DateTimeZone('GMT'));

$request->addHeader(sprintf('If-Modified-Since: %s GMT', $modifiedAt->format('l, d-M-y H:i:s')));
$request->addHeader(
'If-Modified-Since',
sprintf('%s GMT', $modifiedAt->format('l, d-M-y H:i:s'))
);
}

return $request;
Expand Down
117 changes: 28 additions & 89 deletions lib/Github/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Github\HttpClient;

use Buzz\Client\ClientInterface;
use Buzz\Listener\ListenerInterface;
use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Http\ClientInterface;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;

use Github\Exception\ErrorException;
use Github\Exception\RuntimeException;
use Github\HttpClient\Listener\AuthListener;
use Github\HttpClient\Listener\ErrorListener;
use Github\HttpClient\Message\Request;
use Github\HttpClient\Message\Response;
use Buzz\Client\Curl;

/**
* Performs requests on GitHub API. API documentation should be self-explanatory.
Expand All @@ -20,27 +19,18 @@
*/
class HttpClient implements HttpClientInterface
{
/**
* @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_limit' => 5000,
'api_version' => 'beta',
'api_version' => 'v3',

'cache_dir' => null
);
/**
* @var array
*/
protected $listeners = array();
/**
* @var array
*/

protected $headers = array();

private $lastResponse;
Expand All @@ -52,32 +42,14 @@ class HttpClient implements HttpClientInterface
*/
public function __construct(array $options = array(), ClientInterface $client = null)
{
$client = $client ?: new Curl();
$timeout = isset($options['timeout']) ? $options['timeout'] : $this->options['timeout'];
$client->setTimeout($timeout);
$client->setVerifyPeer(false);

$this->options = array_merge($this->options, $options);
$client = $client ?: new GuzzleClient($options['base_url'], $this->options);
$this->client = $client;

$this->addListener(new ErrorListener($this->options));

$this->addListener('request.error', array(new ErrorListener($this->options), 'onRequestError'));
$this->clearHeaders();
}

public function authenticate($tokenOrLogin, $password, $authMethod)
{
$this->addListener(
new AuthListener(
$authMethod,
array(
'tokenOrLogin' => $tokenOrLogin,
'password' => $password
)
)
);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -105,24 +77,17 @@ public function clearHeaders()
);
}

/**
* @param ListenerInterface $listener
*/
public function addListener(ListenerInterface $listener)
public function addListener($eventName, $listener)
{
$this->listeners[get_class($listener)] = $listener;
$this->client->getEventDispatcher()->addListener($eventName, $listener);
}

/**
* {@inheritDoc}
*/
public function get($path, array $parameters = array(), array $headers = array())
{
if (0 < count($parameters)) {
$path .= (false === strpos($path, '?') ? '?' : '&').http_build_query($parameters, '', '&');
}

return $this->request($path, array(), 'GET', $headers);
return $this->request($path, $parameters, 'GET', $headers);
}

/**
Expand Down Expand Up @@ -162,27 +127,15 @@ public function put($path, array $parameters = array(), array $headers = array()
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
{
if (!empty($this->options['base_url']) && 0 !== strpos($path, $this->options['base_url'])) {
$path = trim($this->options['base_url'].$path, '/');
}
$requestBody = count($parameters) === 0
? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0)
;

$request = $this->createRequest($httpMethod, $path);
$request = $this->createRequest($httpMethod, $path, $requestBody, $headers);
$request->addHeaders($headers);
if (count($parameters) > 0) {
$request->setContent(json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0));
}

$hasListeners = 0 < count($this->listeners);
if ($hasListeners) {
foreach ($this->listeners as $listener) {
$listener->preSend($request);
}
}

$response = $this->createResponse();

try {
$this->client->send($request, $response);
$response = $this->client->send($request);
} catch (\LogicException $e) {
throw new ErrorException($e->getMessage());
} catch (\RuntimeException $e) {
Expand All @@ -192,15 +145,19 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
$this->lastRequest = $request;
$this->lastResponse = $response;

if ($hasListeners) {
foreach ($this->listeners as $listener) {
$listener->postSend($request, $response);
}
}

return $response;
}

/**
* {@inheritDoc}
*/
public function authenticate($tokenOrLogin, $password = null, $method)
{
$this->addListener('request.before_send', array(
new AuthListener($tokenOrLogin, $password, $method), 'onRequestBeforeSend'
));
}

/**
* @return Request
*/
Expand All @@ -217,26 +174,8 @@ public function getLastResponse()
return $this->lastResponse;
}

/**
* @param string $httpMethod
* @param string $url
*
* @return Request
*/
protected function createRequest($httpMethod, $url)
{
$request = new Request($httpMethod);
$request->setHeaders($this->headers);
$request->fromUrl($url);

return $request;
}

/**
* @return Response
*/
protected function createResponse()
protected function createRequest($httpMethod, $path, $requestBody, array $headers = array())
{
return new Response();
return $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $requestBody);
}
}
Loading