diff --git a/composer.json b/composer.json index d92b1b00..c6a7ffca 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": ">=7.1.0", "laravel/framework": "~5.5", - "ohmybrew/basic-shopify-api": "~3.0", + "ohmybrew/basic-shopify-api": "~4.0", "doctrine/dbal": "~2.5" }, "require-dev":{ diff --git a/src/ShopifyApp/ShopifyApp.php b/src/ShopifyApp/ShopifyApp.php index 93a6cf64..74480521 100644 --- a/src/ShopifyApp/ShopifyApp.php +++ b/src/ShopifyApp/ShopifyApp.php @@ -65,6 +65,13 @@ public function api() $api->setApiKey(config('shopify-app.api_key')); $api->setApiSecret(config('shopify-app.api_secret')); + if (config('shopify-app.api_rate_limiting_enabled') === true) { + $api->enableRateLimiting( + config('shopify-app.api_rate_limit_cycle'), + config('shopify-app.api_rate_limit_cycle_buffer') + ); + } + return $api; } diff --git a/src/ShopifyApp/resources/config/shopify-app.php b/src/ShopifyApp/resources/config/shopify-app.php index fe996572..7700a525 100644 --- a/src/ShopifyApp/resources/config/shopify-app.php +++ b/src/ShopifyApp/resources/config/shopify-app.php @@ -105,6 +105,48 @@ 'api_class' => env('SHOPIFY_API_CLASS', \OhMyBrew\BasicShopifyAPI::class), + /* + |-------------------------------------------------------------------------- + | Shopify API Rate Limiting + |-------------------------------------------------------------------------- + | + | This option option allows you to enable basic rate limiting + | for API calls using the default BasicShopifyAPI library. + | Default is off. + | + */ + + 'api_rate_limiting_enabled' => env('SHOPIFY_API_RATE_LIMITING_ENABLED', false), + + /* + |-------------------------------------------------------------------------- + | Shopify API Rate Limit Cycle + |-------------------------------------------------------------------------- + | + | This option option allows you to set the millisecond cycle for + | API calls using the default BasicShopifyAPI library. + | Default is 500ms per API call. + | Example: 0.5 * 1000 + | + */ + + 'api_rate_limit_cycle' => env('SHOPIFY_API_RATE_LIMIT_CYCLE', null), + + /* + |-------------------------------------------------------------------------- + | Shopify API Rate Limit Cycle Buffer + |-------------------------------------------------------------------------- + | + | This option option allows you to set the millisecond buffer for + | API calls using the default BasicShopifyAPI library which gets + | appended to the `api_rate_limit_cycle` value for a safety net. + | Default is 100ms per API call. + | Example: 0.1 * 1000 + | + */ + + 'api_rate_limit_cycle_buffer' => env('SHOPIFY_API_RATE_LIMIT_CYCLE_BUFFER', null), + /* |-------------------------------------------------------------------------- | Shopify "MyShopify" domain diff --git a/tests/ShopifyAppTest.php b/tests/ShopifyAppTest.php index 039d32b6..de3e64eb 100644 --- a/tests/ShopifyAppTest.php +++ b/tests/ShopifyAppTest.php @@ -49,6 +49,13 @@ public function testReturnsApiInstance() $this->assertEquals(\OhMyBrew\BasicShopifyAPI::class, get_class($this->shopifyApp->api())); } + public function testReturnsApiInstanceWithRateLimiting() + { + config(['shopify-app.api_rate_limiting_enabled' => true]); + + $this->assertTrue($this->shopifyApp->api()->isRateLimitingEnabled()); + } + public function testShopSanitize() { $domains = ['my-shop', 'my-shop.myshopify.com', 'https://my-shop.myshopify.com', 'http://my-shop.myshopify.com'];