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

1
2use crate::endpoints::StatsAPIUrl;
3use crate::endpoints::teams::team::TeamId;
4use crate::gen_params;
5use std::fmt::{Display, Formatter};
6use chrono::NaiveDate;
7use crate::endpoints::people::PeopleResponse;
8use crate::types::MLB_API_DATE_FORMAT;
9
10pub struct PersonnelEndpointUrl {
11    team_id: TeamId,
12    season: Option<u16>,
13    date: Option<NaiveDate>,
14}
15
16impl Display for PersonnelEndpointUrl {
17    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
18        write!(f, "http://statsapi.mlb.com/api/v1/teams/{}/personnel{}", self.team_id, gen_params! { "season"?: self.season, "date"?: self.date.as_ref().map(|date| date.format(MLB_API_DATE_FORMAT)) })
19    }
20}
21
22impl StatsAPIUrl for PersonnelEndpointUrl {
23    type Response = PeopleResponse;
24}
25
26#[cfg(test)]
27mod tests {
28    use crate::endpoints::StatsAPIUrl;
29    use crate::endpoints::teams::TeamsEndpointUrl;
30    use crate::endpoints::teams::team::personnel::PersonnelEndpointUrl;
31    use chrono::{Datelike, Local};
32
33    #[tokio::test]
34    #[cfg_attr(not(feature = "_heavy_tests"), ignore)]
35    async fn test_heavy() {
36        let season = Local::now().year() as _;
37        let teams = TeamsEndpointUrl { sport_id: None, season: Some(season) }.get().await.unwrap();
38        for team in teams.teams {
39            let json = reqwest::get(PersonnelEndpointUrl { team_id: team.id, season: Some(season), date: None }.to_string()).await.unwrap().bytes().await.unwrap();
40            let mut de = serde_json::Deserializer::from_slice(&json);
41            let result: Result<<PersonnelEndpointUrl as StatsAPIUrl>::Response, serde_path_to_error::Error<_>> = serde_path_to_error::deserialize(&mut de);
42            match result {
43                Ok(_) => {}
44                Err(e) if format!("{:?}", e.inner()).contains("missing field `copyright`") => {}
45                Err(e) => panic!("Err: {:?}", e),
46            }
47        }
48    }
49}