From d4a5cdee646377ae9024bf776a6feebfa23be4b8 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 19 Mar 2013 19:03:59 +0100 Subject: [PATCH 1/2] Fix ApiLimitExceedException details, fix for cache saving date --- lib/Github/Exception/ApiLimitExceedException.php | 2 +- lib/Github/HttpClient/CachedHttpClient.php | 10 ++++++++-- lib/Github/HttpClient/Message/Response.php | 15 ++------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/Github/Exception/ApiLimitExceedException.php b/lib/Github/Exception/ApiLimitExceedException.php index a9b5454f714..851631cccf8 100644 --- a/lib/Github/Exception/ApiLimitExceedException.php +++ b/lib/Github/Exception/ApiLimitExceedException.php @@ -9,7 +9,7 @@ */ class ApiLimitExceedException extends RuntimeException { - public function __construct($limit, $code = 0, $previous = null) + public function __construct($limit = 5000, $code = 0, $previous = null) { parent::__construct('You have reached GitHub hour limit! Actual limit is: '. $limit, $code, $previous); } diff --git a/lib/Github/HttpClient/CachedHttpClient.php b/lib/Github/HttpClient/CachedHttpClient.php index 16457bd0683..8f6a141a82b 100644 --- a/lib/Github/HttpClient/CachedHttpClient.php +++ b/lib/Github/HttpClient/CachedHttpClient.php @@ -47,7 +47,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET', $response = parent::request($path, $parameters, $httpMethod, $headers); $key = trim($this->options['base_url'].$path, '/'); - if ($response->isNotModified()) { + if (304 == $response->getStatusCode()) { return $this->getCache()->get($key); } @@ -64,7 +64,13 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET', protected function createRequest($httpMethod, $url) { $request = parent::createRequest($httpMethod, $url); - $request->addHeader(sprintf('If-Modified-Since: %s', date('r', $this->getCache()->getModifiedSince($url)))); + + if ($modifiedAt = $this->getCache()->getModifiedSince($url)) { + $modifiedAt = new \DateTime('@'.$modifiedAt); + $modifiedAt->setTimezone(new \DateTimeZone('GMT')); + + $request->addHeader(sprintf('If-Modified-Since: %s', $modifiedAt->format(DATE_RFC2822))); + } return $request; } diff --git a/lib/Github/HttpClient/Message/Response.php b/lib/Github/HttpClient/Message/Response.php index 35f2ada5264..c039ac4bae6 100644 --- a/lib/Github/HttpClient/Message/Response.php +++ b/lib/Github/HttpClient/Message/Response.php @@ -3,7 +3,6 @@ namespace Github\HttpClient\Message; use Buzz\Message\Response as BaseResponse; - use Github\Exception\ApiLimitExceedException; class Response extends BaseResponse @@ -55,23 +54,13 @@ public function getPagination() */ public function getApiLimit() { - $header = $this->getHeaderAttributes('X-RateLimit-Remaining'); + $header = $this->getHeader('X-RateLimit-Remaining'); if (!empty($header)) { $this->remainingCalls = $header; } if (null !== $this->remainingCalls && 1 > $this->remainingCalls) { - throw new ApiLimitExceedException($this->options['api_limit']); + throw new ApiLimitExceedException($this->getHeader('X-RateLimit-Limit')); } } - - /** - * Is not modified - * - * @return Boolean - */ - public function isNotModified() - { - return 304 === $this->getStatusCode(); - } } From fcee7e2e65523019a67e173d63763b5a566e7b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gildas=20Qu=C3=A9m=C3=A9ner?= Date: Wed, 20 Mar 2013 10:57:05 +0100 Subject: [PATCH 2/2] Hard-code sent timezone in If-Modified-Since header If-Modified-Since format must be RFC850 compliant and in GMT timezone BUT PHP DATE_RFC850 format keep returning UTC timezone. So, we set the GMT timezone and force its rendering in the header. --- lib/Github/HttpClient/CachedHttpClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Github/HttpClient/CachedHttpClient.php b/lib/Github/HttpClient/CachedHttpClient.php index 8f6a141a82b..f28f54bd9e2 100644 --- a/lib/Github/HttpClient/CachedHttpClient.php +++ b/lib/Github/HttpClient/CachedHttpClient.php @@ -67,9 +67,9 @@ protected function createRequest($httpMethod, $url) if ($modifiedAt = $this->getCache()->getModifiedSince($url)) { $modifiedAt = new \DateTime('@'.$modifiedAt); - $modifiedAt->setTimezone(new \DateTimeZone('GMT')); + $modifiedAt->setTimezone(new \DateTimeZone('GMT')); - $request->addHeader(sprintf('If-Modified-Since: %s', $modifiedAt->format(DATE_RFC2822))); + $request->addHeader(sprintf('If-Modified-Since: %s GMT', $modifiedAt->format('l, d-M-y H:i:s'))); } return $request;