-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIInterface.js
More file actions
124 lines (109 loc) · 3.52 KB
/
Copy pathAPIInterface.js
File metadata and controls
124 lines (109 loc) · 3.52 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
async function requestAPI(url, apiKey) {
try {
const response = await fetch(url, {
headers: { "X-TBA-Auth-Key": apiKey },
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
return data; // Return the data directly
} catch (error) {
console.error(`${url} had a problem.`, error);
throw error; // Re-throw the error to propagate it to the caller
}
}
function resetStats() {
team_rank_element.textContent = "-";
team_opr_element.textContent = "-";
team_epa_element.textContent = "-";
}
function addEventsToList(event) {
new_option = option_template.cloneNode();
new_option.id = index;
new_option.textContent = event.name;
new_option.value = event.key;
team_event_picker.appendChild(new_option);
index += 1;
}
// Function to fetch team information from Blue Alliance API
async function fetchTeamInfo() {
scouting_data = await updateScoutingData();
scouting_data = getTeamScoutData(team_number);
setup_graph(scouting_data);
// API endpoint for team information
const team_info_url = `https://www.thebluealliance.com/api/v3/team/frc${team_number}`;
const team_events_url = `https://www.thebluealliance.com/api/v3/team/frc${team_number}/events/${year}`;
resetStats();
try {
current_data = await requestAPI(team_info_url, APIKey);
} catch (error) {
console.error(error);
team_name_element.textContent = "N/A";
}
if (!current_data) {
team_name_element.textContent = "N/A";
} else {
team_name_element.innerHTML = current_data.nickname;
}
try {
current_data = await requestAPI(team_events_url, APIKey);
} catch (error) {
console.error(error);
team_event_element.textContent = "N/A";
}
if (!current_data) {
team_event_picker.textContent = "N/A";
} else {
option_template = document.getElementsByTagName("option")[0].cloneNode();
while (team_event_picker.firstChild) {
team_event_picker.firstChild.remove();
}
option_template.textContent = "Pick an Event";
team_event_picker.appendChild(option_template);
current_data.forEach(addEventsToList);
}
}
async function fetchEventInfo() {
// API endpoint for team information
const event_opr_url = `https://www.thebluealliance.com/api/v3/event/${this.value}/oprs`;
const event_rank_url = `https://www.thebluealliance.com/api/v3/event/${this.value}/rankings`;
const event_epa_url = `https://api.statbotics.io/v3/team_event/${team_number}/${this.value}`;
resetStats();
try {
current_data = await requestAPI(event_rank_url, APIKey);
} catch (error) {
console.error(error);
team_rank_element.textContent = "N/A";
}
if (!current_data) {
team_rank_element.textContent = "N/A";
} else {
ranking = current_data.rankings.find(
(rank) => rank.team_key === `frc${team_number}`,
).rank;
team_rank_element.textContent = `${ranking}`;
}
try {
current_data = await requestAPI(event_opr_url, APIKey);
} catch (error) {
console.error(error);
team_opr_element.textContent = "N/A";
}
if (!current_data) {
team_opr_element.textContent = "N/A";
} else {
team_opr_element.textContent = `${Number(current_data.oprs[`frc${team_number}`].toFixed(2))}`;
}
try {
current_data = await requestAPI(event_epa_url, APIKey);
} catch (error) {
console.error(error);
team_epa_element.textContent = "N/A";
}
if (!current_data) {
team_epa_element.textContent = "N/A";
} else {
team_epa_element.textContent = `${current_data.epa.stats.mean}`;
}
}