Skip to content

Commit 8ea31d0

Browse files
committed
ShopifySDK maintains maximum 2 calls per second to the API
1 parent 773f276 commit 8ea31d0

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

lib/HttpRequestJson.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public static function get($url, $httpHeaders = array())
6868
{
6969
self::prepareRequest($httpHeaders);
7070

71+
ShopifySDK::checkApiCallLimit();
7172
$response = CurlRequest::get($url, self::$httpHeaders);
7273

7374
return self::processResponse($response);
@@ -86,6 +87,7 @@ public static function post($url, $dataArray, $httpHeaders = array())
8687
{
8788
self::prepareRequest($httpHeaders, $dataArray);
8889

90+
ShopifySDK::checkApiCallLimit();
8991
$response = CurlRequest::post($url, self::$postDataJSON, self::$httpHeaders);
9092

9193
return self::processResponse($response);
@@ -104,6 +106,7 @@ public static function put($url, $dataArray, $httpHeaders = array())
104106
{
105107
self::prepareRequest($httpHeaders, $dataArray);
106108

109+
ShopifySDK::checkApiCallLimit();
107110
$response = CurlRequest::put($url, self::$postDataJSON, self::$httpHeaders);
108111

109112
return self::processResponse($response);
@@ -121,6 +124,7 @@ public static function delete($url, $httpHeaders = array())
121124
{
122125
self::prepareRequest($httpHeaders);
123126

127+
ShopifySDK::checkApiCallLimit();
124128
$response = CurlRequest::delete($url, self::$httpHeaders);
125129

126130
return self::processResponse($response);

lib/ShopifySDK.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565

6666
class ShopifySDK
6767
{
68+
/**
69+
* @var float microtime of last api call
70+
*/
71+
public static $microtimeOfLastAPICall;
72+
6873
/**
6974
* Shop / API configurations
7075
*
@@ -247,4 +252,30 @@ public static function setAdminUrl()
247252
public static function getAdminUrl() {
248253
return self::$config['AdminUrl'];
249254
}
255+
256+
/**
257+
* Maintain maximum 2 calls per second to the API
258+
*
259+
* @param bool $firstCallWait Whether to maintain the wait time even if it is the first API call
260+
*/
261+
public static function checkApiCallLimit($firstCallWait = false)
262+
{
263+
if (static::$microtimeOfLastAPICall == null) {
264+
if ($firstCallWait) {
265+
usleep(500000);
266+
}
267+
} else {
268+
$now = microtime(true);
269+
$timeSinceLastCall = $now - static::$microtimeOfLastAPICall;
270+
//Ensure 2 API calls per second
271+
if($timeSinceLastCall < .5) {
272+
$timeToWait = .5 - $timeSinceLastCall;
273+
//convert time to microseconds
274+
$microSecondsToWait = $timeToWait * 1000000;
275+
//Wait to maintain the API call difference of .5 seconds
276+
usleep($microSecondsToWait);
277+
}
278+
}
279+
static::$microtimeOfLastAPICall = microtime(true);
280+
}
250281
}

tests/TestResource.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ class TestResource extends \PHPUnit_Framework_TestCase
1414
*/
1515
public static $shopify;
1616

17-
/**
18-
* @var float microtime of last api call
19-
*/
20-
public static $microtimeOfLastAPICall;
21-
2217
/**
2318
* @inheritDoc
2419
*/
@@ -31,6 +26,7 @@ public static function setUpBeforeClass()
3126
);
3227

3328
self::$shopify = ShopifySDK::config($config);
29+
ShopifySDK::checkApiCallLimit();
3430
}
3531

3632
/**
@@ -40,27 +36,4 @@ public static function tearDownAfterClass()
4036
{
4137
self::$shopify = null;
4238
}
43-
44-
/**
45-
* @inheritDoc
46-
*/
47-
public function setUp()
48-
{
49-
if(static::$microtimeOfLastAPICall == null) {
50-
//Maintain 2 seconds per call
51-
usleep(500000);
52-
} else {
53-
$now = microtime(true);
54-
$timeSinceLastCall = $now - static::$microtimeOfLastAPICall;
55-
//Ensure 2 API calls per second
56-
if($timeSinceLastCall < .5) {
57-
$timeToWait = .5 - $timeSinceLastCall;
58-
//convert time to microseconds
59-
$microSecondsToWait = $timeToWait * 1000000;
60-
//Wait to maintain the API call difference of .5 seconds
61-
usleep($microSecondsToWait);
62-
}
63-
}
64-
static::$microtimeOfLastAPICall = microtime(true);
65-
}
6639
}

0 commit comments

Comments
 (0)