Exemple API en TD : team sport

master
Juan 3 years ago
parent c2c9c88b5e
commit 1d19b7b04e

@ -0,0 +1,84 @@
import fetch from "node-fetch";
// API constants
const API_KEY = "0c659161aae51ff51a6c0c41c8a591b5";
const API_HOST = "v3.football.api-sports.io";
const ROUTE_LIST = {
LEAGUE: "league",
PLAYER_LIST: "player-list",
};
/**
* Get league list
*
* @returns - League list
*/
async function getLeague(leagueName) {
return await requestApi(ROUTE_LIST.LEAGUE, { leagueName });
}
/**
* Get player list
*
* @returns - League list
*/
async function getPlayerList(leagueId, seasonYear) {
return await requestApi(ROUTE_LIST.PLAYER_LIST, { leagueId, seasonYear });
}
/**
* Request api in fonction of route and params
*
* @param string route - Route requested
* @param object paramList - List of params used by route
*
* @returns - The API response
*/
async function requestApi(route, paramList) {
var promise = new Promise((resolve, reject) => {
const { headers, method, url } = getApiParamList(route, paramList);
fetch(url, { headers, method }).then((response) => {
response
.json()
.then((data) => (response.ok ? resolve(data) : reject(data)));
});
});
return await promise.then(
(data) => data.response,
(err) => console.log(err)
);
}
/**
* Get all params used by route
*
* @param string route - Route requested
* @param object paramList - List of params used by route
*
* @returns - List of params used to requested API (url, method, headers, apiKeys...)
*/
function getApiParamList(route, paramList) {
let httpOptions = {
headers: {
"x-rapidapi-host": API_HOST,
"x-rapidapi-key": API_KEY,
"Content-Type": "application/json",
},
};
switch (route) {
case ROUTE_LIST.PLAYER_LIST:
httpOptions.method = "GET";
httpOptions.url = `https://v3.football.api-sports.io/players?league=${paramList.leagueId}&season=${paramList.seasonYear}`;
break;
case ROUTE_LIST.LEAGUE:
httpOptions.method = "GET";
httpOptions.url = `https://v3.football.api-sports.io/leagues?name=${paramList.leagueName}`;
break;
}
return httpOptions;
}
export { getLeague, getPlayerList };

@ -0,0 +1,69 @@
import { getLeague, getPlayerList } from "./ApiUtils.js";
const HTTP_CODE = {
OK: 200,
BAD_REQUEST: 400,
};
const HTTP_VERB = {
GET: "GET",
POST: "POST",
PUT: "PUT",
PATCH: "PATCH",
DELETE: "DELETE",
};
/**
* Create HTTP json response
* @param int statusCode
* @param object object
* @param object response
*/
function jsonResponse(statusCode, object, response) {
response.statusCode = statusCode;
response.setHeader("Content-type", "Application/json");
response.end(JSON.stringify(object));
}
/**
* Provide HTTP response in function of method
*
* @param object request
* @param object response
*/
function httpResponseProvider(request, response) {
switch (request.method) {
case HTTP_VERB.POST:
case HTTP_VERB.PUT:
case HTTP_VERB.DELETE:
case HTTP_VERB.PATCH:
console.log(HTTP_VERB.POST + " not available");
jsonResponse(
HTTP_CODE.BAD_REQUEST,
{ error: HTTP_VERB.POST + " not available !" },
response
);
break;
case HTTP_VERB.GET:
getPlayerList(39, 2021)
.then((data) => data)
.then((data) => {
getLeague(data[0].statistics[0].league.name).then(
(data) => {
jsonResponse(HTTP_CODE.OK, { data }, response);
}
);
});
break;
default:
console.log(HTTP_VERB.POST + " verb unknown");
jsonResponse(
HTTP_CODE.BAD_REQUEST,
{ error: HTTP_VERB.POST + " verb unknown !" },
response
);
break;
}
}
export { httpResponseProvider };

@ -0,0 +1,18 @@
{
"type": "module",
"name": "api-meteo",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"nodemon": "./node_modules/.bin/nodemon ./server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^3.2.0",
"nodemon": "^2.0.15"
}
}

@ -0,0 +1,14 @@
import http from "http";
import { httpResponseProvider } from "./HttpUtils.js";
// HTTP constants
const HTTP_PORT = 8080;
/**
* Create web server
*/
http.createServer((request, response) => {
httpResponseProvider(request, response);
}).listen(HTTP_PORT);
Loading…
Cancel
Save