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