From 2d2919fda954812d695ff375fa4491029a8154bc Mon Sep 17 00:00:00 2001 From: schneefux Date: Sun, 26 Mar 2017 23:26:29 +0200 Subject: pay respect to rate limit --- api.js | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/api.js b/api.js index e4196a6..6f795dc 100644 --- a/api.js +++ b/api.js @@ -35,6 +35,13 @@ if (APITOKEN == undefined) throw "Need a valid API token!"; http.listen(8080); + +function sleep(ms) { + return new Promise(resolve => { + setTimeout(resolve, ms) + }); +} + /* API helper */ /* search for player name across all shards */ async function api_playerByAttr(attr, val) { @@ -54,20 +61,30 @@ async function api_playerByAttr(attr, val) { gzip: true }; options.qs[filter] = val; - try { - res = await request(options); - finds.push({ - "region": res.data[0].attributes.shardId, - "id": res.data[0].id, - "name": res.data[0].attributes.name, - "last_update": res.data[0].attributes.createdAt, - "source": "api" - }); - } catch (err) { - if (err.statusCode != 404) { - console.error(err); + retry = true; + while (retry) { + try { + res = await request(options); + finds.push({ + "region": res.data[0].attributes.shardId, + "id": res.data[0].id, + "name": res.data[0].attributes.name, + "last_update": res.data[0].attributes.createdAt, + "source": "api" + }); + retry = false; + } catch (err) { + if (err.statusCode == 429) { + await sleep(100); + retry = true; + } else if (err.statusCode == 404) { + retry = false; + } else { + console.error(err); + retry = false; + } + // TODO } - // TODO } } -- cgit v1.3.1