Start Jax: Riot Api Wrapper

This commit is contained in:
Valentin Kaelin 2019-08-24 16:56:55 +02:00
parent b8ec9ca80f
commit 3683776e24
5 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,18 @@
import JaxRequest from '../JaxRequest'
class SummonerEnpoint {
constructor(limiter, region) {
this.limiter = limiter
this.region = region
}
summonerName(summonerName) {
return new JaxRequest(
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
this.limiter,
this.region
).execute()
}
}
export default SummonerEnpoint

21
server/Jax/Jax.js Normal file
View file

@ -0,0 +1,21 @@
const RiotRateLimiter = require('riot-ratelimiter')
import SummonerEndpoint from './Endpoints/SummonerEndpoint'
class Jax {
constructor(key = process.env.API_KEY, region = 'euw1') {
this.key = key
this.limiter = new RiotRateLimiter()
this.region = region
this.Summoner = new SummonerEndpoint(this.limiter, this.region)
}
set regionName(regionName) {
this.region = regionName
}
}
module.exports = {
Jax
}

26
server/Jax/JaxRequest.js Normal file
View file

@ -0,0 +1,26 @@
class JaxRequest {
constructor(endpoint, limiter, region) {
this.endpoint = endpoint
this.region = region
this.limiter = limiter
this.region = region
}
async execute() {
try {
const resp = await this.limiter.executing({
url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`,
token: process.env.API_KEY,
resolveWithFullResponse: false
})
return JSON.parse(resp)
} catch ({ statusCode, ...rest }) {
console.log('error: ' + statusCode, rest)
}
}
}
export default JaxRequest

1
server/Jax/index.js Normal file
View file

@ -0,0 +1 @@
module.exports = require('./Jax')

View file

@ -7,6 +7,7 @@ const Promise = require("bluebird");
const responseTime = require('response-time')
const cors = require('cors');
const app = express()
import { Jax } from "./Jax";
/* Global Variables */
const data = {
@ -19,6 +20,9 @@ const data = {
finalJSON: []
}
/* Setup Riot API Wrapper */
const jax = new Jax()
/* Set Port */
app.set('port', (process.env.PORT || 5000))
@ -69,10 +73,21 @@ app.post('/api', function (req, res) {
console.time('all')
data.region = req.body.region;
data.username = req.body.summoner;
jax.regionName = req.body.region
newVersion()
data.finalJSON = [];
getAccountInfos(res);
});
/* Refactor with the Jax Wrapper */
async function newVersion() {
const { id, accountId } = await jax.Summoner.summonerName(data.username)
console.log(id, accountId)
}
// Get account infos of an username
const getAccountInfos = function (res) {
request(`https://${data.region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${encodeURIComponent(data.username)}?api_key=${data.key}`, function (error, response, body) {