tmdb_api/movie/
upcoming.rs

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