mlb_api/endpoints/teams/affiliates/
mod.rs

1use crate::endpoints::teams::team::TeamId;
2use crate::endpoints::teams::TeamsResponse;
3use crate::endpoints::StatsAPIUrl;
4use crate::gen_params;
5use std::fmt::{Display, Formatter};
6
7pub struct TeamAffiliatesEndpointUrl {
8	pub id: TeamId,
9	pub season: Option<u16>,
10}
11
12impl Display for TeamAffiliatesEndpointUrl {
13	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14		write!(f, "http://statsapi.mlb.com/api/v1/teams/{}/affiliates{params}", self.id, params = gen_params! { "season"?: self.season })
15	}
16}
17
18impl StatsAPIUrl for TeamAffiliatesEndpointUrl {
19	type Response = TeamsResponse;
20}
21
22#[cfg(test)]
23mod tests {
24	use crate::endpoints::sports::SportId;
25	use crate::endpoints::teams::affiliates::TeamAffiliatesEndpointUrl;
26	use crate::endpoints::teams::TeamsEndpointUrl;
27	use crate::endpoints::StatsAPIUrl;
28	use crate::request::Error as EndpointError;
29	use chrono::{Datelike, Local};
30
31	#[tokio::test]
32	async fn all_mlb_teams() {
33		for team in (TeamsEndpointUrl { sport_id: Some(SportId::MLB), season: None }).get().await.unwrap().teams {
34			let _affiliates = TeamAffiliatesEndpointUrl { id: team.id, season: None }.get().await.unwrap();
35		}
36	}
37
38	#[tokio::test]
39	#[cfg_attr(not(feature = "_heavy_tests"), ignore)]
40	async fn all_mlb_teams_all_seasons() {
41		for season in 1876..=Local::now().year() as _ {
42			for team in (TeamsEndpointUrl { sport_id: Some(SportId::MLB), season: Some(season) }).get().await.unwrap().teams {
43				dbg!(team.id);
44				dbg!(&*team.try_as_named_ref().unwrap().name);
45				let affiliates_result = TeamAffiliatesEndpointUrl { id: team.id, season: Some(season) }.get().await;
46				match affiliates_result {
47					Ok(_) => {}
48					Err(EndpointError::StatsAPI(_)) => {},
49					Err(e) => panic!("{e}"),
50				}
51			}
52		}
53	}
54}