diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2017-03-26 23:26:29 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2017-03-26 23:26:29 +0200 |
| commit | 2d2919fda954812d695ff375fa4491029a8154bc (patch) | |
| tree | 2683644eb7cb74735d927a51099d397197c11daa | |
| parent | 3fce1e57d76a168f91f42f0130d86bca57590381 (diff) | |
| download | bridge-2d2919fda954812d695ff375fa4491029a8154bc.tar.gz bridge-2d2919fda954812d695ff375fa4491029a8154bc.zip | |
pay respect to rate limit
| -rw-r--r-- | api.js | 43 |
1 files changed, 30 insertions, 13 deletions
@@ -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 } } |
