Skip to content

Commit

Permalink
Merge pull request #415 from SynergiTech/x-rate-limit-problem
Browse files Browse the repository at this point in the history
Expose x-rate-limit-problem header
  • Loading branch information
calcinai committed Nov 20, 2018
2 parents bcedcbb + 52a799a commit 39b56e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/XeroPHP/Remote/Exception/RateLimitExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ class RateLimitExceededException extends Exception
protected $message = 'The API rate limit for your organisation/application pairing has been exceeded.';

protected $code = Response::STATUS_RATE_LIMIT_EXCEEDED;

protected $rateLimitProblem = null;

public function setRateLimitProblem($rateLimitProblem)
{
$this->rateLimitProblem = strtolower($rateLimitProblem);
}

public function getRateLimitProblem()
{
return $this->rateLimitProblem;
}
}
20 changes: 19 additions & 1 deletion src/XeroPHP/Remote/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,32 @@ public function send()
curl_setopt($ch, CURLOPT_POST, true);
}

$headers = [];
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$headers) {
$len = strlen($header);
if (strpos($header, ':') === false) {
return $len;
}

list($name, $value) = explode(':', $header, 2);
$name = strtolower(trim($name));
$value = trim($value);
if (!array_key_exists($name, $headers)) {
$headers[$name] = [];
}
$headers[$name][] = $value;

return $len;
});

$response = curl_exec($ch);
$info = curl_getinfo($ch);

if ($response === false) {
throw new Exception('Curl error: ' . curl_error($ch));
}

$this->response = new Response($this, $response, $info);
$this->response = new Response($this, $response, $info, $headers);
$this->response->parse();

return $this->response;
Expand Down
10 changes: 7 additions & 3 deletions src/XeroPHP/Remote/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ class Response

private $root_error;

public function __construct(Request $request, $response_body, array $curl_info)
public function __construct(Request $request, $response_body, array $curl_info, $headers)
{
$this->request = $request;
$this->response_body = $response_body;
$this->status = $curl_info['http_code'];
$this->headers = $headers;

list($this->content_type) = explode(';', $curl_info['content_type']);
}
Expand Down Expand Up @@ -104,7 +105,10 @@ public function parse()
if (false !== stripos($response, 'Organisation is offline')) {
throw new OrganisationOfflineException();
} elseif (false !== stripos($response, 'Rate limit exceeded')) {
throw new RateLimitExceededException();
$problem = isset($this->headers['x-rate-limit-problem']) ? current($this->headers['x-rate-limit-problem']) : null;
$exception = new RateLimitExceededException();
$exception->setRateLimitProblem($problem);
throw $exception;
} else {
throw new NotAvailableException();
}
Expand Down Expand Up @@ -137,7 +141,7 @@ public function getStatus()
{
return $this->status;
}

public function getResponseBody()
{
return $this->response_body;
Expand Down

0 comments on commit 39b56e0

Please sign in to comment.