Skip to content

Commit 340ad2f

Browse files
committed
Merge pull request #38 from KnpLabs/bugfix/api_limit
Bugfix/api limit
2 parents e9a3e2d + fcee7e2 commit 340ad2f

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

lib/Github/Exception/ApiLimitExceedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class ApiLimitExceedException extends RuntimeException
1111
{
12-
public function __construct($limit, $code = 0, $previous = null)
12+
public function __construct($limit = 5000, $code = 0, $previous = null)
1313
{
1414
parent::__construct('You have reached GitHub hour limit! Actual limit is: '. $limit, $code, $previous);
1515
}

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
4747
$response = parent::request($path, $parameters, $httpMethod, $headers);
4848

4949
$key = trim($this->options['base_url'].$path, '/');
50-
if ($response->isNotModified()) {
50+
if (304 == $response->getStatusCode()) {
5151
return $this->getCache()->get($key);
5252
}
5353

@@ -64,7 +64,13 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
6464
protected function createRequest($httpMethod, $url)
6565
{
6666
$request = parent::createRequest($httpMethod, $url);
67-
$request->addHeader(sprintf('If-Modified-Since: %s', date('r', $this->getCache()->getModifiedSince($url))));
67+
68+
if ($modifiedAt = $this->getCache()->getModifiedSince($url)) {
69+
$modifiedAt = new \DateTime('@'.$modifiedAt);
70+
$modifiedAt->setTimezone(new \DateTimeZone('GMT'));
71+
72+
$request->addHeader(sprintf('If-Modified-Since: %s GMT', $modifiedAt->format('l, d-M-y H:i:s')));
73+
}
6874

6975
return $request;
7076
}

lib/Github/HttpClient/Message/Response.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Github\HttpClient\Message;
44

55
use Buzz\Message\Response as BaseResponse;
6-
76
use Github\Exception\ApiLimitExceedException;
87

98
class Response extends BaseResponse
@@ -55,23 +54,13 @@ public function getPagination()
5554
*/
5655
public function getApiLimit()
5756
{
58-
$header = $this->getHeaderAttributes('X-RateLimit-Remaining');
57+
$header = $this->getHeader('X-RateLimit-Remaining');
5958
if (!empty($header)) {
6059
$this->remainingCalls = $header;
6160
}
6261

6362
if (null !== $this->remainingCalls && 1 > $this->remainingCalls) {
64-
throw new ApiLimitExceedException($this->options['api_limit']);
63+
throw new ApiLimitExceedException($this->getHeader('X-RateLimit-Limit'));
6564
}
6665
}
67-
68-
/**
69-
* Is not modified
70-
*
71-
* @return Boolean
72-
*/
73-
public function isNotModified()
74-
{
75-
return 304 === $this->getStatusCode();
76-
}
7766
}

0 commit comments

Comments
 (0)