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

41 lines
948 B
JavaScript
Raw Normal View History

2019-08-24 14:56:55 +00:00
class JaxRequest {
2019-09-26 19:20:27 +00:00
constructor(config, endpoint, limiter) {
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
2019-08-24 14:56:55 +00:00
}
async execute() {
try {
const resp = await this.limiter.executing({
2019-09-26 19:20:27 +00:00
url: `https://${this.config.region}.api.riotgames.com/lol/${this.endpoint}`,
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--
if (statusCode !== 503 && status !== 500) return
console.log('====================================')
console.log(statusCode)
console.log('====================================')
if (this.retries > 0) {
setTimeout(
() => this.execute(),
this.config.requestOptions.delayBeforeRetry,
)
}
2019-08-24 14:56:55 +00:00
}
}
}
module.exports = JaxRequest