tmdb_api/tvshow/
content_rating.rs

1//! https://developer.themoviedb.org/reference/tv-series-content-ratings
2
3use std::borrow::Cow;
4
5/// Command to get the content ratings of a TV show.
6///
7/// ```rust
8/// use tmdb_api::prelude::Command;
9/// use tmdb_api::Client;
10/// use tmdb_api::client::reqwest::ReqwestExecutor;
11/// use tmdb_api::tvshow::content_rating::TVShowContentRating;
12///
13/// #[tokio::main]
14/// async fn main() {
15///     let client = Client::<ReqwestExecutor>::new("this-is-my-secret-token".into());
16///     let cmd = TVShowContentRating::new(1);
17///     let result = cmd.execute(&client).await;
18///     match result {
19///         Ok(res) => println!("found: {res:#?}"),
20///         Err(err) => eprintln!("error: {err:?}"),
21///     };
22/// }
23/// ```
24#[derive(Clone, Debug, Default)]
25pub struct TVShowContentRating {
26    pub id: u64,
27}
28
29#[derive(Clone, Debug, Default, Deserialize)]
30pub struct ContentRatingResult {
31    pub id: u64,
32    pub results: Vec<ContentRating>,
33}
34
35#[derive(Clone, Debug, Default, Deserialize)]
36pub struct ContentRating {
37    pub descriptors: Vec<String>,
38    pub iso_3166_1: String,
39    pub rating: String,
40}
41
42impl TVShowContentRating {
43    pub fn new(tv_show_id: u64) -> Self {
44        Self { id: tv_show_id }
45    }
46}
47
48impl crate::prelude::Command for TVShowContentRating {
49    type Output = ContentRatingResult;
50
51    fn path(&self) -> Cow<'static, str> {
52        Cow::Owned(format!("/tv/{}/content_ratings", self.id))
53    }
54
55    fn params(&self) -> Vec<(&'static str, Cow<'_, str>)> {
56        Vec::new()
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use mockito::Matcher;
63
64    use crate::Client;
65    use crate::client::reqwest::ReqwestExecutor;
66    use crate::prelude::Command;
67
68    use super::TVShowContentRating;
69
70    #[tokio::test]
71    async fn it_works() {
72        let mut server = mockito::Server::new_async().await;
73        let client = Client::<ReqwestExecutor>::builder()
74            .with_api_key("secret".into())
75            .with_base_url(server.url())
76            .build()
77            .unwrap();
78
79        let _m = server
80            .mock("GET", "/tv/1399/content_ratings")
81            .match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
82            .with_status(200)
83            .with_header("content-type", "application/json")
84            .with_body(include_str!("../../assets/tv-content-ratings.json"))
85            .create_async()
86            .await;
87
88        let result = TVShowContentRating::new(1399)
89            .execute(&client)
90            .await
91            .unwrap();
92        assert_eq!(result.id, 1399);
93        assert!(!result.results.is_empty());
94    }
95
96    #[tokio::test]
97    async fn invalid_api_key() {
98        let mut server = mockito::Server::new_async().await;
99        let client = Client::<ReqwestExecutor>::builder()
100            .with_api_key("secret".into())
101            .with_base_url(server.url())
102            .build()
103            .unwrap();
104
105        let _m = server
106            .mock("GET", "/tv/1399/content_ratings")
107            .match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
108            .with_status(401)
109            .with_header("content-type", "application/json")
110            .with_body(include_str!("../../assets/invalid-api-key.json"))
111            .create_async()
112            .await;
113
114        let err = TVShowContentRating::new(1399)
115            .execute(&client)
116            .await
117            .unwrap_err();
118        let server_err = err.as_server_error().unwrap();
119        assert_eq!(server_err.status_code, 7);
120    }
121
122    #[tokio::test]
123    async fn resource_not_found() {
124        let mut server = mockito::Server::new_async().await;
125        let client = Client::<ReqwestExecutor>::builder()
126            .with_api_key("secret".into())
127            .with_base_url(server.url())
128            .build()
129            .unwrap();
130
131        let _m = server
132            .mock("GET", "/tv/1399/content_ratings")
133            .match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
134            .with_status(404)
135            .with_header("content-type", "application/json")
136            .with_body(include_str!("../../assets/resource-not-found.json"))
137            .create_async()
138            .await;
139
140        let err = TVShowContentRating::new(1399)
141            .execute(&client)
142            .await
143            .unwrap_err();
144        let server_err = err.as_server_error().unwrap();
145        assert_eq!(server_err.status_code, 34);
146    }
147}
148
149#[cfg(all(test, feature = "integration"))]
150mod integration_tests {
151    use crate::Client;
152    use crate::client::reqwest::ReqwestExecutor;
153    use crate::prelude::Command;
154
155    use super::TVShowContentRating;
156
157    #[tokio::test]
158    async fn execute() {
159        let secret = std::env::var("TMDB_TOKEN_V3").unwrap();
160        let client = Client::<ReqwestExecutor>::new(secret);
161
162        let result = TVShowContentRating::new(1399)
163            .execute(&client)
164            .await
165            .unwrap();
166        assert_eq!(result.id, 1399);
167        assert!(!result.results.is_empty());
168    }
169}