Skip to content

Commit

Permalink
Handle if the limit header is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
pactode committed Jun 12, 2018
1 parent 1aeb9fa commit da030f0
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Providers/DefaultRateLimitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,28 @@ public function getRequestAllowance(RequestInterface $request)
*/
public function setRequestAllowance(ResponseInterface $response)
{
$callLimitHeader = collect($response->getHeader('HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT'))->first();
[$callsMade, $callsLimit] = explode('/', $callLimitHeader);
Cache::forever($this->prefix . 'request_allowance', $this->calculator->calculate($callsMade, $callsLimit));
Cache::forever($this->prefix . 'request_allowance', $this->calculateAllowanceFrom($response));
}

/**
* Calculate the request allowance from the response.
*
* @param ResponseInterface $response
* @return float
*/
protected function calculateAllowanceFrom(ResponseInterface $response)
{
$callLimitHeader = collect(
$response->getHeader('HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT')
)->first();

if ($callLimitHeader) {
[$callsMade, $callsLimit] = explode('/', $callLimitHeader);
return $this->calculator->calculate($callsMade, $callsLimit);
}

return floatval(
config('shopify.rate_limit.processes') * config('shopify.rate_limit.cycle')
);
}
}

0 comments on commit da030f0

Please sign in to comment.