mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 12:57:28 +00:00
fix: improve JaxRequest wrapper
This commit is contained in:
parent
81e995b239
commit
84f7304c23
8 changed files with 63 additions and 34 deletions
15
server/providers/Jax/JaxConfig.js
Normal file
15
server/providers/Jax/JaxConfig.js
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
require('dotenv').config()
|
||||||
|
const { STRATEGY } = require('riot-ratelimiter/dist/RateLimiter')
|
||||||
|
|
||||||
|
const JAX_CONFIG = {
|
||||||
|
key: process.env.API_KEY,
|
||||||
|
region: 'euw1',
|
||||||
|
requestOptions: {
|
||||||
|
retriesBeforeAbort: 3,
|
||||||
|
delayBeforeRetry: 1000,
|
||||||
|
// strategy: process.env.NODE_ENV === 'production' ? STRATEGY.SPREAD : STRATEGY.BURST,
|
||||||
|
strategy: STRATEGY.SPREAD,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = JAX_CONFIG
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
const { ServiceProvider } = require('@adonisjs/fold')
|
const { ServiceProvider } = require('@adonisjs/fold')
|
||||||
const Jax = require('./src/Jax')
|
const Jax = require('./src/Jax')
|
||||||
|
const Config = require('./JaxConfig')
|
||||||
|
|
||||||
class JaxProvider extends ServiceProvider {
|
class JaxProvider extends ServiceProvider {
|
||||||
register () {
|
register () {
|
||||||
this.app.singleton('Jax', () => {
|
this.app.singleton('Jax', () => {
|
||||||
const Env = use('Env')
|
return new Jax(Config)
|
||||||
return new Jax(Env.get('API_KEY'), Env.get('NODE_ENV'))
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
const JaxRequest = require('../JaxRequest')
|
const JaxRequest = require('../JaxRequest')
|
||||||
|
|
||||||
class LeagueEndpoint {
|
class LeagueEndpoint {
|
||||||
constructor(limiter, region) {
|
constructor(config, limiter) {
|
||||||
|
this.config = config
|
||||||
this.limiter = limiter
|
this.limiter = limiter
|
||||||
this.region = region
|
|
||||||
}
|
}
|
||||||
|
|
||||||
summonerID(summonerID) {
|
summonerID(summonerID) {
|
||||||
return new JaxRequest(
|
return new JaxRequest(
|
||||||
|
this.config,
|
||||||
`league/v4/entries/by-summoner/${summonerID}`,
|
`league/v4/entries/by-summoner/${summonerID}`,
|
||||||
this.limiter,
|
this.limiter
|
||||||
this.region
|
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
const JaxRequest = require('../JaxRequest')
|
const JaxRequest = require('../JaxRequest')
|
||||||
|
|
||||||
class MatchEndpoint {
|
class MatchEndpoint {
|
||||||
constructor(limiter, region) {
|
constructor(config, limiter) {
|
||||||
|
this.config = config
|
||||||
this.limiter = limiter
|
this.limiter = limiter
|
||||||
this.region = region
|
|
||||||
|
|
||||||
this.get = this.get.bind(this)
|
this.get = this.get.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
get(matchID) {
|
get(matchID) {
|
||||||
return new JaxRequest(
|
return new JaxRequest(
|
||||||
|
this.config,
|
||||||
`match/v4/matches/${matchID}`,
|
`match/v4/matches/${matchID}`,
|
||||||
this.limiter,
|
this.limiter
|
||||||
this.region
|
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
const JaxRequest = require('../JaxRequest')
|
const JaxRequest = require('../JaxRequest')
|
||||||
|
|
||||||
class MatchlistEndpoint {
|
class MatchlistEndpoint {
|
||||||
constructor(limiter, region) {
|
constructor(config, limiter) {
|
||||||
|
this.config = config
|
||||||
this.limiter = limiter
|
this.limiter = limiter
|
||||||
this.region = region
|
|
||||||
}
|
}
|
||||||
|
|
||||||
accountID(accountID, beginIndex = 0) {
|
accountID(accountID, beginIndex = 0) {
|
||||||
return new JaxRequest(
|
return new JaxRequest(
|
||||||
|
this.config,
|
||||||
`match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`,
|
`match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`,
|
||||||
this.limiter,
|
this.limiter
|
||||||
this.region
|
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
const JaxRequest = require('../JaxRequest')
|
const JaxRequest = require('../JaxRequest')
|
||||||
|
|
||||||
class SummonerEndpoint {
|
class SummonerEndpoint {
|
||||||
constructor(limiter, region) {
|
constructor(config, limiter) {
|
||||||
|
this.config = config
|
||||||
this.limiter = limiter
|
this.limiter = limiter
|
||||||
this.region = region
|
|
||||||
}
|
}
|
||||||
|
|
||||||
summonerName(summonerName) {
|
summonerName(summonerName) {
|
||||||
return new JaxRequest(
|
return new JaxRequest(
|
||||||
|
this.config,
|
||||||
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
|
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
|
||||||
this.limiter,
|
this.limiter
|
||||||
this.region
|
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
const RiotRateLimiter = require('riot-ratelimiter')
|
const RiotRateLimiter = require('riot-ratelimiter')
|
||||||
const { STRATEGY } = require('riot-ratelimiter/dist/RateLimiter')
|
|
||||||
const LeagueEndpoint = require('./Endpoints/LeagueEndpoint')
|
const LeagueEndpoint = require('./Endpoints/LeagueEndpoint')
|
||||||
const MatchEndpoint = require('./Endpoints/MatchEndpoint')
|
const MatchEndpoint = require('./Endpoints/MatchEndpoint')
|
||||||
const MatchlistEndpoint = require('./Endpoints/MatchlistEndpoint')
|
const MatchlistEndpoint = require('./Endpoints/MatchlistEndpoint')
|
||||||
|
|
@ -10,18 +9,18 @@ const DDragonChampionEndpoint = require('./Endpoints/DDragonEndpoints/DDragonCha
|
||||||
const DDragonRuneEndpoint = require('./Endpoints/DDragonEndpoints/DDragonRuneEndpoint')
|
const DDragonRuneEndpoint = require('./Endpoints/DDragonEndpoints/DDragonRuneEndpoint')
|
||||||
|
|
||||||
class Jax {
|
class Jax {
|
||||||
constructor(key, env) {
|
constructor(config) {
|
||||||
this.key = key
|
this.key = config.key
|
||||||
const limiterOptions = {
|
const limiterOptions = {
|
||||||
strategy: env === 'production' ? STRATEGY.SPREAD : STRATEGY.BURST
|
strategy: config.requestOptions.strategy
|
||||||
}
|
}
|
||||||
this.limiter = new RiotRateLimiter(limiterOptions)
|
this.limiter = new RiotRateLimiter(limiterOptions)
|
||||||
this.region = 'euw1'
|
this.config = config
|
||||||
|
|
||||||
this.League = new LeagueEndpoint(this.limiter, this.region)
|
this.League = new LeagueEndpoint(this.config, this.limiter)
|
||||||
this.Match = new MatchEndpoint(this.limiter, this.region)
|
this.Match = new MatchEndpoint(this.config, this.limiter)
|
||||||
this.Matchlist = new MatchlistEndpoint(this.limiter, this.region)
|
this.Matchlist = new MatchlistEndpoint(this.config, this.limiter)
|
||||||
this.Summoner = new SummonerEndpoint(this.limiter, this.region)
|
this.Summoner = new SummonerEndpoint(this.config, this.limiter)
|
||||||
|
|
||||||
this.initDDragon()
|
this.initDDragon()
|
||||||
}
|
}
|
||||||
|
|
@ -37,8 +36,8 @@ class Jax {
|
||||||
}
|
}
|
||||||
|
|
||||||
set regionName(regionName) {
|
set regionName(regionName) {
|
||||||
this.region = regionName
|
this.config.region = regionName
|
||||||
const blacklistedProperties = ['key', 'limiter', 'region', 'version', 'DDragon']
|
const blacklistedProperties = ['key', 'limiter', 'config', 'version', 'DDragon']
|
||||||
|
|
||||||
for (const key of Object.getOwnPropertyNames(this)) {
|
for (const key of Object.getOwnPropertyNames(this)) {
|
||||||
if(blacklistedProperties.includes(key)) continue
|
if(blacklistedProperties.includes(key)) continue
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,37 @@
|
||||||
class JaxRequest {
|
class JaxRequest {
|
||||||
constructor(endpoint, limiter, region) {
|
constructor(config, endpoint, limiter) {
|
||||||
|
this.config = config
|
||||||
this.endpoint = endpoint
|
this.endpoint = endpoint
|
||||||
this.limiter = limiter
|
this.limiter = limiter
|
||||||
this.region = region
|
this.retries = config.requestOptions.retriesBeforeAbort
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute() {
|
async execute() {
|
||||||
try {
|
try {
|
||||||
const resp = await this.limiter.executing({
|
const resp = await this.limiter.executing({
|
||||||
url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`,
|
url: `https://${this.config.region}.api.riotgames.com/lol/${this.endpoint}`,
|
||||||
token: process.env.API_KEY,
|
token: this.config.key,
|
||||||
resolveWithFullResponse: false
|
resolveWithFullResponse: false
|
||||||
})
|
})
|
||||||
|
|
||||||
return JSON.parse(resp)
|
return JSON.parse(resp)
|
||||||
|
|
||||||
} catch ({ statusCode, ...rest }) {
|
} catch ({ statusCode, ...rest }) {
|
||||||
console.log('error: ' + statusCode)
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue