-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi.ts
68 lines (57 loc) · 1.92 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { TelemetryRequest, WcaKeyRequest, WcaModelIdRequest } from "./types";
import axios from "axios";
export const API_TELEMETRY_PATH = "/api/v1/telemetry/";
export const API_WCA_KEY_PATH = "/api/v1/wca/apikey/";
export const API_WCA_MODEL_ID_PATH = "/api/v1/wca/modelid/";
export const API_WCA_KEY_TEST_PATH = "/api/v1/wca/apikey/test/";
export const API_WCA_MODEL_ID_TEST_PATH = "/api/v1/wca/modelid/test/";
export const readCookie = (name: string): string | null => {
const nameEQ = name + "=";
const ca = document.cookie.split(";");
for (let c of ca) {
const cookie = c.trim();
if (cookie.startsWith(nameEQ)) {
return cookie.substring(nameEQ.length, cookie.length);
}
}
return null;
};
export const getWcaKey = () => {
return axios.get(API_WCA_KEY_PATH);
};
export const saveWcaKey = (wcaKey: WcaKeyRequest) => {
const csrfToken = readCookie("csrftoken");
return axios.post(API_WCA_KEY_PATH, wcaKey, {
headers: { "X-CSRFToken": csrfToken },
});
};
export const deleteWcaKey = () => {
const csrfToken = readCookie("csrftoken");
return axios.delete(API_WCA_KEY_PATH, {
headers: { "X-CSRFToken": csrfToken },
});
};
export const testWcaKey = () => {
return axios.get(API_WCA_KEY_TEST_PATH);
};
export const getWcaModelId = () => {
return axios.get(API_WCA_MODEL_ID_PATH);
};
export const saveWcaModelId = (wcaModelId: WcaModelIdRequest) => {
const csrfToken = readCookie("csrftoken");
return axios.post(API_WCA_MODEL_ID_PATH, wcaModelId, {
headers: { "X-CSRFToken": csrfToken },
});
};
export const testWcaModelId = () => {
return axios.get(API_WCA_MODEL_ID_TEST_PATH);
};
export const getTelemetrySettings = () => {
return axios.get(API_TELEMETRY_PATH);
};
export const saveTelemetrySettings = (telemetry: TelemetryRequest) => {
const csrfToken = readCookie("csrftoken");
return axios.post(API_TELEMETRY_PATH, telemetry, {
headers: { "X-CSRFToken": csrfToken },
});
};