Giter Site home page Giter Site logo

screenstaring / shopify_api_retry Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 4.0 27 KB

Retry a ShopifyAPI request if rate-limited or other errors occur. Works with the REST and GraphQL APIs.

License: MIT License

Ruby 100.00%
shopify-api http-status-code ruby shopify-app-development shopify shopify-graphql-api graphql rate-limiting

shopify_api_retry's Introduction

Shopify API Retry

CI

Simple Ruby module to retry a ShopifyAPI request if rate-limited or other errors occur. Works with the REST and GraphQL APIs.

Installation

Bundler:

gem "shopify_api_retry"

Gem:

gem install shopify_api_retry

Usage

REST API

By default requests are retried when a Shopify rate limit error (HTTP 429) is returned. The retry happens once after waiting for the seconds given by the HTTP Retry-After header:

require "shopify_api_retry" # requires "shopify_api" for you

ShopifyAPIRetry::REST.retry { customer.update_attribute(:tags, "foo") }
customer = ShopifyAPIRetry::REST.retry { ShopifyAPI::Customer.find(id) }

You can override this:

ShopifyAPIRetry::REST.retry(:wait => 3, :tries => 5) { customer.update_attribute(:tags, "foo")  }

This will try the request 5 times, waiting 3 seconds between each attempt. If a retry fails after the given number of :tries the last error will be raised.

You can also retry requests when other errors occur:

ShopifyAPIRetry::REST.retry "5XX" => { :wait => 10, :tries => 2 } do
  customer.update_attribute(:tags, "foo")
end

This still retries rate limit requests, but also all HTTP 5XX errors.

Classes can be specified too:

ShopifyAPIRetry::REST.retry SocketError => { :wait => 1, :tries => 5 } do
  customer.update_attribute(:tags, "foo")
end

You can also set global defaults.

GraphQL API

By default a retry attempt is made when your GraphQL request is rate-limited. In order to calculate the proper amount of time to wait before retrying you must set the X-GraphQL-Cost-Include-Fields header (maybe this library should do this?):

require "shopify_api_retry" # requires "shopify_api" for you

ShopifyAPI::Base.headers["X-GraphQL-Cost-Include-Fields"] = "true"

Once this is set run your queries and mutations and rate-limited requests will be retried. The retry happens once, calculating the wait time from the API's cost data:

result = ShopifyAPIRetry::GraphQL.retry { ShopifyAPI::GraphQL.client.query(YOUR_QUERY) }
p result.data.whatever

To calculate the retry time the query's return value must be returned by the block.

You can override the retry times:

result = ShopifyAPIRetry::GraphQL.retry(:wait => 4, :tries => 4) do
  ShopifyAPI::GraphQL.client.query(YOUR_QUERY)
end
p result.data.whatever

Like retry attempts made with the REST API you can specify errors to retry but, due to the GraphQL specification, these must not be HTTP status codes and are only relevant to network connection-related errors:

result = ShopifyAPIRetry::GraphQL.retry SocketError => { :wait => 1, :tries => 5 } do
  ShopifyAPI::GraphQL.client.query(YOUR_QUERY)
end

To give wait and try times for specifically for rate limit errors, use the key :graphql:

result = ShopifyAPIRetry::GraphQL.retry :graphql => { :wait => 10, :tries => 5 } do
  ShopifyAPI::GraphQL.client.query(YOUR_QUERY)
end

Note that specifying a :wait via :graphql or without an error key will skip calculating the retry based on the API response's cost data.

Global Defaults

You can configure global defaults to be used by REST and GraphQL calls. For example:

ShopifyAPIRetry.configure do |config|
  config.default_wait  = 2.5
  config.default_tries = 10

  # Use default_* for these
  config.on ["5XX", Net::ReadTimeout]

  config.on SocketError, :tries => 2, :wait => 1
end

See Also

License

Released under the MIT License: www.opensource.org/licenses/MIT


Made by ScreenStaring

shopify_api_retry's People

Contributors

sshaw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.