-
-
Notifications
You must be signed in to change notification settings - Fork 594
Use If-modified-since headers to avoid X-Rate-Limit decrease #20
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace Github\HttpClient\Cache; | ||
|
|
||
| use Github\HttpClient\Message\Response; | ||
|
|
||
| /** | ||
| * Caches github api responses | ||
| * | ||
| * @author Florian Klein <florian.klein@free.fr> | ||
| */ | ||
| interface CacheInterface | ||
| { | ||
| /** | ||
| * @param string the id of the cached resource | ||
| * @return int the modified since timestamp | ||
| **/ | ||
| public function getModifiedSince($id); | ||
|
|
||
| /** | ||
| * @param string the id of the cached resource | ||
| * @return Response The cached response object | ||
| **/ | ||
| public function get($id); | ||
|
|
||
| /** | ||
| * @param string the id of the cached resource | ||
| * @param Response the response to cache | ||
| **/ | ||
| public function set($id, Response $response); | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| namespace Github\HttpClient\Cache; | ||
|
|
||
| use Github\HttpClient\Message\Response; | ||
|
|
||
| class FilesystemCache implements CacheInterface | ||
| { | ||
| protected $path; | ||
|
|
||
| public function __construct($path = null) | ||
| { | ||
| $this->path = $path ?: sys_get_temp_dir().DIRECTORY_SEPARATOR.'php-github-api-cache'; | ||
| } | ||
|
|
||
| public function get($id) | ||
| { | ||
| if (false !== $content = @file_get_contents($this->getPath($id))) { | ||
| return unserialize($content); | ||
| } | ||
|
|
||
| throw new \InvalidArgumentException(sprintf('File "%s" not found', $this->getPath($id))); | ||
| } | ||
|
|
||
| public function set($id, Response $response) | ||
| { | ||
| if (!is_dir($this->path)) { | ||
| mkdir($this->path, 0777, true); | ||
| } | ||
|
|
||
| if (false === $bytes = @file_put_contents($this->getPath($id), serialize($response))) { | ||
| throw new \InvalidArgumentException(sprintf('Cannot put content in file "%s"', $this->getPath($id))); | ||
| } | ||
| } | ||
|
|
||
| public function getModifiedSince($id) | ||
| { | ||
| if (file_exists($this->getPath($id))) { | ||
| return filemtime($this->getPath($id)); | ||
| } | ||
| } | ||
|
|
||
| protected function getPath($id) | ||
| { | ||
| return sprintf('%s%s%s', rtrim($this->path, DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR, md5($id)); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| namespace Github\HttpClient; | ||
|
|
||
| use Buzz\Client\ClientInterface; | ||
| use Buzz\Message\MessageInterface; | ||
| use Buzz\Message\RequestInterface; | ||
| use Buzz\Listener\ListenerInterface; | ||
|
|
||
| use Github\Exception\ErrorException; | ||
| use Github\Exception\RuntimeException; | ||
| use Github\HttpClient\Listener\ErrorListener; | ||
| use Github\HttpClient\Message\Request; | ||
| use Github\HttpClient\Message\Response; | ||
| use Github\HttpClient\Cache\CacheInterface; | ||
| use Github\HttpClient\Cache\FilesystemCache; | ||
|
|
||
| /** | ||
| * Performs requests on GitHub API using If-Modified-Since headers. | ||
| * Returns a cached version if not modified | ||
| * Avoids increasing the X-Rate-Limit, which is cool | ||
| * | ||
| * @author Florian Klein <florian.klein@free.fr> | ||
| */ | ||
| class CachedHttpClient extends HttpClient | ||
| { | ||
| protected $cache; | ||
|
|
||
| public function __construct(array $options = array(), ClientInterface $client = null, CacheInterface $cache = null) | ||
| { | ||
| parent::__construct($options, $client); | ||
|
|
||
| $this->cache = $cache ?: new FilesystemCache; | ||
| } | ||
|
|
||
| public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array(), Response $response = null) | ||
| { | ||
| $response = parent::request($path, $parameters, $httpMethod, $headers, $response); | ||
|
|
||
| $key = trim($this->options['base_url'].$path, '/'); | ||
| if ($response->isNotModified()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Such method not exist in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added it :) oops |
||
| return $this->cache->get($key); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will fail if cache was not found and response was not modified (i.e. some GC cleaned temp folder) |
||
| } | ||
|
|
||
| $this->cache->set($key, $response); | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| /** | ||
| * Create requests with If-Modified-Since headers | ||
| * @param string $httpMethod | ||
| * @param string $url | ||
| * | ||
| * @return Request | ||
| */ | ||
| protected function createRequest($httpMethod, $url) | ||
| { | ||
| $request = parent::createRequest($httpMethod, $url); | ||
| $modifiedSince = date('r', $this->cache->getModifiedSince($url)); | ||
| $request->addHeader(sprintf('If-Modified-Since: %s', $modifiedSince)); | ||
|
|
||
| return $request; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| 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. | ||
|
|
@@ -48,8 +49,12 @@ class HttpClient implements HttpClientInterface | |
| * @param array $options | ||
| * @param ClientInterface $client | ||
| */ | ||
| public function __construct(array $options, ClientInterface $client) | ||
| public function __construct(array $options = array(), ClientInterface $client = null) | ||
| { | ||
| $client = $client ?: new Curl(); | ||
| $client->setTimeout($this->options['timeout']); | ||
| $client->setVerifyPeer(false); | ||
|
|
||
| $this->options = array_merge($this->options, $options); | ||
| $this->client = $client; | ||
|
|
||
|
|
@@ -139,7 +144,7 @@ public function put($path, array $parameters = array(), array $headers = array() | |
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array()) | ||
| public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array(), Response $response = null) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason of this change ? I look at code and can't find in what case this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It permits more flexibility. We had no way access to the response in precedent code. It's used in tests currently :-s
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that similar method to |
||
| { | ||
| $path = trim($this->options['base_url'].$path, '/'); | ||
|
|
||
|
|
@@ -154,7 +159,9 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET', | |
| } | ||
| } | ||
|
|
||
| $response = new Response(); | ||
| if (null === $response) { | ||
| $response = new Response; | ||
| } | ||
|
|
||
| try { | ||
| $this->client->send($request, $response); | ||
|
|
@@ -198,7 +205,7 @@ public function getLastResponse() | |
| * | ||
| * @return Request | ||
| */ | ||
| private function createRequest($httpMethod, $url) | ||
| protected function createRequest($httpMethod, $url) | ||
| { | ||
| $request = new Request($httpMethod); | ||
| $request->setHeaders($this->headers); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
|
|
||
| namespace Github\Tests\HttpClient\Cache; | ||
|
|
||
| use Github\HttpClient\Message\Response; | ||
| use Github\HttpClient\Cache\FilesystemCache; | ||
|
|
||
| class FilesystemCacheTest extends \PHPUnit_Framework_TestCase | ||
| { | ||
| /** | ||
| * @test | ||
| */ | ||
| public function shouldStoreAResponseForAGivenKey() | ||
| { | ||
| $cache = new FilesystemCache('/tmp/github-api-test'); | ||
|
|
||
| $cache->set('test', new Response); | ||
|
|
||
| $this->assertNotNull($cache->get('test')); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function shouldGetATimestampForExistingFile() | ||
| { | ||
| $cache = new FilesystemCache('/tmp/github-api-test'); | ||
|
|
||
| $cache->set('test', new Response); | ||
|
|
||
| $this->assertInternalType('int', $cache->getModifiedSince('test')); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function shouldNotGetATimestampForInexistingFile() | ||
| { | ||
| $cache = new FilesystemCache('/tmp/github-api-test'); | ||
|
|
||
| $this->assertNull($cache->getModifiedSince('test2')); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is better :)