Sha256: 973874312c928e6a2a1f9342a2a6590e9a0c8f8f54a4cddf93890ed4cb8e6a2a

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module ShopifyApiRateLimiter
  class DailySkuLimitError < ::ActiveResource::ClientError ; end

  module ThrottledConnection
    SHOPIFY_SLEEP_TIME = 0.5

    def request(method, path, *arguments)
      if self === ShopifyAPI::Base.connection
        if ShopifyAPI::Base.connection.response && ShopifyAPI.credit_maxed?
          ShopifyApiRateLimiter.logger.info "Shopify rate limit credit maxed. Sleeping #{SHOPIFY_SLEEP_TIME}..."
          sleep(SHOPIFY_SLEEP_TIME)
        end
        begin
          super
        rescue ActiveResource::ClientError => ex
          if ex.response.body == '{"errors":{"product":["Daily SKU limit reached. Please try again later."]}}'
            raise ShopifyApiRateLimiter::DailySkuLimitError, "Daily Sku Limit Reached for Shopify Admin API"
          elsif ex&.response&.code&.to_s == '429'
            ShopifyApiRateLimiter.logger.info "Shopify returned 429 (Rate Limit Exceeded). Sleeping #{SHOPIFY_SLEEP_TIME}..."
            sleep(SHOPIFY_SLEEP_TIME)
            retry
          else
            raise ex
          end
        end
      else
        super
      end
    rescue ShopifyAPI::Limits::LimitUnavailable => limit_unavailable
      # Either Shopify API stopped sending the limit header, or we're in a stubby test
      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shopify_api_rate_limiter-0.0.6 lib/shopify_api_rate_limiter/throttled_connection.rb