Add API code
This commit is contained in:
30
src/api/endpoints/Actions.tsx
Normal file
30
src/api/endpoints/Actions.tsx
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import {deleteData, postData, putData} from "../fetchApi.tsx";
|
||||||
|
import type IPlayer from "../types/IPlayer.ts";
|
||||||
|
|
||||||
|
export function AddPlayer(apiUri: string, steamId: bigint){
|
||||||
|
return putData(`${apiUri}/Actions/Player/${steamId}`) as Promise<IPlayer>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DeletePlayer(apiUri: string, steamId: bigint){
|
||||||
|
return deleteData(`${apiUri}/Actions/Player/${steamId}`) as Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function UpdatePlayerData(apiUri: string, steamId: bigint){
|
||||||
|
return postData(`${apiUri}/Actions/Update/Player/${steamId}/All`) as Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function UpdatePlayerInfo(apiUri: string, steamId: bigint){
|
||||||
|
return postData(`${apiUri}/Actions/Update/Player/${steamId}/Info`) as Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function UpdatePlayerOwnedGames(apiUri: string, steamId: bigint){
|
||||||
|
return postData(`${apiUri}/Actions/Update/Player/${steamId}/OwnedGames`) as Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function UpdatePlayerTimeTracked(apiUri: string, steamId: bigint){
|
||||||
|
return postData(`${apiUri}/Actions/Update/Player/${steamId}/TimeTracked`) as Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function UpdateAll(apiUri: string){
|
||||||
|
return postData(`${apiUri}/Actions/All`) as Promise<void>;
|
||||||
|
}
|
27
src/api/endpoints/Data.tsx
Normal file
27
src/api/endpoints/Data.tsx
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import {getData} from "../fetchApi.tsx";
|
||||||
|
import type IPlayer from "../types/IPlayer.ts";
|
||||||
|
import type IGame from "../types/IGame.ts";
|
||||||
|
|
||||||
|
export function GetPlayers(apiUri: string){
|
||||||
|
return getData(`${apiUri}/Data/Players`) as Promise<IPlayer[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetPlayer(apiUri: string, steamdId: bigint){
|
||||||
|
return getData(`${apiUri}/Data/Player/${steamdId}`) as Promise<IPlayer>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetGamesOfPlayer(apiUri: string, steamdId: bigint){
|
||||||
|
return getData(`${apiUri}/Data/Player/${steamdId}/Games`) as Promise<IGame[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetGames(apiUri: string){
|
||||||
|
return getData(`${apiUri}/Data/Games`) as Promise<IGame[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetGame(apiUri: string, appId: bigint){
|
||||||
|
return getData(`${apiUri}/Data/Game/${appId}`) as Promise<IGame>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetPlayersOfGame(apiUri: string, appId: bigint){
|
||||||
|
return getData(`${apiUri}/Data/Game/${appId}/Players`) as Promise<IPlayer[]>;
|
||||||
|
}
|
18
src/api/endpoints/TimeTrack.tsx
Normal file
18
src/api/endpoints/TimeTrack.tsx
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import {getData} from "../fetchApi.tsx";
|
||||||
|
import type ITrackedTime from "../types/ITrackedTime.ts";
|
||||||
|
|
||||||
|
export function GetTimelines(apiUri: string, steamId: bigint){
|
||||||
|
return getData(`${apiUri}/TimeTrack/${steamId}`) as Promise<Map<bigint, ITrackedTime[]>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetTimelineGame(apiUri: string, steamId: bigint, appId: bigint){
|
||||||
|
return getData(`${apiUri}/TimeTrack/${steamId}/${appId}`) as Promise<ITrackedTime[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetTotal(apiUri: string, steamId: bigint){
|
||||||
|
return getData(`${apiUri}/TimeTrack/${steamId}/Total`) as unknown as Promise<bigint>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetTotalPerGame(apiUri: string, steamId: bigint){
|
||||||
|
return getData(`${apiUri}/TimeTrack/${steamId}/Total/PerGame`) as Promise<Map<bigint, bigint>>;
|
||||||
|
}
|
89
src/api/fetchApi.tsx
Normal file
89
src/api/fetchApi.tsx
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import {createContext} from "react";
|
||||||
|
|
||||||
|
export const ApiUriContext = createContext<string>("");
|
||||||
|
|
||||||
|
export function getData(uri: string) : Promise<object | undefined> {
|
||||||
|
return makeRequestWrapper("GET", uri, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function postData(uri: string, content?: object | string | number | boolean | null) : Promise<object | undefined> {
|
||||||
|
return makeRequestWrapper("POST", uri, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deleteData(uri: string) : Promise<void> {
|
||||||
|
return makeRequestWrapper("DELETE", uri, null) as Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function patchData(uri: string, content: object | string | number | boolean) : Promise<object | undefined> {
|
||||||
|
return makeRequestWrapper("patch", uri, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function putData(uri: string, content?: object | string | number | boolean | null) : Promise<object | undefined> {
|
||||||
|
return makeRequestWrapper("PUT", uri, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeRequestWrapper(method: string, uri: string, content?: object | string | number | null | boolean) : Promise<object | undefined>{
|
||||||
|
return makeRequest(method, uri, content)
|
||||||
|
.then((result) => result as Promise<object>)
|
||||||
|
.catch((e) => {
|
||||||
|
console.warn(e);
|
||||||
|
return Promise.resolve(undefined);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentlyRequestedEndpoints: string[] = [];
|
||||||
|
function makeRequest(method: string, uri: string, content?: object | string | number | null | boolean) : Promise<object | void> {
|
||||||
|
const id = method + uri;
|
||||||
|
if(currentlyRequestedEndpoints.find(x => x == id) != undefined)
|
||||||
|
return Promise.reject(`Already requested: ${method} ${uri}`);
|
||||||
|
currentlyRequestedEndpoints.push(id);
|
||||||
|
return fetch(uri,
|
||||||
|
{
|
||||||
|
method: method,
|
||||||
|
headers : {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
},
|
||||||
|
body: content ? JSON.stringify(content) : null
|
||||||
|
})
|
||||||
|
.then(function(response){
|
||||||
|
if(!response.ok){
|
||||||
|
if(response.status === 503){
|
||||||
|
currentlyRequestedEndpoints.splice(currentlyRequestedEndpoints.indexOf(id), 1)
|
||||||
|
let retryHeaderVal = response.headers.get("Retry-After");
|
||||||
|
let seconds = 10;
|
||||||
|
if(retryHeaderVal === null){
|
||||||
|
return response.text().then(text => {
|
||||||
|
seconds = parseInt(text);
|
||||||
|
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
|
||||||
|
.then(() => {
|
||||||
|
return makeRequest(method, uri, content);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}else {
|
||||||
|
seconds = parseInt(retryHeaderVal);
|
||||||
|
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
|
||||||
|
.then(() => {
|
||||||
|
return makeRequest(method, uri, content);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}else
|
||||||
|
throw new Error(response.statusText);
|
||||||
|
}
|
||||||
|
let json = response.json();
|
||||||
|
return json.then((json) => json).catch(() => null);
|
||||||
|
})
|
||||||
|
.catch(function(err : Error){
|
||||||
|
console.error(`Error ${method}ing Data ${uri}\n${err}`);
|
||||||
|
return Promise.reject();
|
||||||
|
}).finally(() => currentlyRequestedEndpoints.splice(currentlyRequestedEndpoints.indexOf(id), 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isValidUri(uri: string) : boolean{
|
||||||
|
try {
|
||||||
|
new URL(uri);
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
4
src/api/types/IGame.ts
Normal file
4
src/api/types/IGame.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export default interface IGame {
|
||||||
|
appId: bigint,
|
||||||
|
name: string
|
||||||
|
}
|
7
src/api/types/IPlayer.ts
Normal file
7
src/api/types/IPlayer.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export default interface IPlayer {
|
||||||
|
steamId: bigint,
|
||||||
|
name: string,
|
||||||
|
profileUrl: string,
|
||||||
|
avatarUrl: string,
|
||||||
|
updatedAt: Date
|
||||||
|
}
|
4
src/api/types/ITrackedTime.ts
Normal file
4
src/api/types/ITrackedTime.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export default interface ITrackedTime {
|
||||||
|
timeStamp: Date,
|
||||||
|
timePlayed: bigint
|
||||||
|
}
|
Reference in New Issue
Block a user