You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
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 }; |