2020-10-04 15:30:04 +00:00
|
|
|
import { promisify } from 'util'
|
|
|
|
|
import { JaxConfig } from '../JaxConfig'
|
|
|
|
|
import Logger from '@ioc:Adonis/Core/Logger'
|
|
|
|
|
import Redis from '@ioc:Adonis/Addons/Redis'
|
2020-10-10 20:39:13 +00:00
|
|
|
import RiotRateLimiter from 'riot-ratelimiter'
|
|
|
|
|
// import { RiotRateLimiter } from '@fightmegg/riot-rate-limiter'
|
2020-10-04 15:30:04 +00:00
|
|
|
|
|
|
|
|
export default class JaxRequest {
|
|
|
|
|
private region: string
|
|
|
|
|
private config: JaxConfig
|
|
|
|
|
private endpoint: string
|
|
|
|
|
private limiter: RiotRateLimiter
|
|
|
|
|
private cacheTime: number
|
|
|
|
|
private retries: number
|
|
|
|
|
private sleep: { (ms: number): Promise<void>; <T>(ms: number, value: T): Promise<T> }
|
|
|
|
|
|
|
|
|
|
constructor (region: string, config: JaxConfig, endpoint: string, limiter: RiotRateLimiter, cacheTime: number) {
|
|
|
|
|
this.region = region
|
|
|
|
|
this.config = config
|
|
|
|
|
this.endpoint = endpoint
|
|
|
|
|
this.limiter = limiter
|
|
|
|
|
this.cacheTime = cacheTime
|
|
|
|
|
this.retries = config.requestOptions.retriesBeforeAbort
|
|
|
|
|
|
|
|
|
|
this.sleep = promisify(setTimeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async execute () {
|
|
|
|
|
const url = `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`
|
|
|
|
|
|
|
|
|
|
// Redis cache
|
|
|
|
|
if (this.cacheTime > 0) {
|
|
|
|
|
const requestCached = await Redis.get(url)
|
|
|
|
|
if (requestCached) {
|
|
|
|
|
return JSON.parse(requestCached)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2020-10-10 20:39:13 +00:00
|
|
|
const resp:any = await this.limiter.executing({
|
2020-10-04 15:30:04 +00:00
|
|
|
url,
|
2020-10-10 20:39:13 +00:00
|
|
|
token: this.config.key,
|
|
|
|
|
resolveWithFullResponse: false,
|
2020-10-04 15:30:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (this.cacheTime > 0) {
|
2020-10-10 20:39:13 +00:00
|
|
|
await Redis.setex(url, this.cacheTime, resp)
|
2020-10-04 15:30:04 +00:00
|
|
|
}
|
2020-10-10 20:39:13 +00:00
|
|
|
return JSON.parse(resp)
|
2020-10-18 13:24:35 +00:00
|
|
|
} catch ({ statusCode , ...rest }) {
|
2020-10-04 15:30:04 +00:00
|
|
|
this.retries--
|
|
|
|
|
|
2020-10-18 13:24:35 +00:00
|
|
|
if (statusCode !== 500 && statusCode !== 503 && statusCode !== 504) { //
|
2020-10-04 15:30:04 +00:00
|
|
|
// Don't log 404 when summoner isn't playing or the summoner doesn't exist
|
2020-10-10 20:39:13 +00:00
|
|
|
// Or if summoner has no MatchList
|
2020-10-04 15:30:04 +00:00
|
|
|
if (!this.endpoint.includes('spectator/v4/active-games/by-summoner') &&
|
2020-10-10 20:39:13 +00:00
|
|
|
!this.endpoint.includes('summoner/v4/summoners/by-name') &&
|
|
|
|
|
!this.endpoint.includes('match/v4/matchlists/by-account')
|
2020-10-04 15:30:04 +00:00
|
|
|
) {
|
2020-10-18 13:24:35 +00:00
|
|
|
Logger.error(`JaxRequest Error ${statusCode}: `, rest)
|
2020-10-04 15:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('====================================')
|
2020-10-18 13:24:35 +00:00
|
|
|
console.log(statusCode)
|
2020-10-04 15:30:04 +00:00
|
|
|
console.log('====================================')
|
|
|
|
|
|
|
|
|
|
if (this.retries > 0) {
|
|
|
|
|
await this.sleep(this.config.requestOptions.delayBeforeRetry)
|
|
|
|
|
return this.execute()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|