Skip to main content

mangadex_api/v5/api_client/id/
delete.rs

1//! Builder for the delete manga endpoint.
2//!
3//! <https://api.mangadex.org/docs/redoc.html#tag/ApiClient/operation/delete-apiclient>
4//!
5//! # Examples
6//!
7//! ```rust
8//! use uuid::Uuid;
9//!
10//! use mangadex_api::v5::MangaDexClient;
11//! // use mangadex_api_types::{Password, Username};
12//!
13//! # async fn run() -> anyhow::Result<()> {
14//! let client = MangaDexClient::default();
15//!
16//! /*
17//!     // Putting your login script
18//!     let _login_res = client
19//!         .auth()
20//!         .login()
21//!         .username(Username::parse("myusername")?)
22//!         .password(Password::parse("hunter23")?)
23//!         .send()
24//!         .await?;
25//!  */
26//!
27//!
28//! let client_id = Uuid::new_v4();
29//! let res = client
30//!     .client()
31//!     .id(client_id)
32//!     .delete()
33//!     .send()
34//!     .await?;
35//!
36//! println!("delete: {:?}", res);
37//! # Ok(())
38//! # }
39//! ```
40
41use derive_builder::Builder;
42use serde::Serialize;
43use uuid::Uuid;
44
45use crate::HttpClientRef;
46use crate::Result;
47use mangadex_api_schema::NoData;
48
49#[cfg_attr(
50    feature = "deserializable-endpoint",
51    derive(serde::Deserialize, getset::Getters, getset::Setters)
52)]
53#[derive(Debug, Serialize, Clone, Builder)]
54#[serde(rename_all = "camelCase")]
55#[builder(build_fn(error = "crate::error::BuilderError"))]
56#[non_exhaustive]
57pub struct DeleteClient {
58    /// This should never be set manually as this is only for internal use.
59    #[doc(hidden)]
60    #[serde(skip)]
61    #[builder(pattern = "immutable")]
62    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
63    pub http_client: HttpClientRef,
64
65    #[serde(skip_serializing)]
66    pub client_id: Uuid,
67    #[serde(skip_serializing_if = "Option::is_none")]
68    #[builder(default)]
69    pub version: Option<u32>,
70}
71
72endpoint! {
73    DELETE ("/client/{}", client_id),
74    #[query auth] DeleteClient,
75    #[discard_result] Result<NoData>,
76    DeleteClientBuilder
77}
78
79#[cfg(test)]
80mod tests {
81    use serde_json::json;
82    use url::Url;
83    use uuid::Uuid;
84    use wiremock::matchers::{header, method, path_regex};
85    use wiremock::{Mock, MockServer, ResponseTemplate};
86
87    use crate::v5::AuthTokens;
88    use crate::{HttpClient, MangaDexClient};
89
90    #[tokio::test]
91    async fn delete_client_fires_a_request_to_base_url() -> anyhow::Result<()> {
92        let mock_server = MockServer::start().await;
93        let http_client = HttpClient::builder()
94            .base_url(Url::parse(&mock_server.uri())?)
95            .auth_tokens(non_exhaustive::non_exhaustive!(AuthTokens {
96                session: "sessiontoken".to_string(),
97                refresh: "refreshtoken".to_string(),
98            }))
99            .build()?;
100        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
101
102        let client_id = Uuid::new_v4();
103        let response_body = json!({
104            "result": "ok",
105        });
106
107        Mock::given(method("DELETE"))
108            .and(path_regex(r"/client/[0-9a-fA-F-]+"))
109            .and(header("Authorization", "Bearer sessiontoken"))
110            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
111            .expect(1)
112            .mount(&mock_server)
113            .await;
114
115        mangadex_client
116            .client()
117            .id(client_id)
118            .delete()
119            .send()
120            .await?;
121
122        Ok(())
123    }
124}