Skip to content

Commit

Permalink
feature #1072 API rate limit error status can be 403 (matthewnessworthy)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4.x-dev branch.

Discussion
----------

Fixes #1070

Commits
-------

55e881a API rate limit error status can be 403
  • Loading branch information
acrobat authored Jun 26, 2022
2 parents 83b8a32 + 55e881a commit d90368c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/Github/HttpClient/Plugin/GithubExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
throw new SsoRequiredException($url);
}

$remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining');
if ((403 === $response->getStatusCode()) && null !== $remaining && 1 > $remaining && isset($content['message']) && (0 === strpos($content['message'], 'API rate limit exceeded'))) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');

throw new ApiLimitExceedException($limit, $reset);
}

$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
if ((403 === $response->getStatusCode()) && 0 < $reset && isset($content['message']) && (0 === strpos($content['message'], 'You have exceeded a secondary rate limit.'))) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');

throw new ApiLimitExceedException($limit, $reset);
}

throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
});
}
Expand Down
34 changes: 34 additions & 0 deletions test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,40 @@ public static function responseProvider()
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Rate Limit Exceeded via 403 status' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-RateLimit-Remaining' => 0,
'X-RateLimit-Limit' => 5000,
'X-RateLimit-Reset' => 1609245810,
],
json_encode(
[
'message' => 'API rate limit exceeded for installation ID xxxxxxx.',
]
)
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Secondary Rate Limit Exceeded via 403 status' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-RateLimit-Remaining' => 100,
'X-RateLimit-Limit' => 5000,
'X-RateLimit-Reset' => 1609245810,
],
json_encode(
[
'message' => 'You have exceeded a secondary rate limit. Please wait a few minutes before you try again.',
]
)
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Two Factor Authentication Required' => [
'response' => new Response(
401,
Expand Down

0 comments on commit d90368c

Please sign in to comment.