mlb_api/endpoints/teams/
mod.rs

1pub mod affiliates;
2pub mod history;
3pub mod stats;
4pub mod team;
5
6use crate::endpoints::StatsAPIUrl;
7use crate::endpoints::sports::SportId;
8use crate::endpoints::teams::team::Team;
9use crate::gen_params;
10use crate::types::Copyright;
11use serde::Deserialize;
12use std::fmt::{Display, Formatter};
13
14/// Hydrations:
15/// * `previousSchedule`
16/// * `nextSchedule`
17/// * `venue`
18/// * `springVenue`
19/// * `social`
20/// * `deviceProperties`
21/// * `game(promotions)`
22/// * `game(atBatPromotions)`
23/// * `game(tickets)`
24/// * `game(atBatTickets)`
25/// * `game(sponsorships)`
26/// * `league`
27/// * `person`
28/// * `sport`
29/// * `standings`
30/// * `division`
31/// * `xrefId`
32/// * `location`
33#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
34#[serde(rename_all = "camelCase")]
35pub struct TeamsResponse {
36	pub copyright: Copyright,
37	pub teams: Vec<Team>,
38}
39
40pub struct TeamsEndpointUrl {
41	pub sport_id: Option<SportId>,
42	pub season: Option<u16>,
43}
44
45impl Display for TeamsEndpointUrl {
46	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47		write!(f, "http://statsapi.mlb.com/api/v1/teams{}", gen_params! { "sportId"?: self.sport_id, "season"?: self.season })
48	}
49}
50
51impl StatsAPIUrl for TeamsEndpointUrl {
52	type Response = TeamsResponse;
53}
54
55#[cfg(test)]
56mod tests {
57	use super::*;
58	use chrono::{Datelike, Local};
59
60	#[tokio::test]
61	#[cfg_attr(not(feature = "_heavy_tests"), ignore)]
62	async fn parse_all_teams_all_seasons() {
63		// let json = reqwest::get(TeamsEndpointUrl { sport_id: None, season: Some(2009) }.to_string()).await.unwrap().bytes().await.unwrap();
64		// let mut de = serde_json::Deserializer::from_slice(&json);
65		// let _response: TeamsResponse = serde_path_to_error::deserialize(&mut de).unwrap();
66		for season in 1871..=Local::now().year() as _ {
67			let _response = TeamsEndpointUrl { sport_id: None, season: Some(season) }.get().await.unwrap();
68		}
69	}
70
71	#[tokio::test]
72	async fn parse_all_mlb_teams_this_season() {
73		let _response = TeamsEndpointUrl {
74			sport_id: Some(SportId::default()),
75			season: None,
76		}
77		.get()
78		.await
79		.unwrap();
80	}
81}