-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve rate limit handling in the SDK
- Loading branch information
Showing
3 changed files
with
60 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace ShoppinPal\Vend\Exception; | ||
|
||
use DateTime; | ||
use YapepBase\Exception\Exception; | ||
|
||
/** | ||
* Thrown if the request is denied because of rate limiting | ||
*/ | ||
class RateLimitingException extends CommunicationException | ||
{ | ||
|
||
/** | ||
* @return DateTime | ||
* @throws Exception | ||
*/ | ||
public function getRetryAfter() | ||
{ | ||
$responseBody = $this->curlResult->getResponseBody(); | ||
$decodedBody = json_decode($responseBody, true); | ||
|
||
if (empty($decodedBody)) { | ||
throw new Exception('Unable to decode response body: ' . $responseBody); | ||
} | ||
|
||
if (!isset($decodedBody['retry-after'])) { | ||
throw new Exception('No retry-after in response body: ' . $responseBody); | ||
} | ||
|
||
$retryAfter = DateTime::createFromFormat(DateTime::RFC822, $decodedBody['retry-after']); | ||
|
||
if (empty($retryAfter)) { | ||
throw new Exception('Unable to parse date from retry-after string: ' . $decodedBody['retry-after']); | ||
} | ||
|
||
return $retryAfter; | ||
} | ||
} |