mangadex_api/v5/manga/id/list/list_id/
delete.rs

1//! Builder for the removing manga from a custom list endpoint.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/Manga/delete-manga-id-list-listId>
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//!     // Put your login script here
18//!     let _login_res = client
19//!         .auth()
20//!         .login()
21//!         .post()
22//!         .username(Username::parse("myusername")?)
23//!         .password(Password::parse("hunter23")?)
24//!         .send()
25//!         .await?;
26//! */
27//!
28//! let manga_id = Uuid::new_v4();
29//! let list_id = Uuid::new_v4();
30//! let res = client
31//!     .manga()
32//!     .id(manga_id)
33//!     .list()
34//!     .list_id(list_id)
35//!     .delete()
36//!     .send()
37//!     .await?;
38//!
39//! println!("remove manga from custom list: {:?}", res);
40//! # Ok(())
41//! # }
42//! ```
43
44use derive_builder::Builder;
45use serde::Serialize;
46use uuid::Uuid;
47
48use crate::HttpClientRef;
49use mangadex_api_schema::NoData;
50use mangadex_api_types::error::Result;
51
52#[cfg_attr(
53    feature = "deserializable-endpoint",
54    derive(serde::Deserialize, getset::Getters, getset::Setters)
55)]
56#[derive(Debug, Serialize, Clone, Builder)]
57#[serde(rename_all = "camelCase")]
58#[builder(
59    setter(into, strip_option),
60    build_fn(error = "mangadex_api_types::error::BuilderError")
61)]
62pub struct RemoveMangaFromCustomList {
63    /// This should never be set manually as this is only for internal use.
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)]
71    pub manga_id: Uuid,
72    #[serde(skip_serializing)]
73    pub list_id: Uuid,
74}
75
76endpoint! {
77    DELETE ("/manga/{}/list/{}", manga_id, list_id),
78    #[no_data auth] RemoveMangaFromCustomList,
79    #[discard_result] Result<NoData>,
80    RemoveMangaFromCustomListBuilder
81}
82
83#[cfg(test)]
84mod tests {
85    use serde_json::json;
86    use url::Url;
87    use uuid::Uuid;
88    use wiremock::matchers::{header, method, path_regex};
89    use wiremock::{Mock, MockServer, ResponseTemplate};
90
91    use crate::v5::AuthTokens;
92    use crate::{HttpClient, MangaDexClient};
93
94    #[tokio::test]
95    async fn remove_manga_from_custom_list_fires_a_request_to_base_url() -> anyhow::Result<()> {
96        let mock_server = MockServer::start().await;
97        let http_client = HttpClient::builder()
98            .base_url(Url::parse(&mock_server.uri())?)
99            .auth_tokens(AuthTokens {
100                session: "sessiontoken".to_string(),
101                refresh: "refreshtoken".to_string(),
102            })
103            .build()?;
104        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
105
106        let manga_id = Uuid::new_v4();
107        let list_id = Uuid::new_v4();
108        let response_body = json!({
109            "result": "ok",
110        });
111
112        Mock::given(method("DELETE"))
113            .and(path_regex(r"/manga/[0-9a-fA-F-]+/list/[0-9a-fA-F-]+"))
114            .and(header("Authorization", "Bearer sessiontoken"))
115            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
116            .expect(1)
117            .mount(&mock_server)
118            .await;
119
120        mangadex_client
121            .manga()
122            .id(manga_id)
123            .list()
124            .list_id(list_id)
125            .delete()
126            .send()
127            .await?;
128
129        Ok(())
130    }
131}