mlb_api/endpoints/seasons/
mod.rs1pub mod season;
2
3use crate::endpoints::StatsAPIUrl;
4use crate::endpoints::seasons::season::Season;
5use crate::endpoints::sports::SportId;
6use crate::gen_params;
7use crate::types::Copyright;
8use serde::Deserialize;
9use std::fmt::{Display, Formatter};
10
11#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
12#[serde(rename_all = "camelCase")]
13pub struct SeasonsResponse {
14 pub copyright: Copyright,
15 pub seasons: Vec<Season>,
16}
17
18pub struct SeasonsEndpointUrl {
19 sport_id: SportId,
20 season: Option<u16>,
21}
22
23impl Display for SeasonsEndpointUrl {
24 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25 write!(f, "http://statsapi.mlb.com/api/v1/seasons{}", gen_params! { "sportId": self.sport_id, "season"?: self.season })
26 }
27}
28
29impl StatsAPIUrl for SeasonsEndpointUrl {
30 type Response = SeasonsResponse;
31}
32
33#[cfg(test)]
34mod tests {
35 use crate::endpoints::StatsAPIUrl;
36 use crate::endpoints::seasons::SeasonsEndpointUrl;
37 use crate::endpoints::sports::{SportId, SportsEndpointUrl};
38 use chrono::{Datelike, Local};
39
40 #[tokio::test]
41 #[cfg_attr(not(feature = "_heavy_tests"), ignore)]
42 async fn parses_all_seasons() {
43 let all_sport_ids = SportsEndpointUrl { id: None }.get().await.unwrap().sports.into_iter().map(|sport| sport.id).collect::<Vec<_>>();
44
45 for season in 1871..=Local::now().year() as _ {
46 for id in all_sport_ids.iter().copied() {
47 let _response = SeasonsEndpointUrl { sport_id: id, season: Some(season) }.get().await.unwrap();
48 }
49 }
50 }
51
52 #[tokio::test]
53 async fn parse_this_season_mlb() {
54 let _response = SeasonsEndpointUrl { sport_id: SportId::default(), season: None }.get().await.unwrap();
55 }
56}