mangadex_api/v5/user/
get.rs1use derive_builder::Builder;
44use mangadex_api_schema::v5::UserCollection;
45use serde::Serialize;
46use uuid::Uuid;
47
48use crate::HttpClientRef;
49use mangadex_api_types::UserSortOrder;
50
51#[cfg_attr(
52 feature = "deserializable-endpoint",
53 derive(serde::Deserialize, getset::Getters, getset::Setters)
54)]
55#[derive(Debug, Serialize, Clone, Builder, Default)]
56#[serde(rename_all = "camelCase")]
57#[builder(
58 setter(into, strip_option),
59 default,
60 build_fn(error = "crate::error::BuilderError")
61)]
62#[non_exhaustive]
63pub struct ListUser {
64 #[doc(hidden)]
65 #[serde(skip)]
66 #[builder(pattern = "immutable")]
67 #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
68 pub http_client: HttpClientRef,
69
70 #[serde(skip_serializing_if = "Option::is_none")]
71 pub limit: Option<u32>,
72 #[serde(skip_serializing_if = "Option::is_none")]
73 pub offset: Option<u32>,
74 #[builder(setter(each = "add_user_id"))]
75 #[serde(rename = "ids")]
76 #[serde(skip_serializing_if = "Vec::is_empty")]
77 pub user_ids: Vec<Uuid>,
78 #[serde(skip_serializing_if = "Option::is_none")]
79 pub username: Option<String>,
80 #[serde(skip_serializing_if = "Option::is_none")]
81 pub order: Option<UserSortOrder>,
82}
83
84endpoint! {
85 GET "/user",
86 #[query auth] ListUser,
87 #[flatten_result] crate::Result<UserCollection>,
88 ListUserBuilder
89}
90
91#[cfg(test)]
92mod tests {
93 use serde_json::json;
94 use url::Url;
95 use uuid::Uuid;
96 use wiremock::matchers::{header, method, path};
97 use wiremock::{Mock, MockServer, ResponseTemplate};
98
99 use crate::error::Error;
100 use crate::v5::AuthTokens;
101 use crate::{HttpClient, MangaDexClient};
102 use mangadex_api_types::ResponseType;
103
104 #[tokio::test]
105 async fn list_user_fires_a_request_to_base_url() -> anyhow::Result<()> {
106 let mock_server = MockServer::start().await;
107 let http_client = HttpClient::builder()
108 .base_url(Url::parse(&mock_server.uri())?)
109 .auth_tokens(non_exhaustive::non_exhaustive!(AuthTokens {
110 session: "sessiontoken".to_string(),
111 refresh: "refreshtoken".to_string(),
112 }))
113 .build()?;
114 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
115
116 let user_id = Uuid::new_v4();
117 let response_body = json!({
118 "result": "ok",
119 "response": "collection",
120 "data": [
121 {
122 "id": user_id,
123 "type": "user",
124 "attributes": {
125 "username": "myusername",
126 "roles": [
127 "ROLE_MEMBER",
128 "ROLE_GROUP_MEMBER",
129 "ROLE_GROUP_LEADER"
130 ],
131 "version": 1
132 },
133 "relationships": [
134 {
135 "id": "a3219a4f-73c0-4213-8730-05985130539a",
136 "type": "scanlation_group"
137 }
138 ]
139 }
140 ],
141 "limit": 1,
142 "offset": 0,
143 "total": 1
144 });
145
146 Mock::given(method("GET"))
147 .and(path("/user"))
148 .and(header("Authorization", "Bearer sessiontoken"))
149 .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
150 .expect(1)
151 .mount(&mock_server)
152 .await;
153
154 let res = mangadex_client.user().get().limit(1u32).send().await?;
155
156 assert_eq!(res.response, ResponseType::Collection);
157 let user = &res.data[0];
158 assert_eq!(user.id, user_id);
159 assert_eq!(user.attributes.username, "myusername");
160 assert_eq!(user.attributes.version, 1);
161
162 Ok(())
163 }
164
165 #[tokio::test]
166 async fn list_manga_handles_400() -> anyhow::Result<()> {
167 let mock_server = MockServer::start().await;
168 let http_client: HttpClient = HttpClient::builder()
169 .base_url(Url::parse(&mock_server.uri())?)
170 .auth_tokens(non_exhaustive::non_exhaustive!(AuthTokens {
171 session: "sessiontoken".to_string(),
172 refresh: "refreshtoken".to_string(),
173 }))
174 .build()?;
175 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
176
177 let error_id = Uuid::new_v4();
178
179 let response_body = json!({
180 "result": "error",
181 "errors": [{
182 "id": error_id.to_string(),
183 "status": 400,
184 "title": "Invalid limit",
185 "detail": "Limit must be between 1 and 100"
186 }]
187 });
188
189 Mock::given(method("GET"))
190 .and(path("/user"))
191 .respond_with(ResponseTemplate::new(400).set_body_json(response_body))
192 .expect(1)
193 .mount(&mock_server)
194 .await;
195
196 let res = mangadex_client
197 .user()
198 .get()
199 .limit(0u32)
200 .send()
201 .await
202 .expect_err("expected error");
203
204 if let Error::Api(errors) = res {
205 assert_eq!(errors.errors.len(), 1);
206
207 assert_eq!(errors.errors[0].id, error_id);
208 assert_eq!(errors.errors[0].status, 400);
209 assert_eq!(errors.errors[0].title, Some("Invalid limit".to_string()));
210 assert_eq!(
211 errors.errors[0].detail,
212 Some("Limit must be between 1 and 100".to_string())
213 );
214 }
215
216 Ok(())
217 }
218}