Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active May 11, 2017 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/cf0566bd598732c1e81bad0de1be4ff7 to your computer and use it in GitHub Desktop.
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)
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
@hrp
Copy link

hrp commented Apr 25, 2017

I had to change the first line to ShopifyAPI::Connection::RequestNotification.class_eval do.

@iloveitaly
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment