phpclassic / php-shopify

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

resend requests that returns http error #240

Closed fabio-sassi-spotview closed 2 years ago

fabio-sassi-spotview commented 2 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 a configuration parameters RequestRetryCallback that allow to define a callback to manage or log the retry. The callback take 3 input parameter: raw curl response, exception object and retry number. If this callback return true (or non null value) another attempt will be performed.

By default this callback is not defined and this does not change the original behavior of the library.

For example

PHPShopify\ShopifySDK::config([
    'ShopUrl' => SHOPIFY_URL,
    'AccessToken' => SHOPIFY_API_PASSWORD,
    'RequestRetryCallback' => function($response, $error, $retry) {
        echo "retry n=$retry: " . $error->getMessage() . "\n";
        sleep(1); 
        return $retry <= 5;
    }
]);

This is a modified version of #234 after @tareqtms advice.

fabio-sassi-spotview commented 2 years ago

Hi @tareqtms

Any news about this pull request? Do you think it would be ok or it need of some refactory?

tareqtms commented 2 years ago

@fabio-sassi-spotview Sorry, I need to test GraphQL manually as there is no test case made for that yet. Just to make sure it didn't break. I will do it by tomorrow (hopefully).

fabio-sassi-spotview commented 2 years ago

Hi @tareqtms

Have you any news?

tareqtms commented 2 years ago

@fabio-sassi-spotview Merged and released. Sorry for the delay and thanks for your patience.

fabio-sassi-spotview commented 2 years ago

thank you very much @tareqtms