2017-05-16 76 views
0

私はShopify APIを使用しています。しかし、私はエラーが発生しています。 このエラーがなぜ表示されるのか分かりません。Shopify API - レスポンスコード= 429.レスポンスメッセージ=リクエストが多すぎます

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

このチュートリアルは、https://help.shopify.com/api/getting-started/api-call-limitに従っています。ここに私のコードはあります:

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 

なぜShopify API限界エラーですか?私は彼らのチュートリアルに従うことによってわずか1つのAPIコールを作った。どうかしてください。

+0

リンクしたページの指示に従ってAPIKEY、PASSWORD、SHOPNAMEを使用して 'cleanup.rb'を作成しましたか? –

+0

私はこの宝石、https://github.com/Shopify/shopify_appを使用しました。他は完全に働いています。 – Saiful

+0

独自のAPIキーを使用していますか? [shopify_app - Apiキーの管理](https://github.com/Shopify/shopify_app#managing-api-keys) –

答えて

0

Shopifyは、漏れバケットアルゴリズムに従っています。これは、まれなバースト呼び出しを可能にすることを意味します。しかし、平均0.5秒/コールする必要があります。これらは、スクリプト内の別のAPI呼び出しに対応するメソッドです:

のみ最初の呼び出しは、あなたが上記の書かれているタイミングマネージャを使用して管理されている
  1. ShopifyAPI::Product.find
  2. product.metafields
  3. product.save

。次の2回のコールもその一部にする必要があります。

関連する問題