简体   繁体   中英

Shopify API - Response code = 429. Response message = Too Many Requests

I am working with Shopify API with rails. But I am getting an error. I don't know why this error showing.

ActiveResource::ClientError: Failed.  Response code = 429.  Response message = Too Many Requests.

I have followed this tutorial: https://help.shopify.com/api/getting-started/api-call-limit . Here is my code:

cycle = 0.5

product_count = ShopifyAPI::Product.count
nb_pages      = (product_count / 250.0).ceil

start_time = Time.now
1.upto(nb_pages) do |page|
    unless page == 1
        stop_time = Time.now
        processing_duration = stop_time - start_time
        wait_time = (cycle - processing_duration).ceil
        sleep wait_time if wait_time > 0
        start_time = Time.now
    end


    if product_list == 'all'
        products = ShopifyAPI::Product.find( :all, :params => { :limit => 250, :page => page } )
    elsif product_list.include? 'category'
        product_list.slice! "category"
        products = ShopifyAPI::Product.where(:collection_id => product_list, :limit => 250, :page => page )
    else
        products = ShopifyAPI::Product.find(:all, params: {ids: product_list})
    end

    products.each do |product|
        variants = Array.new
        metafields = Array.new
        product.variants.each do |variant|
            variants << variant
            metafields << product.metafields
            if variant.title.include? "+"
                next
            end
            quantity.each_with_index  do |q, i|

                unless customer_tag.to_s.strip.empty?
                    variants << {
                                    "title"=>"c #{variant.title} #{q}+",
                                    "price"=> price_calculate(calculation_type, discount_value[i], variant.price),
                                    "inventory_policy"=>"deny",
                                    "option1"=>"c #{variant.option1} #{q}+",
                                    "option2"=>nil,
                                    "option3"=>nil,
                                    "created_at"=> Time.now,
                                    "updated_at"=> Time.now,
                                    "taxable"=> variant.taxable,
                                    "inventory_quantity"=>1,
                                    "weight"=> variant.weight,
                                    "weight_unit"=> "#{variant.weight_unit}",
                                    "requires_shipping"=> variant.requires_shipping
                                }
                else
                    variants << {
                                    "title"=>"#{variant.title} #{q}+",
                                    "price"=> price_calculate(calculation_type, discount_value[i], variant.price),
                                    "inventory_policy"=>"deny",
                                    "option1"=>"#{variant.option1} #{q}+",
                                    "option2"=>nil,
                                    "option3"=>nil,
                                    "created_at"=> Time.now,
                                    "updated_at"=> Time.now,
                                    "taxable"=> variant.taxable,
                                    "inventory_quantity"=>1,
                                    "weight"=> variant.weight,
                                    "weight_unit"=> "#{variant.weight_unit}",
                                    "requires_shipping"=> variant.requires_shipping
                                }
                end

            end

        end

        unless customer_tag.to_s.strip.empty?
            metafields << {
                            "namespace": "customer_tag",
                            "key": "#{customer_tag}",
                            "value": "#{customer_tag}",
                            "value_type": "string"
                        }
        end

        product.variants = variants
        product.metafields = metafields
        product.save
    end
end

Why it's showing Shopify API Limit error ? I made only 1 API call by following their tutorial. Please any idea ..

Shopify follows leaky bucket algorithm, which means that it allows infrequent bursts of calls. But you need to average 0.5 seconds/call. These are the methods corresponding to different API calls in your script:

  1. ShopifyAPI::Product.find
  2. product.metafields
  3. product.save

Only the first call is being managed using the timing manager that you have written above. The next two calls will also need to be a part of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM