LeagueStats/server/providers/Jax/src/JaxRequest.js

44 lines
1 KiB
JavaScript
Raw Normal View History

const { promisify } = require('util')
2019-08-24 14:56:55 +00:00
class JaxRequest {
constructor(region, config, endpoint, limiter) {
this.region = region
2019-09-26 19:20:27 +00:00
this.config = config
2019-08-24 14:56:55 +00:00
this.endpoint = endpoint
this.limiter = limiter
2019-09-26 19:20:27 +00:00
this.retries = config.requestOptions.retriesBeforeAbort
this.sleep = promisify(setTimeout)
2019-08-24 14:56:55 +00:00
}
async execute() {
try {
const resp = await this.limiter.executing({
url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`,
2019-09-26 19:20:27 +00:00
token: this.config.key,
2019-08-24 14:56:55 +00:00
resolveWithFullResponse: false
})
return JSON.parse(resp)
} catch ({ statusCode, ...rest }) {
2019-09-26 19:20:27 +00:00
this.retries--
2019-09-26 19:22:45 +00:00
if (statusCode !== 503 && statusCode !== 500) return
2019-09-26 19:20:27 +00:00
console.log('====================================')
console.log(statusCode)
console.log('====================================')
if (this.retries > 0) {
await this.sleep(this.config.requestOptions.delayBeforeRetry)
return this.execute()
2019-09-26 19:20:27 +00:00
}
2019-08-24 14:56:55 +00:00
}
}
}
module.exports = JaxRequest