mlb_api/endpoints/teams/team/leaders/
mod.rs

1use crate::endpoints::stats::leaders::StatLeaders;
2use crate::endpoints::teams::team::TeamId;
3use crate::endpoints::{BaseballStat, GameType, StatsAPIUrl};
4use crate::gen_params;
5use crate::types::{Copyright, PlayerPool};
6use itertools::Itertools;
7use serde::Deserialize;
8use std::fmt::{Display, Formatter};
9
10#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
11#[serde(rename_all = "camelCase")]
12pub struct TeamStatLeadersResponse {
13	pub copyright: Copyright,
14	pub team_leaders: Vec<StatLeaders>,
15}
16
17/// Stat leaders per team
18pub struct TeamStatLeadersEndpointUrl {
19	pub team_id: TeamId,
20	pub stats: Vec<BaseballStat>,
21	pub season: Option<u16>,
22	pub pool: PlayerPool,
23
24	/// [`None`] represents matching for all [`GameType`]s.
25	pub game_types: Option<Vec<GameType>>,
26}
27
28impl Display for TeamStatLeadersEndpointUrl {
29	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30		write!(
31			f,
32			"http://statsapi.mlb.com/api/v1/teams/{}/leaders{params}",
33			self.team_id,
34			params = gen_params! {
35				"leaderCategories": self.stats.iter().map(|stat| &stat.id).join(","),
36				"season"?: self.season,
37				"pool": self.pool,
38				"game_types"?: self.game_types.as_ref().map(|x| x.iter().join(",")),
39			}
40		)
41	}
42}
43
44impl StatsAPIUrl for TeamStatLeadersEndpointUrl {
45	type Response = TeamStatLeadersResponse;
46}
47
48#[cfg(test)]
49mod tests {
50	use crate::endpoints::meta::MetaEndpointUrl;
51	use crate::endpoints::sports::SportId;
52	use crate::endpoints::teams::team::leaders::TeamStatLeadersEndpointUrl;
53	use crate::endpoints::teams::TeamsEndpointUrl;
54	use crate::endpoints::{BaseballStat, StatsAPIUrl};
55
56	#[tokio::test]
57	async fn test_all_mlb_teams_all_stats() {
58		let all_categories = MetaEndpointUrl::<BaseballStat>::new().get().await.unwrap().entries;
59
60		for team in (TeamsEndpointUrl { sport_id: Some(SportId::MLB), season: None }).get().await.unwrap().teams {
61			let _all_stats = TeamStatLeadersEndpointUrl {
62				team_id: team.id,
63				stats: all_categories.clone(),
64				season: None,
65				pool: Default::default(),
66				game_types: None,
67			}
68			.get()
69			.await
70			.unwrap();
71		}
72	}
73}