tmdb_api/movie/
top_rated.rs

1use std::borrow::Cow;
2
3use crate::common::PaginatedResult;
4
5#[derive(Clone, Debug, Default, serde::Serialize)]
6pub struct Params<'a> {
7    /// ISO 639-1 value to display translated data for the fields that support it.
8    #[serde(skip_serializing_if = "Option::is_none")]
9    pub language: Option<Cow<'a, str>>,
10    /// Specify which page to query.
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub page: Option<u32>,
13    /// Specify a ISO 3166-1 code to filter release dates. Must be uppercase.
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub region: Option<Cow<'a, str>>,
16}
17
18impl<'a> Params<'a> {
19    pub fn set_page(&mut self, value: u32) {
20        self.page = Some(value);
21    }
22
23    pub fn with_page(mut self, value: u32) -> Self {
24        self.set_page(value);
25        self
26    }
27
28    pub fn set_language(&mut self, value: impl Into<Cow<'a, str>>) {
29        self.language = Some(value.into());
30    }
31
32    pub fn with_language(mut self, value: impl Into<Cow<'a, str>>) -> Self {
33        self.set_language(value);
34        self
35    }
36
37    pub fn set_region(&mut self, value: impl Into<Cow<'a, str>>) {
38        self.region = Some(value.into());
39    }
40
41    pub fn with_region(mut self, value: impl Into<Cow<'a, str>>) -> Self {
42        self.set_region(value);
43        self
44    }
45}
46
47impl<E: crate::client::Executor> crate::Client<E> {
48    /// Get a list of the current popular movies on TMDB. This list updates daily.
49    ///
50    /// ```rust
51    /// use tmdb_api::client::Client;
52    /// use tmdb_api::client::reqwest::Client as ReqwestClient;
53    ///
54    /// #[tokio::main]
55    /// async fn main() {
56    ///     let client = Client::<ReqwestClient>::new("this-is-my-secret-token".into());
57    ///     match client.list_movies_top_rated(&Default::default()).await {
58    ///         Ok(res) => println!("found: {:#?}", res),
59    ///         Err(err) => eprintln!("error: {:?}", err),
60    ///     };
61    /// }
62    /// ```
63    pub async fn list_movies_top_rated(
64        &self,
65        params: &Params<'_>,
66    ) -> crate::Result<PaginatedResult<super::MovieShort>> {
67        self.execute("/movie/top_rated", params).await
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use crate::client::Client;
74    use crate::client::reqwest::Client as ReqwestClient;
75    use mockito::Matcher;
76
77    #[tokio::test]
78    async fn it_works() {
79        let mut server = mockito::Server::new_async().await;
80        let client = Client::<ReqwestClient>::builder()
81            .with_api_key("secret".into())
82            .with_base_url(server.url())
83            .build()
84            .unwrap();
85
86        let _m = server
87            .mock("GET", "/movie/top_rated")
88            .match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
89            .with_status(200)
90            .with_header("content-type", "application/json")
91            .with_body(include_str!("../../assets/movie-top-rated.json"))
92            .create_async()
93            .await;
94
95        let result = client
96            .list_movies_top_rated(&Default::default())
97            .await
98            .unwrap();
99        assert_eq!(result.page, 1);
100    }
101
102    #[tokio::test]
103    async fn invalid_api_key() {
104        let mut server = mockito::Server::new_async().await;
105        let client = Client::<ReqwestClient>::builder()
106            .with_api_key("secret".into())
107            .with_base_url(server.url())
108            .build()
109            .unwrap();
110
111        let _m = server
112            .mock("GET", "/movie/top_rated")
113            .match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
114            .with_status(401)
115            .with_header("content-type", "application/json")
116            .with_body(include_str!("../../assets/invalid-api-key.json"))
117            .create_async()
118            .await;
119
120        let err = client
121            .list_movies_top_rated(&Default::default())
122            .await
123            .unwrap_err();
124        let server_err = err.as_server_error().unwrap();
125        assert_eq!(server_err.status_code, 7);
126    }
127
128    #[tokio::test]
129    async fn resource_not_found() {
130        let mut server = mockito::Server::new_async().await;
131        let client = Client::<ReqwestClient>::builder()
132            .with_api_key("secret".into())
133            .with_base_url(server.url())
134            .build()
135            .unwrap();
136
137        let _m = server
138            .mock("GET", "/movie/top_rated")
139            .match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
140            .with_status(404)
141            .with_header("content-type", "application/json")
142            .with_body(include_str!("../../assets/resource-not-found.json"))
143            .create_async()
144            .await;
145
146        let err = client
147            .list_movies_top_rated(&Default::default())
148            .await
149            .unwrap_err();
150        let server_err = err.as_server_error().unwrap();
151        assert_eq!(server_err.status_code, 34);
152    }
153}
154
155#[cfg(all(test, feature = "integration"))]
156mod integration_tests {
157    use crate::client::Client;
158    use crate::client::reqwest::Client as ReqwestClient;
159
160    #[tokio::test]
161    async fn execute() {
162        let secret = std::env::var("TMDB_TOKEN_V3").unwrap();
163        let client = Client::<ReqwestClient>::new(secret);
164
165        let _result = client
166            .list_movies_top_rated(&Default::default())
167            .await
168            .unwrap();
169    }
170}