大佬教程收集整理的这篇文章主要介绍了如何减少调用 API 的频率?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在通过 REST API 更新库存项目, 第一次调用:获取令牌
然后循环遍历包含项目和数量的数组 在这些循环中,有 2 个调用(一个用于获取外部系统项目 ID,另一个用于更新数量)。 我的系统是netsuite,外部系统是lightspeed。
所以对于我的代码是用于令牌调用和循环调用包装
async function exec(){
const resfilEID = await runtime.getCurrentScript().getParameter({
name: "custscript_filEID"
});
const res@R_403_5962@ = await runtime.getCurrentScript().getParameter({
name: "custscript_lspd_@R_403_5962@"
});
log.audit("file ID",resfilEID);
log.audit("@R_403_5962@ Type",res@R_403_5962@);
if(resfilEID){
const csvfile = await file.load({
ID:resfilEID
});
const data = await readfile(csvfilE);
log.deBUG("data",data);
if(res@R_403_5962@ == "updateInventory" && data)
const token = await lightspeedrefreshThetoken();
for(let i of data){
await setTimeout(function(){ log.deBUG('In Loop','Ran after 1 second'); },1000);
await lightspeedupdateItemQuantity(token,data[0],data[1]);
}
}
}
}
令牌函数
async function lightspeedrefreshThetoken(context) {
await setTimeout(function(){ log.audit('Token Ran after 1 second'); },1000);
var lightspeedStringUrl = lightspeedHost + "/oauth/access_token.php";
var lightspeedGrantType = "refresh_token";
var lightspeedData = "clIEnt_ID=" + lightspeedClIEntID
+ "&clIEnt_secret=" + lightspeedClIEntSecret
+ "&grant_type=" + lightspeedGrantType
+ "&refresh_token=" + lightspeedrefreshToken;
var lightspeedheader = { "Content-Type": "application/x-www-form-urlencoded" };
const response = await https
.post({
url: lightspeedStringUrl,body: lightspeedData,headers: lightspeedheader
})
var newSFID = JsON.parse(response.body);
return newSFID.access_token
};
详情调用函数
async function lightspeedupdateItemQuantity(token,item,quantity) {
const lightspeedQuantity = quantity;
await setTimeout(function(){ log.deBUG('update Shop Quantity',1000);
const lightspeedDetails = await lightspeedIGetItemID(token,item)
.then(async function (result){
let lightspeedItemID = result[0];
let lightspeedItemShopID = result[1];
await setTimeout(function(){ log.deBUG('update Shop Item',1000);
const shop = await lightspeedupdateShopItem(token,lightspeedItemShopID,lightspeedItemID,quantity);
});
}
伪setTimeout函数
async function setTimeout(aFunction,milliseconds){
var date = new Date();
date.setMilliseconds(date.getMilliseconds() + milliseconds);
while(new Date() < datE){
}
return aFunction();
}
尽管有所有延迟,但外部服务仍在发送 429 响应代码。难道我做错了什么? 注意:服务器端脚本中的 netsuite 中没有可用的 setTimeout。
{ http代码:“429”, httpmessage: "请求过多",消息:“超出速率限制:请减少您的请求量。要消除此消息,请将您的请求速率保持在每秒 1 个请求以下。继续收到此响应的客户端可能会受到流量限制。”,errorClass: "RatelimitedException" }
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的如何减少调用 API 的频率?全部内容,希望文章能够帮你解决如何减少调用 API 的频率?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。