Skip to content

Commit 146ae66

Browse files
committed
Improved handling of api exceptions and ratelimit failures
1 parent 7da1c3e commit 146ae66

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

lib/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use HaloApi\Exception\BadMethodCallException;
77
use HaloApi\Exception\InvalidArgumentException;
88
use HaloApi\HttpClient\Builder;
9+
use HaloApi\HttpClient\Plugin\ExceptionThrowerPlugin;
910
use Http\Client\Common\HttpMethodsClient;
1011
use Http\Client\Common\Plugin;
1112
use Http\Client\HttpClient;
@@ -26,6 +27,7 @@ public function __construct($apiKey, Builder $httpClientBuilder = null)
2627
{
2728
$this->httpClientBuilder = $builder = $httpClientBuilder ?: new Builder();
2829

30+
$builder->addPlugin(new ExceptionThrowerPlugin());
2931
$builder->addPlugin(new Plugin\RedirectPlugin());
3032
$builder->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri('https://www.haloapi.com')));
3133

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace HaloApi\Exception;
4+
5+
class RateLimitExceededException extends RuntimeException
6+
{
7+
/**
8+
* Retry after xx seconds
9+
*
10+
* @var int
11+
*/
12+
private $reset;
13+
14+
public function __construct(int $reset = 10, int $code = 0, $previous = null)
15+
{
16+
$this->reset = (int)$reset;
17+
18+
parent::__construct(sprintf('You have reached the api rate limit! Try again after %ds', $reset), $code, $previous);
19+
}
20+
21+
public function getResetTime(): int
22+
{
23+
return $this->reset;
24+
}
25+
}

lib/Exception/RuntimeException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace HaloApi\Exception;
4+
5+
class RuntimeException extends \RuntimeException implements ExceptionInterface
6+
{
7+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace HaloApi\HttpClient\Plugin;
4+
5+
use HaloApi\Exception\RateLimitExceededException;
6+
use HaloApi\Exception\RuntimeException;
7+
use HaloApi\HttpClient\Message\ResponseMediator;
8+
use Http\Client\Common\Plugin;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
class ExceptionThrowerPlugin implements Plugin
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
18+
{
19+
return $next($request)->then(function (ResponseInterface $response) use ($request) {
20+
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {
21+
return $response;
22+
}
23+
24+
$content = ResponseMediator::getContent($response);
25+
26+
if (429 === $response->getStatusCode()) {
27+
$retryAfter = ResponseMediator::getHeader($response, 'Retry-After');
28+
throw new RateLimitExceededException($retryAfter, $response->getStatusCode());
29+
}
30+
31+
throw new RuntimeException(sprintf('Error calling the api, response code "%s".', $response->getStatusCode()));
32+
});
33+
}
34+
}

0 commit comments

Comments
 (0)