私はこれに対する答えを見つけて頭を叩いてきましたが、私には理解できません。microappsによるShopifyAPIのNode.jsモジュールを使用しています。更新する必要のある製品IDとSKUのリストを含むJSONオブジェクトがあるので、ファイルをループしてapiを呼び出す関数を呼び出します。ShopifyのAPIは、Shopifyへの呼び出しを制限し、残りの値を含む応答ヘッダーを送信します。このノードモジュールは、制限と使用法を含むオブジェクトを提供します。私の質問は、制限に達したときにsetTimeoutなどで実行できる方法を以下のコードに基づいています。最初の呼び出しを行うと、次のようなlimitsオブジェクトが返されます。
{
remaining: 30,
current: 10,
max: 40
}
私が試したすべてが失敗するので、制限を尊重せずに私が持っているものは次のとおりです。
const products = JSON.parse(fs.readFileSync('./skus.json','utf8'));
for(var i = 0;i < products.length; i++) {
updateProduct(products[i]);
}
function updateProduct(product){
shopify.productVariant.update(variant.id, { sku: variant.sku })
.then(result => cb(shopify.callLimits.remaining))
.catch(err => console.error(err.statusMessage));
}
ある種のコールバックを実装して、残りの使用量が少ないかどうかを確認してから、数秒待ってから再度呼び出す必要があることはわかっています。どんな助けでも大歓迎です。
https://github.com/lpinca/valveletのように、リクエストを作成するためにshopify-api-node
(Shopify.prototype.request
)で使用される関数の実行速度を制限するために何かを使用します。
以下のコードはテストされていませんが、機能するはずです。1秒あたり2回の呼び出しの制限を尊重する必要があります。
var Shopify = require('shopify-api-node');
var valvelet = require('valvelet');
var products = require('./skus');
var shopify = new Shopify({
shopName: 'your-shop-name',
apiKey: 'your-api-key',
password: 'your-app-password'
});
// Prevent the private shopify.request method from being called more than twice per second.
shopify.request = valvelet(shopify.request, 2, 1000);
var promises = products.map(function (product) {
return shopify.productVariant.update(product.id, { sku: product.sku });
});
Promise.all(promises).then(function (values) {
// Do something with the responses.
}).catch(function (err) {
console.error(err.stack);
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加