fix: improve JaxRequest wrapper

This commit is contained in:
Valentin Kaelin 2019-09-26 21:20:27 +02:00
parent 81e995b239
commit 84f7304c23
8 changed files with 63 additions and 34 deletions

View 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

View file

@ -1,11 +1,11 @@
const { ServiceProvider } = require('@adonisjs/fold')
const Jax = require('./src/Jax')
const Config = require('./JaxConfig')
class JaxProvider extends ServiceProvider {
register () {
this.app.singleton('Jax', () => {
const Env = use('Env')
return new Jax(Env.get('API_KEY'), Env.get('NODE_ENV'))
return new Jax(Config)
})
}

View file

@ -1,16 +1,16 @@
const JaxRequest = require('../JaxRequest')
class LeagueEndpoint {
constructor(limiter, region) {
constructor(config, limiter) {
this.config = config
this.limiter = limiter
this.region = region
}
summonerID(summonerID) {
return new JaxRequest(
this.config,
`league/v4/entries/by-summoner/${summonerID}`,
this.limiter,
this.region
this.limiter
).execute()
}
}

View file

@ -1,18 +1,18 @@
const JaxRequest = require('../JaxRequest')
class MatchEndpoint {
constructor(limiter, region) {
constructor(config, limiter) {
this.config = config
this.limiter = limiter
this.region = region
this.get = this.get.bind(this)
}
get(matchID) {
return new JaxRequest(
this.config,
`match/v4/matches/${matchID}`,
this.limiter,
this.region
this.limiter
).execute()
}
}

View file

@ -1,16 +1,16 @@
const JaxRequest = require('../JaxRequest')
class MatchlistEndpoint {
constructor(limiter, region) {
constructor(config, limiter) {
this.config = config
this.limiter = limiter
this.region = region
}
accountID(accountID, beginIndex = 0) {
return new JaxRequest(
this.config,
`match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`,
this.limiter,
this.region
this.limiter
).execute()
}
}

View file

@ -1,16 +1,16 @@
const JaxRequest = require('../JaxRequest')
class SummonerEndpoint {
constructor(limiter, region) {
constructor(config, limiter) {
this.config = config
this.limiter = limiter
this.region = region
}
summonerName(summonerName) {
return new JaxRequest(
this.config,
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
this.limiter,
this.region
this.limiter
).execute()
}
}

View file

@ -1,5 +1,4 @@
const RiotRateLimiter = require('riot-ratelimiter')
const { STRATEGY } = require('riot-ratelimiter/dist/RateLimiter')
const LeagueEndpoint = require('./Endpoints/LeagueEndpoint')
const MatchEndpoint = require('./Endpoints/MatchEndpoint')
const MatchlistEndpoint = require('./Endpoints/MatchlistEndpoint')
@ -10,18 +9,18 @@ const DDragonChampionEndpoint = require('./Endpoints/DDragonEndpoints/DDragonCha
const DDragonRuneEndpoint = require('./Endpoints/DDragonEndpoints/DDragonRuneEndpoint')
class Jax {
constructor(key, env) {
this.key = key
constructor(config) {
this.key = config.key
const limiterOptions = {
strategy: env === 'production' ? STRATEGY.SPREAD : STRATEGY.BURST
strategy: config.requestOptions.strategy
}
this.limiter = new RiotRateLimiter(limiterOptions)
this.region = 'euw1'
this.config = config
this.League = new LeagueEndpoint(this.limiter, this.region)
this.Match = new MatchEndpoint(this.limiter, this.region)
this.Matchlist = new MatchlistEndpoint(this.limiter, this.region)
this.Summoner = new SummonerEndpoint(this.limiter, this.region)
this.League = new LeagueEndpoint(this.config, this.limiter)
this.Match = new MatchEndpoint(this.config, this.limiter)
this.Matchlist = new MatchlistEndpoint(this.config, this.limiter)
this.Summoner = new SummonerEndpoint(this.config, this.limiter)
this.initDDragon()
}
@ -37,8 +36,8 @@ class Jax {
}
set regionName(regionName) {
this.region = regionName
const blacklistedProperties = ['key', 'limiter', 'region', 'version', 'DDragon']
this.config.region = regionName
const blacklistedProperties = ['key', 'limiter', 'config', 'version', 'DDragon']
for (const key of Object.getOwnPropertyNames(this)) {
if(blacklistedProperties.includes(key)) continue

View file

@ -1,22 +1,37 @@
class JaxRequest {
constructor(endpoint, limiter, region) {
constructor(config, endpoint, limiter) {
this.config = config
this.endpoint = endpoint
this.limiter = limiter
this.region = region
this.retries = config.requestOptions.retriesBeforeAbort
}
async execute() {
try {
const resp = await this.limiter.executing({
url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`,
token: process.env.API_KEY,
url: `https://${this.config.region}.api.riotgames.com/lol/${this.endpoint}`,
token: this.config.key,
resolveWithFullResponse: false
})
return JSON.parse(resp)
} 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,
)
}
}
}