Last active
May 11, 2017 15:07
-
-
Save iloveitaly/cf0566bd598732c1e81bad0de1be4ff7 to your computer and use it in GitHub Desktop.
Automatically retry shopify API requests if they fail due to API request limits (code 429)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ShopifyAPI::Connection.class_eval do | |
alias_method :shopify_request, :request | |
def request(*args) | |
count = 0 | |
limit = 10 | |
begin | |
count += 1 | |
shopify_request(*args) | |
rescue ActiveResource::ClientError => e | |
# TODO look at code instead of static string? This is brittle | |
if count >= limit || e.message != "Failed. Response code = 429. Response message = Too Many Requests." | |
raise | |
else | |
sleep(count) | |
retry | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@hrp FYI I've bundled this into a gem https://github.com/iloveitaly/shopify_api_extensions