Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit a431764

Browse files
committed
WIP: Handle 429
1 parent cab9495 commit a431764

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Discord/RegisterClient.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Discord\Slash\Parts\Command;
1616
use Discord\Slash\Parts\Part;
1717
use GuzzleHttp\Client as GuzzleClient;
18+
use GuzzleHttp\Exception\RequestException;
1819
use ReflectionClass;
1920
use Symfony\Component\OptionsResolver\OptionsResolver;
2021

@@ -34,6 +35,13 @@ class RegisterClient
3435
*/
3536
private $application;
3637

38+
/**
39+
* HTTP client.
40+
*
41+
* @var GuzzleClient
42+
*/
43+
private $http;
44+
3745
/**
3846
* HTTP client constructor.
3947
*
@@ -282,7 +290,18 @@ private function request(string $method, string $endpoint, ?array $content = nul
282290
$options['json'] = $content;
283291
}
284292

285-
$response = $this->http->request($method, $endpoint, $options);
293+
try {
294+
$response = $this->http->request($method, $endpoint, $options);
295+
} catch (RequestException $e) {
296+
switch ($e->getResponse()->getStatusCode()) {
297+
case 429:
298+
$resetAfter = (float) $e->getResponse()->getheaderLine('X-RateLimit-Reset-After');
299+
usleep($resetAfter * 1000);
300+
return $this->request($method, $endpoint, $content);
301+
default:
302+
throw $e;
303+
}
304+
}
286305

287306
return json_decode($response->getBody(), true);
288307
}

0 commit comments

Comments
 (0)