Skip to content

Bugfix/api limit #38

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 2 commits into from
Mar 20, 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 lib/Github/Exception/ApiLimitExceedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/Github/HttpClient/CachedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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 GMT', $modifiedAt->format('l, d-M-y H:i:s')));
}

return $request;
}
Expand Down
15 changes: 2 additions & 13 deletions lib/Github/HttpClient/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Github\HttpClient\Message;

use Buzz\Message\Response as BaseResponse;

use Github\Exception\ApiLimitExceedException;

class Response extends BaseResponse
Expand Down Expand Up @@ -55,23 +54,13 @@ public function getPagination()
*/
public function getApiLimit()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, this method is used nowhere

{
$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();
}
}