mangadex_api/v5/api_client/id/
post.rs1use derive_builder::Builder;
31use serde::Serialize;
32
33use crate::HttpClientRef;
34use uuid::Uuid;
35
36type ApiClientResponse = crate::Result<mangadex_api_schema::v5::ApiClientData>;
37
38#[cfg_attr(
44 feature = "deserializable-endpoint",
45 derive(serde::Deserialize, getset::Getters, getset::Setters)
46)]
47#[derive(Debug, Serialize, Clone, Builder, Default)]
48#[serde(rename_all = "camelCase")]
49#[builder(
50 setter(into, strip_option),
51 build_fn(error = "crate::error::BuilderError")
52)]
53#[non_exhaustive]
54pub struct EditClient {
55 #[doc(hidden)]
57 #[serde(skip)]
58 #[builder(pattern = "immutable")]
59 #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
60 pub http_client: HttpClientRef,
61
62 #[serde(skip_serializing)]
63 pub client_id: Uuid,
64 #[serde(skip_serializing_if = "Option::is_none")]
65 #[builder(default)]
66 pub description: Option<String>,
67 pub version: u32,
69}
70
71endpoint! {
72 POST ("/client/{}", client_id),
73 #[body auth] EditClient,
74 #[flatten_result] ApiClientResponse,
75 EditClientBuilder
76}
77
78#[cfg(test)]
79mod tests {
80 use serde::Serialize;
81 use serde_json::json;
82 use url::Url;
83 use uuid::Uuid;
84 use wiremock::matchers::{body_json, header, method, path_regex};
85 use wiremock::{Mock, MockServer, ResponseTemplate};
86
87 use crate::{HttpClient, MangaDexClient};
88 use mangadex_api_schema::v5::AuthTokens;
89 use mangadex_api_types::RelationshipType;
90
91 #[derive(Serialize, Clone)]
92 struct EditClientBody {
93 description: Option<String>,
94 version: u32,
95 }
96
97 #[tokio::test]
98 async fn edit_client_fires_a_request_to_base_url() -> anyhow::Result<()> {
99 let mock_server = MockServer::start().await;
100 let http_client = HttpClient::builder()
101 .base_url(Url::parse(&mock_server.uri())?)
102 .auth_tokens(non_exhaustive::non_exhaustive!(AuthTokens {
103 session: "myToken".to_string(),
104 refresh: "myRefreshToken".to_string(),
105 }))
106 .build()?;
107 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
108
109 let client_id = Uuid::parse_str("eec486de-24f0-4e68-8459-34f26a62ceaa").unwrap();
110
111 let _expected_body: EditClientBody = EditClientBody{
112 description: Some("This is a API Client used for the [mangadex-api](https://github.com/tonymushah/mangadex-api) tests.".to_string()),
113 version: 1
114 };
115
116 let response_body = json!({
117 "result": "ok",
118 "response": "entity",
119 "data": {
120 "id": client_id,
121 "type": "api_client",
122 "attributes": {
123 "name": "Mangadex-API-Auth",
124 "description": _expected_body.description.clone(),
125 "profile": "personal",
126 "externalClientId": null,
127 "isActive": false,
128 "state": "requested",
129 "createdAt": "2023-10-28T12:37:22+00:00",
130 "updatedAt": "2023-10-28T12:37:22+00:00",
131 "version": _expected_body.version
132 },
133 "relationships": [
134 {
135 "id": "554149c7-f28f-4a30-b5fa-9db9b1e11353",
136 "type": "creator"
137 }
138 ]
139 }
140 });
141
142 Mock::given(method("POST"))
143 .and(path_regex(r"/client/[0-9a-fA-F-]+"))
144 .and(header("Authorization", "Bearer myToken"))
145 .and(header("Content-Type", "application/json"))
146 .and(body_json(_expected_body.clone()))
147 .respond_with(ResponseTemplate::new(201).set_body_json(response_body))
148 .expect(1)
149 .mount(&mock_server)
150 .await;
151
152 let res = mangadex_client
153 .client()
154 .id(client_id)
155 .post()
156 .description(_expected_body.description.clone().unwrap())
157 .version(_expected_body.version)
158 .send()
159 .await?;
160
161 assert_eq!(res.data.type_, RelationshipType::ApiClient);
162 assert_eq!(
163 res.data.attributes.description,
164 _expected_body.description.clone()
165 );
166 assert_eq!(res.data.attributes.version, _expected_body.version);
167 Ok(())
168 }
169}