Skip to main content

mlb_api/requests/team/
history.rs

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