Skip to content

Commit

Permalink
Merge pull request #454 from MaartenWaegeman/patch-2
Browse files Browse the repository at this point in the history
Updates to rate limits
  • Loading branch information
stephangroen authored Dec 28, 2020
2 parents b4cc4e4 + 3db453b commit 7dbca25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ $connection->getDailyLimitRemaining(); // Retrieve the remaining amount of API c
$connection->getDailyLimitReset(); // Retrieve the timestamp for when the limit will reset
$connection->getMinutelyLimit(); // Retrieve your limit per minute
$connection->getMinutelyLimitRemaining(); // Retrieve the amount of API calls remaining for this minute
$connection->getMinutelyLimitReset(); // Retrieve the timestamp for when the minutely limit will reset
```
_Do note when you have no more minutely calls available, Exact only sends the Minutely Limit headers. So in that case, the Daily Limit headers will remain 0 until the minutely reset rolls over._


### Use the library to do stuff (examples)

Expand Down
18 changes: 16 additions & 2 deletions src/Picqer/Financials/Exact/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class Connection
*/
protected $minutelyLimitRemaining;

/**
* @var int|null
*/
protected $minutelyLimitReset;

/**
* @return Client
*/
Expand Down Expand Up @@ -408,12 +413,12 @@ public function needsAuthentication()
private function parseResponse(Response $response, $returnSingleIfPossible = true)
{
try {
$this->extractRateLimits($response);

if ($response->getStatusCode() === 204) {
return [];
}

$this->extractRateLimits($response);

Psr7\rewind_body($response);
$json = json_decode($response->getBody()->getContents(), true);
if (false === is_array($json)) {
Expand Down Expand Up @@ -701,6 +706,14 @@ public function getMinutelyLimitRemaining()
return $this->minutelyLimitRemaining;
}

/**
* @return int|null The time at which the minutely rate limit window resets in UTC epoch milliseconds
*/
public function getMinutelyLimitReset()
{
return $this->minutelyLimitReset;
}

/**
* @return string
*/
Expand Down Expand Up @@ -768,5 +781,6 @@ private function extractRateLimits(Response $response)

$this->minutelyLimit = (int) $response->getHeaderLine('X-RateLimit-Minutely-Limit');
$this->minutelyLimitRemaining = (int) $response->getHeaderLine('X-RateLimit-Minutely-Remaining');
$this->minutelyLimitReset = (int) $response->getHeaderLine('X-RateLimit-Minutely-Reset');
}
}

0 comments on commit 7dbca25

Please sign in to comment.