Skip to main content

mlb_api/requests/team/
history.rs

1//! The history of a team's ID, such as the Los Angeles Dodgers and the Brooklyn Dodgers.
2
3use crate::season::SeasonId;
4use crate::team::TeamId;
5use crate::team::TeamsResponse;
6use crate::request::RequestURL;
7use bon::Builder;
8use std::fmt::{Display, Formatter};
9
10/// History of a [`TeamId`] throughout the years.
11/// For example, the team history of the Los Angeles Dodgers would include those of the Brooklyn Dodgers.
12#[derive(Builder)]
13#[builder(derive(Into))]
14pub struct TeamHistoryRequest {
15	#[builder(into)]
16	team_id: TeamId,
17	#[builder(into)]
18	start_season: Option<SeasonId>,
19	#[builder(into)]
20	end_season: Option<SeasonId>,
21}
22
23impl<S: team_history_request_builder::State + team_history_request_builder::IsComplete> crate::request::RequestURLBuilderExt for TeamHistoryRequestBuilder<S> {
24	type Built = TeamHistoryRequest;
25}
26
27impl Display for TeamHistoryRequest {
28	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29		write!(f, "http://statsapi.mlb.com/api/v1/teams/{}/history{params}", self.team_id, params = gen_params! { "startSeason"?: self.start_season, "endSeason"?: self.end_season })
30	}
31}
32
33impl RequestURL for TeamHistoryRequest {
34	type Response = TeamsResponse<()>;
35}
36
37#[cfg(test)]
38mod tests {
39	use crate::team::history::TeamHistoryRequest;
40	use crate::team::TeamsRequest;
41	use crate::request::RequestURLBuilderExt;
42
43	#[tokio::test]
44	async fn all_mlb_teams() {
45		for team in TeamsRequest::mlb_teams().build_and_get().await.unwrap().teams {
46			let _history = TeamHistoryRequest::builder().team_id(team.id).build_and_get().await.unwrap();
47		}
48	}
49}