updated api requests from try/catch to axios handling
All checks were successful
CSGOWTF/csgowtf/pipeline/head This commit looks good

This commit is contained in:
2022-02-02 02:23:42 +01:00
parent 85828dd983
commit f3ef7b65a8
4 changed files with 264 additions and 113 deletions

View File

@@ -15,6 +15,7 @@
"dotenv-webpack": "^7.1.0", "dotenv-webpack": "^7.1.0",
"echarts": "^5.3.0", "echarts": "^5.3.0",
"fork-awesome": "^1.2.0", "fork-awesome": "^1.2.0",
"http-status-codes": "^2.2.0",
"jquery": "^3.6.0", "jquery": "^3.6.0",
"luxon": "^2.3.0", "luxon": "^2.3.0",
"string-sanitizer": "^2.0.2", "string-sanitizer": "^2.0.2",

View File

@@ -21,3 +21,6 @@ export const GRENADES = {
export const NAV_HEIGHT = 70 export const NAV_HEIGHT = 70
export const FOOTER_HEIGHT = 200 export const FOOTER_HEIGHT = 200
export const SHARECODE_REGEX = /^CSGO(?:-?[ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789]{5}){5}$/
export const AUTHCODE_REGEX = /^[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{5}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}$/

View File

@@ -1,171 +1,310 @@
import axios from "axios"; 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 API_URL = process.env.VUE_APP_API_URL
// const TIMEOUT = 10_000
export const GetMatches = async () => { export const GetMatches = async () => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/matches`, .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 response
return res.data
} catch {
console.log('Could not load matches')
}
} }
export const GetUser = async (id) => { export const GetUser = async (id) => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/player/${id}`, .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 response
return res.data
} catch {
console.log('Could not load user')
}
} }
export const TrackMe = async (id64, authcode, sharecode = '') => { export const TrackMe = async (id64, authcode, sharecode = '') => {
let statusError = '' // TODO: NEEDS WORK!!!!!
let statusError = null
let status = 202 let status = 202
const shareCodeRegex = /^CSGO(?:-?[ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789]{5}){5}$/ if (sharecode !== '' && !SHARECODE_REGEX.test(sharecode)) {
const authCodeRegex = /^[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{5}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}$/ statusError = 'Sharecode is invalid or missing'
status = STATUS.IM_A_TEAPOT
if (sharecode !== '' && !shareCodeRegex.test(sharecode)) {
statusError = 'Is not a valid sharecode'
status = 418
return [status, statusError] return [status, statusError]
} }
if (authcode === '' || !authCodeRegex.test(authcode)) { if (authcode === '' || !AUTHCODE_REGEX.test(authcode)) {
statusError = 'Is not a valid authcode' statusError = 'Authcode is invalid or missing'
status = 418 status = STATUS.IM_A_TEAPOT
return [status, statusError] return [status, statusError]
} }
try { await axios
// const res = await axios .post({
// .post(`${API_URL}/player/${id64}/track`, `authcode=${authcode}&sharecode=${sharecode}`)
// TODO: Needs testing
const res = await axios({
method: 'post',
url: `${API_URL}/player/${id64}/track`, 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) { switch (err.response.status) {
status = res.status case STATUS.BAD_REQUEST:
} statusError = 'Invalid arguments'
} catch (err) { break
if (err.response.status === 401) { case STATUS.NOT_FOUND:
statusError = 'Data does not match player' statusError = 'Player not found'
status = err.response.status break
} else if (err.response.status === 400) { case STATUS.SERVICE_UNAVAILABLE:
statusError = 'Userinput was wrong' statusError = 'Problem with request'
status = err.response.status break
} else { case STATUS.UNAUTHORIZED:
errorHandling(err.response.status) 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] return [status, statusError]
} }
export const GetPlayerValue = async (match_id) => { export const GetPlayerValue = async (match_id) => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/match/${match_id}/rounds`, .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 response
return res.data
}
} catch {
console.log('Could not get player value')
}
} }
export const GetMatchDetails = async (match_id) => { export const GetMatchDetails = async (match_id) => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/match/${match_id}`, .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 response
return res.data
}
} catch {
console.log('Could not load match details')
}
} }
export const LoadMoreMatches = async (player_id, date) => { export const LoadMoreMatches = async (player_id, date) => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/player/${player_id}/next/${date}`, .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 response
return res.data
}
} catch {
console.log('Could not load more matches')
}
} }
export const GetPlayerMeta = async (player_id, limit = 4) => { export const GetPlayerMeta = async (player_id, limit = 4) => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/player/${player_id}/meta/${limit}`, .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 response
return res.data
}
} catch {
console.log('Could not load player metadata')
}
} }
export const GetWeaponDmg = async (match_id) => { export const GetWeaponDmg = async (match_id) => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/match/${match_id}/weapons`, .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 response
return res.data
}
} catch {
console.log('Could not calculate weapon damage')
}
} }
export const LoadMoreMatchesExplore = async (date) => { export const LoadMoreMatchesExplore = async (date) => {
try { let response = null
const res = await axios({
method: 'get', await axios
url: `${API_URL}/matches/next/${date}`, .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 response
return res.data
}
} catch {
console.log('Could not load more matches')
}
} }

View File

@@ -4047,6 +4047,7 @@ __metadata:
eslint: ^6.8.0 eslint: ^6.8.0
eslint-plugin-vue: ^7.20.0 eslint-plugin-vue: ^7.20.0
fork-awesome: ^1.2.0 fork-awesome: ^1.2.0
http-status-codes: ^2.2.0
jquery: ^3.6.0 jquery: ^3.6.0
luxon: ^2.3.0 luxon: ^2.3.0
sass: ^1.49.0 sass: ^1.49.0
@@ -6338,6 +6339,13 @@ fsevents@~2.3.2:
languageName: node languageName: node
linkType: hard 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": "https-browserify@npm:^1.0.0":
version: 1.0.0 version: 1.0.0
resolution: "https-browserify@npm:1.0.0" resolution: "https-browserify@npm:1.0.0"