我正在使用带有rails的Shopify API。但是我收到了一个错误。 我不知道为什么会出现这个错误。
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 Limit错误?按照他们的教程,我只进行了一次API调用。请任何想法..
答案 0 :(得分:0)
Shopify遵循漏桶算法,这意味着它允许不频繁的呼叫突发。但你需要平均0.5秒/通话。这些是与脚本中不同API调用相对应的方法:
ShopifyAPI::Product.find
product.metafields
product.save
使用您在上面编写的时序管理器仅管理第一个呼叫。接下来的两个电话也需要成为它的一部分。