phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
568 stars 211 forks source link

resend requests that returns http errors #234

Closed fabio-sassi-spotview closed 2 years ago

fabio-sassi-spotview commented 3 years ago

Sometime for temporary issue, shopify http request fail, for example I found in my test logs:

Request failed with HTTP Code 520
Request failed with HTTP Code 503
OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 104

If you try again a moment later, same requests are ok.

This implementation try to mitigate this problem.

I add two configuration parameters MaxRequestRetry that define how many retry are allowed (default is 0 that does not change the original behaviour of the library) and RequestRetryCallback that allow to define a callback to manage or log the retry.

For example

PHPShopify\ShopifySDK::config([
    'ShopUrl' => SHOPIFY_URL,
    'AccessToken' => SHOPIFY_API_PASSWORD,
    'MaxRequestRetry' => 5,
    'RequestRetryCallback' => function($response, $error, $retry) {
        echo "retry n=$retry: " . $error->getMessage() . "\n";
        sleep(1); 
    }
]);
fabio-sassi-spotview commented 2 years ago

thank you very much @tareqtms for your advice, I create pull request #240 as you suggested.