Skip to content

Commit 4fb08af

Browse files
committed
Testing - 1 Second wait time per call since 2 builds run at the same time.
1 parent 8ea31d0 commit 4fb08af

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/ShopifySDK.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ class ShopifySDK
6868
/**
6969
* @var float microtime of last api call
7070
*/
71-
public static $microtimeOfLastAPICall;
71+
public static $microtimeOfLastApiCall;
72+
73+
/**
74+
* @var float Minimum gap in seconds to maintain between 2 api calls
75+
*/
76+
public static $timeAllowedForEachApiCall = .5;
7277

7378
/**
7479
* Shop / API configurations
@@ -216,6 +221,10 @@ public static function config($config)
216221
self::setAdminUrl();
217222
}
218223

224+
if (isset($config['AllowedTimePerCall'])) {
225+
static::$timeAllowedForEachApiCall = $config['AllowedTimePerCall'];
226+
}
227+
219228
return new ShopifySDK;
220229
}
221230

@@ -256,26 +265,33 @@ public static function getAdminUrl() {
256265
/**
257266
* Maintain maximum 2 calls per second to the API
258267
*
268+
* @see https://help.shopify.com/api/guides/api-call-limit
269+
*
259270
* @param bool $firstCallWait Whether to maintain the wait time even if it is the first API call
260271
*/
261272
public static function checkApiCallLimit($firstCallWait = false)
262273
{
263-
if (static::$microtimeOfLastAPICall == null) {
274+
$timeToWait = 0;
275+
if (static::$microtimeOfLastApiCall == null) {
264276
if ($firstCallWait) {
265-
usleep(500000);
277+
$timeToWait = static::$timeAllowedForEachApiCall;
266278
}
267279
} else {
268280
$now = microtime(true);
269-
$timeSinceLastCall = $now - static::$microtimeOfLastAPICall;
281+
$timeSinceLastCall = $now - static::$microtimeOfLastApiCall;
270282
//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);
283+
if($timeSinceLastCall < static::$timeAllowedForEachApiCall) {
284+
$timeToWait = static::$timeAllowedForEachApiCall - $timeSinceLastCall;
277285
}
278286
}
279-
static::$microtimeOfLastAPICall = microtime(true);
287+
288+
if ($timeToWait) {
289+
//convert time to microseconds
290+
$microSecondsToWait = $timeToWait * 1000000;
291+
//Wait to maintain the API call difference of .5 seconds
292+
usleep($microSecondsToWait);
293+
}
294+
295+
static::$microtimeOfLastApiCall = microtime(true);
280296
}
281297
}

tests/TestResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static function setUpBeforeClass()
2323
'ShopUrl' => 'phpclassic.myshopify.com',
2424
'ApiKey' => '81781200c08b31208031f983ab930f2a',
2525
'Password' => '5260904f8293bce93ddd4d65c535faa4',
26+
'AllowedTimePerCall' => 1, //2 test builds run at the same time, so keep 1 sec gap instead of .5 sec.
2627
);
2728

2829
self::$shopify = ShopifySDK::config($config);

0 commit comments

Comments
 (0)