tmdb_api/movie/
translations.rs

1use std::borrow::Cow;
2
3/// Get a list of translations that have been created for a movie.
4///
5/// ```rust
6/// use tmdb_api::prelude::Command;
7/// use tmdb_api::client::Client;
8/// use tmdb_api::client::reqwest::ReqwestExecutor;
9/// use tmdb_api::movie::translations::MovieTranslations;
10///
11/// #[tokio::main]
12/// async fn main() {
13///     let client = Client::<ReqwestExecutor>::new("this-is-my-secret-token".into());
14///     let cmd = MovieTranslations::new(1);
15///     let result = cmd.execute(&client).await;
16///     match result {
17///         Ok(res) => println!("found: {:#?}", res),
18///         Err(err) => eprintln!("error: {:?}", err),
19///     };
20/// }
21/// ```
22#[derive(Clone, Debug, Default)]
23pub struct MovieTranslations {
24    /// ID of the movie.
25    pub movie_id: u64,
26}
27
28impl MovieTranslations {
29    pub fn new(movie_id: u64) -> Self {
30        Self { movie_id }
31    }
32}
33
34#[derive(Debug, Deserialize, Serialize)]
35pub struct TranslationData {
36    #[serde(deserialize_with = "crate::util::empty_string::deserialize")]
37    pub title: Option<String>,
38    #[serde(deserialize_with = "crate::util::empty_string::deserialize")]
39    pub overview: Option<String>,
40    #[serde(deserialize_with = "crate::util::empty_string::deserialize")]
41    pub homepage: Option<String>,
42}
43
44#[derive(Debug, Deserialize, Serialize)]
45pub struct Translation {
46    pub iso_3166_1: String,
47    pub iso_639_1: String,
48    pub name: String,
49    pub english_name: String,
50    pub data: TranslationData,
51}
52
53#[derive(Debug, Deserialize, Serialize)]
54pub struct MovieTranslationsResult {
55    pub id: u64,
56    pub translations: Vec<Translation>,
57}
58
59impl crate::prelude::Command for MovieTranslations {
60    type Output = MovieTranslationsResult;
61
62    fn path(&self) -> Cow<'static, str> {
63        Cow::Owned(format!("/movie/{}/translations", self.movie_id))
64    }
65
66    fn params(&self) -> Vec<(&'static str, Cow<'_, str>)> {
67        Vec::new()
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::MovieTranslations;
74    use crate::client::Client;
75    use crate::client::reqwest::ReqwestExecutor;
76    use crate::prelude::Command;
77    use mockito::Matcher;
78
79    #[tokio::test]
80    async fn it_works() {
81        let mut server = mockito::Server::new_async().await;
82        let client = Client::<ReqwestExecutor>::builder()
83            .with_api_key("secret".into())
84            .with_base_url(server.url())
85            .build()
86            .unwrap();
87
88        let _m = server
89            .mock("GET", "/movie/550/translations")
90            .match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
91            .with_status(200)
92            .with_header("content-type", "application/json")
93            .with_body(include_str!("../../assets/movie-translations.json"))
94            .create_async()
95            .await;
96
97        let result = MovieTranslations::new(550).execute(&client).await.unwrap();
98        assert_eq!(result.id, 550);
99        assert!(!result.translations.is_empty());
100    }
101
102    #[tokio::test]
103    async fn invalid_api_key() {
104        let mut server = mockito::Server::new_async().await;
105        let client = Client::<ReqwestExecutor>::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/550/translations")
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 = MovieTranslations::new(550)
121            .execute(&client)
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::<ReqwestExecutor>::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/550/translations")
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 = MovieTranslations::new(550)
147            .execute(&client)
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 super::MovieTranslations;
158    use crate::client::Client;
159    use crate::client::reqwest::ReqwestExecutor;
160    use crate::prelude::Command;
161
162    #[tokio::test]
163    async fn execute() {
164        let secret = std::env::var("TMDB_TOKEN_V3").unwrap();
165        let client = Client::<ReqwestExecutor>::new(secret);
166
167        let result = MovieTranslations::new(550).execute(&client).await.unwrap();
168        assert_eq!(result.id, 550);
169    }
170}