Skip to content

Commit 791ea37

Browse files
authored
Merge pull request #82 from andyexeter/resource-rate-limit-fix-pr
[Fixes #56, #81] Fix new resource rate limit issue
2 parents 41916ac + 6ab5656 commit 791ea37

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

lib/CurlRequest.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
use PHPShopify\Exception\CurlException;
12+
use PHPShopify\Exception\ResourceRateLimitException;
1213

1314
/*
1415
|--------------------------------------------------------------------------
@@ -47,6 +48,7 @@ protected static function init($url, $httpHeaders = array())
4748
//Return the transfer as a string
4849
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
4950

51+
curl_setopt($ch, CURLOPT_HEADER, true);
5052
curl_setopt($ch, CURLOPT_USERAGENT, 'PHPClassic/PHPShopify');
5153

5254
$headers = array();
@@ -143,23 +145,32 @@ public static function delete($url, $httpHeaders = array())
143145
protected static function processRequest($ch)
144146
{
145147
# Check for 429 leaky bucket error
146-
while(1) {
147-
$output = curl_exec($ch);
148-
self::$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
149-
if(self::$lastHttpCode != 429) {
150-
break;
151-
}
152-
usleep(500000);
148+
while (1) {
149+
$output = curl_exec($ch);
150+
$response = new CurlResponse($output);
151+
152+
self::$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
153+
if (self::$lastHttpCode != 429) {
154+
break;
155+
}
156+
157+
$limitHeader = explode('/', $response->getHeader('X-Shopify-Shop-Api-Call-Limit'), 2);
158+
159+
if (isset($limitHeader[1]) && $limitHeader[0] < $limitHeader[1]) {
160+
throw new ResourceRateLimitException($response->getBody());
161+
}
162+
163+
usleep(500000);
153164
}
154-
165+
155166
if (curl_errno($ch)) {
156167
throw new Exception\CurlException(curl_errno($ch) . ' : ' . curl_error($ch));
157168
}
158169

159170
// close curl resource to free up system resources
160171
curl_close($ch);
161172

162-
return $output;
173+
return $response->getBody();
163174
}
164175

165176
}

lib/CurlResponse.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace PHPShopify;
4+
5+
class CurlResponse
6+
{
7+
/** @var array */
8+
private $headers = [];
9+
/** @var string */
10+
private $body;
11+
12+
public function __construct($response)
13+
{
14+
$this->parse($response);
15+
}
16+
17+
/**
18+
* @param string $response
19+
*/
20+
private function parse($response)
21+
{
22+
$response = \explode("\r\n\r\n", $response);
23+
if (\count($response) > 1) {
24+
// We want the last two parts
25+
$response = \array_slice($response, -2, 2);
26+
list($headers, $body) = $response;
27+
foreach (\explode("\r\n", $headers) as $header) {
28+
$pair = \explode(': ', $header, 2);
29+
if (isset($pair[1])) {
30+
$this->headers[$pair[0]] = $pair[1];
31+
}
32+
}
33+
} else {
34+
$body = $response[0];
35+
}
36+
37+
$this->body = $body;
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public function getHeaders()
44+
{
45+
return $this->headers;
46+
}
47+
48+
/**
49+
* @param string $key
50+
*
51+
* @return string
52+
*/
53+
public function getHeader($key)
54+
{
55+
return isset($this->headers[$key]) ? $this->headers[$key] : null;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getBody()
62+
{
63+
return $this->body;
64+
}
65+
66+
public function __toString()
67+
{
68+
$body = $this->getBody();
69+
$body = $body ? : '';
70+
71+
return $body;
72+
}
73+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PHPShopify\Exception;
4+
5+
class ResourceRateLimitException extends ApiException
6+
{
7+
8+
}

0 commit comments

Comments
 (0)