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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::query::{
DivisionMatchesQuery, DivisionRankingsQuery, EventAwardsQuery, EventSkillsQuery,
EventTeamsQuery, SeasonEventsQuery, TeamAwardsQuery, TeamEventsQuery, TeamMatchesQuery,
TeamRankingsQuery, TeamSkillsQuery,
};
use super::{
query::{EventsQuery, SeasonsQuery, TeamsQuery},
schema::*,
};
use std::time::Duration;
#[derive(Default, Debug, Clone)]
pub struct RobotEvents {
pub bearer_token: String,
pub req_client: reqwest::Client,
}
pub const V1_API_BASE: &str = "https://www.robotevents.com/api";
pub const V2_API_BASE: &str = "https://www.robotevents.com/api/v2";
impl RobotEvents {
/// Creates a new RobotEvents API client.
///
/// A bearer authentication token is required for requests to be made. This can
/// be obtained from RobotEvents by creating an account and requesting one.
///
/// # Examples
///
/// Creating a client with a token stored as an enviornment variable:
///
/// ```
/// use robotevents::RobotEvents;
///
/// let token = std::env::var("ROBOTEVENTS_TOKEN")?;
/// let client = RobotEvents::new(token);
/// ```
pub fn new(bearer_token: impl AsRef<str>) -> Self {
Self {
bearer_token: bearer_token.as_ref().to_owned(),
req_client: reqwest::Client::new(),
}
}
/// Make a request to a [RobotEvents API v2](https://www.robotevents.com/api/v2) endpoint using the
/// client's bearer token.
pub async fn request(
&self,
endpoint: impl AsRef<str>,
) -> Result<reqwest::Response, reqwest::Error> {
Ok(self
.req_client
.get(format!("{V2_API_BASE}{}", endpoint.as_ref()))
.bearer_auth(&self.bearer_token)
.timeout(Duration::from_secs(10))
.send()
.await?)
}
/// Make a request to a RobotEvents API v1 endpoint.
pub async fn request_api_v1(
&self,
endpoint: impl AsRef<str>,
) -> Result<reqwest::Response, reqwest::Error> {
Ok(self
.req_client
.get(format!("{V1_API_BASE}{}", endpoint.as_ref()))
.timeout(Duration::from_secs(10))
.send()
.await?)
}
/////////////////////////////////////////////////////////////////////////
// Team-related endpoint methods
/////////////////////////////////////////////////////////////////////////
/// Get a paginated list of [`Team`]s from RobotEvents.
///
/// Team listings can be queryed using a [`TeamsQuery`] search.
pub async fn teams(
&self,
query: TeamsQuery,
) -> Result<PaginatedResponse<Team>, reqwest::Error> {
Ok(self
.request(format!("/teams{query}"))
.await?
.json()
.await?)
}
/// Get a specific RobotEvents [`Team`] by ID.
pub async fn team(&self, team_id: i32) -> Result<Team, reqwest::Error> {
Ok(self
.request(format!("/teams/{team_id}"))
.await?
.json()
.await?)
}
/// Gets a List of [`Event`]s that a given Team ID has attended.
pub async fn team_events(
&self,
team_id: i32,
query: TeamEventsQuery,
) -> Result<PaginatedResponse<Event>, reqwest::Error> {
Ok(self
.request(format!("/teams/{team_id}/events{query}"))
.await?
.json()
.await?)
}
/// Gets a List of [`Match`]es that a given Team ID has played in.
pub async fn team_matches(
&self,
team_id: i32,
query: TeamMatchesQuery,
) -> Result<PaginatedResponse<Match>, reqwest::Error> {
Ok(self
.request(format!("/teams/{team_id}/matches{query}"))
.await?
.json()
.await?)
}
/// Gets a List of [`Ranking`]s that a given Team ID has played in.
pub async fn team_rankings(
&self,
team_id: i32,
query: TeamRankingsQuery,
) -> Result<PaginatedResponse<Ranking>, reqwest::Error> {
Ok(self
.request(format!("/teams/{team_id}/rankings{query}"))
.await?
.json()
.await?)
}
/// Gets a List of [`Skill`]s runs that a given Team ID has performed.
pub async fn team_skills(
&self,
team_id: i32,
query: TeamSkillsQuery,
) -> Result<PaginatedResponse<Skill>, reqwest::Error> {
Ok(self
.request(format!("/teams/{team_id}/skills{query}"))
.await?
.json()
.await?)
}
/// Gets a List of [`Award`]s that a given Team ID has received.
pub async fn team_awards(
&self,
team_id: i32,
query: TeamAwardsQuery,
) -> Result<PaginatedResponse<Award>, reqwest::Error> {
Ok(self
.request(format!("/teams/{team_id}/awards{query}"))
.await?
.json()
.await?)
}
/////////////////////////////////////////////////////////////////////////
// Season-related endpoint methods
/////////////////////////////////////////////////////////////////////////
/// Get a paginated list of [`Season`]s from RobotEvents.
///
/// Season listings can be queryed using a [`SeasonQuery`] search.
pub async fn seasons(
&self,
query: SeasonsQuery,
) -> Result<PaginatedResponse<Season>, reqwest::Error> {
Ok(self
.request(format!("/seasons{query}"))
.await?
.json()
.await?)
}
/// Get a specific RobotEvents [`Season`] by ID.
pub async fn season(&self, season_id: i32) -> Result<Season, reqwest::Error> {
Ok(self
.request(format!("/seasons/{season_id}"))
.await?
.json()
.await?)
}
/// Gets a List of Events for a given Season.
pub async fn season_events(
&self,
season_id: i32,
query: SeasonEventsQuery,
) -> Result<PaginatedResponse<Event>, reqwest::Error> {
Ok(self
.request(format!("/seasons/{season_id}/events{query}"))
.await?
.json()
.await?)
}
/////////////////////////////////////////////////////////////////////////
// Program-related endpoint methods
/////////////////////////////////////////////////////////////////////////
/// Get a paginated list of all programs from RobotEvents.
pub async fn programs(&self) -> Result<PaginatedResponse<IdInfo>, reqwest::Error> {
Ok(self.request("/programs").await?.json().await?)
}
/// Get a specific RobotEvents program by ID.
pub async fn program(&self, program_id: i32) -> Result<IdInfo, reqwest::Error> {
Ok(self
.request(format!("/programs/{program_id}"))
.await?
.json()
.await?)
}
/////////////////////////////////////////////////////////////////////////
// Event-related endpoint methods
/////////////////////////////////////////////////////////////////////////
/// Get a paginated list of [`Event`]s from RobotEvents.
///
/// Event listings can be queryed using an [`EventQuery`] search.
pub async fn events(
&self,
query: EventsQuery,
) -> Result<PaginatedResponse<Event>, reqwest::Error> {
Ok(self
.request(format!("/events{query}"))
.await?
.json()
.await?)
}
/// Get a specific RobotEvents event by ID.
pub async fn event(&self, event_id: i32) -> Result<Event, reqwest::Error> {
Ok(self
.request(format!("/events/{event_id}"))
.await?
.json()
.await?)
}
/// Get a paginated list of teams attending an event.
pub async fn event_teams(
&self,
event_id: i32,
query: EventTeamsQuery,
) -> Result<PaginatedResponse<Team>, reqwest::Error> {
Ok(self
.request(format!("/events/{event_id}/teams{query}"))
.await?
.json()
.await?)
}
/// Get a paginated list of skills runs at an event.
pub async fn event_skills(
&self,
event_id: i32,
query: EventSkillsQuery,
) -> Result<PaginatedResponse<Skill>, reqwest::Error> {
Ok(self
.request(format!("/events/{event_id}/skills{query}"))
.await?
.json()
.await?)
}
/// Get a paginated list of skills runs at an event.
pub async fn event_awards(
&self,
event_id: i32,
query: EventAwardsQuery,
) -> Result<PaginatedResponse<Award>, reqwest::Error> {
Ok(self
.request(format!("/events/{event_id}/awards{query}"))
.await?
.json()
.await?)
}
/// Gets a List of Matches for a single Division of an Event.
pub async fn event_division_matches(
&self,
event_id: i32,
division_id: i32,
query: DivisionMatchesQuery,
) -> Result<PaginatedResponse<Match>, reqwest::Error> {
Ok(self
.request(format!(
"/events/{event_id}/divisions/{division_id}/matches{query}"
))
.await?
.json()
.await?)
}
/// Gets a List of Finalist Rankings for a single Division of an Event.
pub async fn event_division_finalist_rankings(
&self,
event_id: i32,
division_id: i32,
query: DivisionRankingsQuery,
) -> Result<PaginatedResponse<Ranking>, reqwest::Error> {
Ok(self
.request(format!(
"/events/{event_id}/divisions/{division_id}/finalistRankings{query}"
))
.await?
.json()
.await?)
}
/// Gets a List of Rankings for a single Division of an Event.
pub async fn event_division_rankings(
&self,
event_id: i32,
division_id: i32,
query: DivisionRankingsQuery,
) -> Result<PaginatedResponse<Ranking>, reqwest::Error> {
Ok(self
.request(format!(
"/events/{event_id}/divisions/{division_id}/finalist{query}"
))
.await?
.json()
.await?)
}
}