From f3ef7b65a8def03fa8083fcdea6f9bb2af8c9c91 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Wed, 2 Feb 2022 02:23:42 +0100 Subject: [PATCH] updated api requests from try/catch to axios handling --- package.json | 1 + src/constants/index.js | 3 + src/utils/ApiRequests.js | 365 +++++++++++++++++++++++++++------------ yarn.lock | 8 + 4 files changed, 264 insertions(+), 113 deletions(-) diff --git a/package.json b/package.json index 7897787..04dd77e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dotenv-webpack": "^7.1.0", "echarts": "^5.3.0", "fork-awesome": "^1.2.0", + "http-status-codes": "^2.2.0", "jquery": "^3.6.0", "luxon": "^2.3.0", "string-sanitizer": "^2.0.2", diff --git a/src/constants/index.js b/src/constants/index.js index 46d9807..a97bacd 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -21,3 +21,6 @@ export const GRENADES = { export const NAV_HEIGHT = 70 export const FOOTER_HEIGHT = 200 + +export const SHARECODE_REGEX = /^CSGO(?:-?[ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789]{5}){5}$/ +export const AUTHCODE_REGEX = /^[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{5}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}$/ diff --git a/src/utils/ApiRequests.js b/src/utils/ApiRequests.js index c9bba20..da05d60 100644 --- a/src/utils/ApiRequests.js +++ b/src/utils/ApiRequests.js @@ -1,171 +1,310 @@ import axios from "axios"; -import {errorHandling} from "@/utils/Utils"; +import {StatusCodes as STATUS} from "http-status-codes"; +import {AUTHCODE_REGEX, SHARECODE_REGEX} from "@/constants"; const API_URL = process.env.VUE_APP_API_URL -// const TIMEOUT = 10_000 export const GetMatches = async () => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/matches`, + let response = null + + await axios + .get(`${API_URL}/matches`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetMatches - bad request') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetMatches - internal server error') + break + default: + // TODO: ERROR + console.log('GetMatches - default error') + } }) - if (res.status === 200) - return res.data - } catch { - console.log('Could not load matches') - } + return response } export const GetUser = async (id) => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/player/${id}`, + let response = null + + await axios + .get(`${API_URL}/player/${id}`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetUser - bad request') + break + case STATUS.NOT_FOUND: + // TODO: ERROR + console.log('GetUser - not found') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetUser - internal server error') + break + default: + // TODO: ERROR + console.log('GetUser - default error') + } }) - if (res.status === 200) - return res.data - } catch { - console.log('Could not load user') - } + return response } export const TrackMe = async (id64, authcode, sharecode = '') => { - let statusError = '' + // TODO: NEEDS WORK!!!!! + + let statusError = null let status = 202 - const shareCodeRegex = /^CSGO(?:-?[ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789]{5}){5}$/ - const authCodeRegex = /^[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{5}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}$/ - - if (sharecode !== '' && !shareCodeRegex.test(sharecode)) { - statusError = 'Is not a valid sharecode' - status = 418 + if (sharecode !== '' && !SHARECODE_REGEX.test(sharecode)) { + statusError = 'Sharecode is invalid or missing' + status = STATUS.IM_A_TEAPOT return [status, statusError] } - if (authcode === '' || !authCodeRegex.test(authcode)) { - statusError = 'Is not a valid authcode' - status = 418 + if (authcode === '' || !AUTHCODE_REGEX.test(authcode)) { + statusError = 'Authcode is invalid or missing' + status = STATUS.IM_A_TEAPOT return [status, statusError] } - try { - // const res = await axios - // .post(`${API_URL}/player/${id64}/track`, `authcode=${authcode}&sharecode=${sharecode}`) - - // TODO: Needs testing - - const res = await axios({ - method: 'post', + await axios + .post({ url: `${API_URL}/player/${id64}/track`, - data: `authcode=${authcode}&sharecode=${sharecode}`, + data: `authcode=${authcode}&sharecode=${sharecode}` }) + .then((res) => { + if (res.status === STATUS.ACCEPTED) + status = res.status + }) + .catch((err) => { + status = err.response.status - if (res.status === 202) { - status = res.status - } - } catch (err) { - if (err.response.status === 401) { - statusError = 'Data does not match player' - status = err.response.status - } else if (err.response.status === 400) { - statusError = 'Userinput was wrong' - status = err.response.status - } else { - errorHandling(err.response.status) - } - } + switch (err.response.status) { + case STATUS.BAD_REQUEST: + statusError = 'Invalid arguments' + break + case STATUS.NOT_FOUND: + statusError = 'Player not found' + break + case STATUS.SERVICE_UNAVAILABLE: + statusError = 'Problem with request' + break + case STATUS.UNAUTHORIZED: + statusError = 'Authcode is invalid' + break + case STATUS.PRECONDITION_FAILED: + statusError = 'Sharecode is invalid or missing' + break + case STATUS.INTERNAL_SERVER_ERROR: + statusError = 'Service currently unavailable - please try again later' + break + default: + statusError = 'An unknown error occured' + console.log('TrackMe - An unknown error occured') + } + }) return [status, statusError] } export const GetPlayerValue = async (match_id) => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/match/${match_id}/rounds`, + let response = null + + await axios + .get(`${API_URL}/match/${match_id}/rounds`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetPlayerValue - bad request') + break + case STATUS.NOT_FOUND: + // TODO: ERROR + console.log('GetPlayerValue - not found') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetPlayerValue - internal server error') + break + default: + // TODO: ERROR + console.log('GetPlayerValue - default error') + } }) - if (res.status === 200) { - return res.data - } - } catch { - console.log('Could not get player value') - } + return response } export const GetMatchDetails = async (match_id) => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/match/${match_id}`, + let response = null + + await axios + .get(`${API_URL}/match/${match_id}`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetMatchDetails - bad request') + break + case STATUS.NOT_FOUND: + // TODO: ERROR + console.log('GetMatchDetails - not found') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetMatchDetails - internal server errror') + break + default: + // TODO: ERROR + console.log('GetMatchDetails - default error') + } }) - if (res.status === 200) { - return res.data - } - } catch { - console.log('Could not load match details') - } + return response } export const LoadMoreMatches = async (player_id, date) => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/player/${player_id}/next/${date}`, + let response = null + + await axios + .get(`${API_URL}/player/${player_id}/next/${date}`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetUser - bad request') + break + case STATUS.NOT_FOUND: + // TODO: ERROR + console.log('GetUser - not found') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetUser - internal server error') + break + default: + // TODO: ERROR + console.log('GetUser - default error') + } }) - if (res.status === 200) { - return res.data - } - } catch { - console.log('Could not load more matches') - } + return response } export const GetPlayerMeta = async (player_id, limit = 4) => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/player/${player_id}/meta/${limit}`, + let response = null + + await axios + .get(`${API_URL}/player/${player_id}/meta/${limit}`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetPlayerMeta - bad request') + break + case STATUS.NOT_FOUND: + // TODO: ERROR + console.log('GetPlayerMeta - not found') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetPlayerMeta - internal server error') + break + default: + // TODO: ERROR + console.log('GetPlayerMeta - default error') + } }) - if (res.status === 200) { - return res.data - } - } catch { - console.log('Could not load player metadata') - } + return response } export const GetWeaponDmg = async (match_id) => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/match/${match_id}/weapons`, + let response = null + + await axios + .get(`${API_URL}/match/${match_id}/weapons`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetWeaponDmg - bad request') + break + case STATUS.NOT_FOUND: + // TODO: ERROR + console.log('GetWeaponDmg - not found') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetWeaponDmg - internal server error') + break + default: + // TODO: ERROR + console.log('GetWeaponDmg - default error') + } }) - if (res.status === 200) { - return res.data - } - } catch { - console.log('Could not calculate weapon damage') - } + return response } export const LoadMoreMatchesExplore = async (date) => { - try { - const res = await axios({ - method: 'get', - url: `${API_URL}/matches/next/${date}`, + let response = null + + await axios + .get(`${API_URL}/matches/next/${date}`) + .then((res) => { + if (res.status === STATUS.OK) + response = res.data + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + // TODO: ERROR + console.log('GetMatches - bad request') + break + case STATUS.INTERNAL_SERVER_ERROR: + // TODO: ERROR + console.log('GetMatches - internal server error') + break + default: + // TODO: ERROR + console.log('GetMatches - default error') + } }) - if (res.status === 200) { - return res.data - } - } catch { - console.log('Could not load more matches') - } + return response } diff --git a/yarn.lock b/yarn.lock index a4aff3f..edc57ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4047,6 +4047,7 @@ __metadata: eslint: ^6.8.0 eslint-plugin-vue: ^7.20.0 fork-awesome: ^1.2.0 + http-status-codes: ^2.2.0 jquery: ^3.6.0 luxon: ^2.3.0 sass: ^1.49.0 @@ -6338,6 +6339,13 @@ fsevents@~2.3.2: languageName: node linkType: hard +"http-status-codes@npm:^2.2.0": + version: 2.2.0 + resolution: "http-status-codes@npm:2.2.0" + checksum: 31e1d730856210445da0907d9b484629e69e4fe92ac032478a7aa4d89e5b215e2b4e75d7ebce40d0537b6850bd281b2f65c7cc36cc2677e5de056d6cea1045ce + languageName: node + linkType: hard + "https-browserify@npm:^1.0.0": version: 1.0.0 resolution: "https-browserify@npm:1.0.0"