This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchData.js
70 lines (61 loc) · 1.83 KB
/
fetchData.js
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
69
70
const fetch = require("node-fetch");
const config = require("./config.js");
const URL_TEAMS = `${config.rcj_server_hostname}/schedule/json/teams.json`;
const URL_RUNS = `${config.rcj_server_hostname}/api/v1/get_runs`;
const COMPETITION_ID_ENTRY = `${config.competition}-line-entry`;
const COMPETITION_ID_LINE = `${config.competition}-line`;
exports.fetchAllData = function () {
return Promise.all([
fetchTeams(),
fetchRuns()
]);
};
let fetchTeams = function () {
return fetch(URL_TEAMS, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then((response) => response.json())
.then((json) => {
const teamsEntry = [], teamsLine = [];
for (const teamObj of json) {
if (teamObj.competition === "line-entry") {
teamsEntry.push(teamObj.name);
} else if (teamObj.competition === "line") {
teamsLine.push(teamObj.name);
}
}
return {
teamsEntry,
teamsLine
};
});
};
let fetchRuns = function () {
return fetch(URL_RUNS, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + encodeBase64(config.username + ":" + config.password)
}
})
.then((response) => response.json())
.then((json) => {
return {
runsEntry: json.runs.filter(filterRunsEntry),
runsLine: json.runs.filter(filterRunsLine)
};
});
};
let filterRunsEntry = function (run) {
return run.competition === COMPETITION_ID_ENTRY;
};
let filterRunsLine = function (run) {
return run.competition === COMPETITION_ID_LINE;
};
let encodeBase64 = function (text) {
return Buffer.from(text).toString('base64');
};