mangadex_api/v5/api_client/id/
get.rs1use derive_builder::Builder;
27use serde::Serialize;
28use uuid::Uuid;
29
30use crate::HttpClientRef;
31use mangadex_api_schema::v5::ApiClientResponse;
32use mangadex_api_types::ReferenceExpansionResource;
33
34#[cfg_attr(
35 feature = "deserializable-endpoint",
36 derive(serde::Deserialize, getset::Getters, getset::Setters)
37)]
38#[derive(Debug, Serialize, Clone, Builder)]
39#[serde(rename_all = "camelCase")]
40#[builder(
41 setter(into, strip_option),
42 build_fn(error = "mangadex_api_types::error::BuilderError")
43)]
44pub struct GetClient {
45 #[doc(hidden)]
47 #[serde(skip)]
48 #[builder(pattern = "immutable")]
49 #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
50 pub http_client: HttpClientRef,
51
52 #[serde(skip_serializing)]
53 pub client_id: Uuid,
54
55 #[builder(setter(each = "include"), default)]
56 #[serde(skip_serializing_if = "Vec::is_empty")]
57 pub includes: Vec<ReferenceExpansionResource>,
58}
59
60endpoint! {
61 GET ("/client/{}", client_id),
62 #[query auth] GetClient,
63 #[flatten_result] ApiClientResponse,
64 GetClientBuilder
65}
66
67#[cfg(test)]
68mod tests {
69 use serde_json::json;
70 use url::Url;
71 use uuid::Uuid;
72 use wiremock::matchers::{header, method, path_regex};
73 use wiremock::{Mock, MockServer, ResponseTemplate};
74
75 use crate::{HttpClient, MangaDexClient};
76 use mangadex_api_schema::v5::{AuthTokens, RelatedAttributes};
77 use mangadex_api_types::{ReferenceExpansionResource, RelationshipType};
78
79 #[tokio::test]
80 async fn get_client_fires_a_request_to_base_url() -> anyhow::Result<()> {
81 let mock_server = MockServer::start().await;
82 let http_client = HttpClient::builder()
83 .base_url(Url::parse(&mock_server.uri())?)
84 .auth_tokens(AuthTokens {
85 session: "myToken".to_string(),
86 refresh: "myRefreshToken".to_string(),
87 })
88 .build()?;
89 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
90
91 let client_id = Uuid::parse_str("eec486de-24f0-4e68-8459-34f26a62ceaa").unwrap();
92
93 let response_body = json!({
94 "result": "ok",
95 "response": "entity",
96 "data": {
97 "id": client_id,
98 "type": "api_client",
99 "attributes": {
100 "name": "Mangadex-API-Auth",
101 "description": "This is a API Client used for the [mangadex-api](https://github.com/tonymushah/mangadex-api) tests.",
102 "profile": "personal",
103 "externalClientId": null,
104 "isActive": false,
105 "state": "requested",
106 "createdAt": "2023-10-28T12:37:22+00:00",
107 "updatedAt": "2023-10-28T12:37:22+00:00",
108 "version": 1
109 },
110 "relationships": [
111 {
112 "id": "554149c7-f28f-4a30-b5fa-9db9b1e11353",
113 "type": "creator"
114 }
115 ]
116 }
117 });
118
119 Mock::given(method("GET"))
120 .and(path_regex(r"/client/[0-9a-fA-F-]+"))
121 .and(header("Authorization", "Bearer myToken"))
122 .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
123 .expect(1)
124 .mount(&mock_server)
125 .await;
126
127 let res = mangadex_client.client().id(client_id).get().send().await?;
128
129 assert_eq!(res.data.type_, RelationshipType::ApiClient);
130
131 Ok(())
132 }
133
134 #[tokio::test]
135 async fn get_client_handles_reference_expansion() -> anyhow::Result<()> {
136 let mock_server = MockServer::start().await;
137 let http_client = HttpClient::builder()
138 .base_url(Url::parse(&mock_server.uri())?)
139 .auth_tokens(AuthTokens {
140 session: "myToken".to_string(),
141 refresh: "myRefreshToken".to_string(),
142 })
143 .build()?;
144 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
145
146 let client_id = Uuid::parse_str("eec486de-24f0-4e68-8459-34f26a62ceaa").unwrap();
147
148 let response_body = json!({
149 "result": "ok",
150 "response": "entity",
151 "data": {
152 "id": client_id,
153 "type": "api_client",
154 "attributes": {
155 "name": "Mangadex-API-Auth",
156 "description": "This is a API Client used for the [mangadex-api](https://github.com/tonymushah/mangadex-api) tests.",
157 "profile": "personal",
158 "externalClientId": null,
159 "isActive": false,
160 "state": "requested",
161 "createdAt": "2023-10-28T12:37:22+00:00",
162 "updatedAt": "2023-10-28T12:37:22+00:00",
163 "version": 1
164 },
165 "relationships": [
166 {
167 "id": "554149c7-f28f-4a30-b5fa-9db9b1e11353",
168 "type": "creator",
169 "attributes": {
170 "username": "Tony_Mushah",
171 "roles": [
172 "ROLE_USER"
173 ],
174 "version": 175
175 }
176 }
177 ]
178 }
179 });
180
181 Mock::given(method("GET"))
182 .and(path_regex(r"/client/[0-9a-fA-F-]+"))
183 .and(header("Authorization", "Bearer myToken"))
184 .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
185 .expect(1)
186 .mount(&mock_server)
187 .await;
188
189 let res = mangadex_client
190 .client()
191 .id(client_id)
192 .get()
193 .include(&ReferenceExpansionResource::Creator)
194 .send()
195 .await?;
196
197 assert_eq!(res.data.relationships[0].type_, RelationshipType::Creator);
198 assert!(res.data.relationships[0].related.is_none());
199 if let RelatedAttributes::User(user) =
200 res.data.relationships[0].attributes.as_ref().unwrap()
201 {
202 assert_eq!(user.username, "Tony_Mushah".to_string());
203 } else {
204 panic!("Expected user RelatedAttributes");
205 }
206
207 Ok(())
208 }
209}