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
use crate::basketball_types::{Game, ListReturnValue};
use crate::helpers::{format_numbers_query_param_array, format_strings_query_param_array};
pub struct GamesQueryParams {
pub page: u32,
pub per_page: u32,
pub team_ids: Vec<u32>,
pub dates: Vec<String>,
pub seasons: Vec<u32>,
pub postseason: Option<bool>,
pub start_date: Option<String>,
pub end_date: Option<String>,
}
impl Default for GamesQueryParams {
fn default() -> Self {
GamesQueryParams {
page: 0,
per_page: 30,
team_ids: vec![],
dates: vec![],
seasons: vec![],
postseason: None,
start_date: None,
end_date: None,
}
}
}
#[tokio::main]
pub async fn get_games(
query_params: GamesQueryParams,
) -> Result<Vec<Game>, Box<dyn std::error::Error>> {
let mut query_params_list = vec![
("page".to_string(), query_params.page.to_string()),
("per_page".to_string(), query_params.per_page.to_string()),
];
if query_params.postseason.is_some() {
query_params_list.push((
"postseason".to_string(),
query_params.postseason.unwrap().to_string(),
))
}
if query_params.start_date.is_some() {
query_params_list.push((
"start_date".to_string(),
query_params.start_date.unwrap().to_string(),
))
}
if query_params.end_date.is_some() {
query_params_list.push((
"end_date".to_string(),
query_params.end_date.unwrap().to_string(),
))
}
query_params_list.append(&mut format_numbers_query_param_array(
"team_ids".to_string(),
&query_params.team_ids,
));
query_params_list.append(&mut format_numbers_query_param_array(
"seasons".to_string(),
&query_params.seasons,
));
query_params_list.append(&mut format_strings_query_param_array(
"dates".to_string(),
&query_params.dates,
));
let client = reqwest::Client::new();
let request_url = "https://www.balldontlie.io/api/v1/games";
let resp = client
.get(request_url)
.query(&query_params_list)
.send()
.await?
.json::<ListReturnValue<Game>>()
.await?;
Ok(resp.data)
}
#[tokio::main]
pub async fn get_game(game_id: u32) -> Result<Game, Box<dyn std::error::Error>> {
let request_url = format!(
"https://www.balldontlie.io/api/v1/games/{game_id}",
game_id = game_id
);
let resp = reqwest::get(request_url).await?.json::<Game>().await?;
Ok(resp)
}