Exemple API en TD : team LoL

master
Juan 3 years ago
parent 1d19b7b04e
commit 00ebe47554

@ -0,0 +1,3 @@
lol_match=https://europe.api.riotgames.com
lol_account=https://euw1.api.riotgames.com
keyLol=RGAPI-b1198975-16a9-4767-a81c-e3fae2a6df1f

@ -0,0 +1,74 @@
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
const lolaccount = process.env.lol_account;
const lolmatch = process.env.lol_match;
const header = { headers: { 'X-Riot-Token': process.env.keyLol }};
export default class Manager {
async getInfo(name) {
const user0 = await this.getAccountByName(name);
if (!user0?.puuid) {
return {error: 'Pseudo invalide'};
}
const listMatch = await this.getMatchByUuid(user0.puuid);
const lastMatchInfo = await this.getInfoByMatchId(listMatch[0]);
let allUser = lastMatchInfo.info.participants;
allUser = allUser.map(x => ({
puuid: x.puuid,
champion: x.championName,
kda: `${x.kills}/${x.deaths}/${x.assists}`,
resultat: x.win ? 'Victoire' : 'Defaite'
}));
let results = [];
for (const user of allUser) {
let nameLevel = await this.getNameLevelByUuid(user.puuid);
delete user.puuid;
results.push({...nameLevel, ...user});
}
return results;
}
async getAccountByName(name) {
let response = {};
try {
response = await axios.get(`${lolaccount}/lol/summoner/v4/summoners/by-name/${encodeURIComponent(name)}`, header);
} catch(e) {
console.log(e);
}
return response.data;
}
async getMatchByUuid(uuid) {
let response = {};
try {
response = await axios.get(`${lolmatch}/lol/match/v5/matches/by-puuid/${encodeURIComponent(uuid)}/ids`, header);
} catch(e) {
console.log(e);
}
return response.data;
}
async getInfoByMatchId(id) {
let response = {};
try {
response = await axios.get(`${lolmatch}/lol/match/v5/matches/${encodeURIComponent(id)}`, header);
} catch(e) {
console.log(e);
}
return response.data;
}
async getNameLevelByUuid(uuid) {
let response = {};
try {
response = await axios.get(`${lolaccount}/lol/summoner/v4/summoners/by-puuid/${encodeURIComponent(uuid)}`, header);
} catch(e) {
console.log(e);
}
return {name: response.data.name, level: response.data.summonerLevel};
}
}

@ -0,0 +1,23 @@
import express from 'express';
import Manager from './Manager.js';
const app = express();
const appManager = new Manager();
app.get('/api/lastmatch', async function(req, res) {
if (!req.query.name) {
return res.status(400).send();
}
try {
const results = await appManager.getInfo(req.query.name);
res.send(results);
} catch (e) {
console.error(e);
return res.status(503).send();
}
});
app.listen('80', async function() {
console.log(`server listening on 80`);
});

@ -0,0 +1,17 @@
{
"name": "node-cours",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.24.0",
"dotenv": "^10.0.0",
"express": "^4.17.1"
},
"type": "module"
}
Loading…
Cancel
Save