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 crate::Result;
50use mangadex_api_schema::NoData;
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 = "crate::error::BuilderError")
61)]
62#[non_exhaustive]
63pub struct RemoveMangaFromCustomList {
64    /// This should never be set manually as this is only for internal use.
65    #[doc(hidden)]
66    #[serde(skip)]
67    #[builder(pattern = "immutable")]
68    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
69    pub http_client: HttpClientRef,
70
71    #[serde(skip_serializing)]
72    pub manga_id: Uuid,
73    #[serde(skip_serializing)]
74    pub list_id: Uuid,
75}
76
77endpoint! {
78    DELETE ("/manga/{}/list/{}", manga_id, list_id),
79    #[no_data auth] RemoveMangaFromCustomList,
80    #[discard_result] Result<NoData>,
81    RemoveMangaFromCustomListBuilder
82}
83
84#[cfg(test)]
85mod tests {
86    use serde_json::json;
87    use url::Url;
88    use uuid::Uuid;
89    use wiremock::matchers::{header, method, path_regex};
90    use wiremock::{Mock, MockServer, ResponseTemplate};
91
92    use crate::v5::AuthTokens;
93    use crate::{HttpClient, MangaDexClient};
94
95    #[tokio::test]
96    async fn remove_manga_from_custom_list_fires_a_request_to_base_url() -> anyhow::Result<()> {
97        let mock_server = MockServer::start().await;
98        let http_client = HttpClient::builder()
99            .base_url(Url::parse(&mock_server.uri())?)
100            .auth_tokens(non_exhaustive::non_exhaustive!(AuthTokens {
101                session: "sessiontoken".to_string(),
102                refresh: "refreshtoken".to_string(),
103            }))
104            .build()?;
105        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
106
107        let manga_id = Uuid::new_v4();
108        let list_id = Uuid::new_v4();
109        let response_body = json!({
110            "result": "ok",
111        });
112
113        Mock::given(method("DELETE"))
114            .and(path_regex(r"/manga/[0-9a-fA-F-]+/list/[0-9a-fA-F-]+"))
115            .and(header("Authorization", "Bearer sessiontoken"))
116            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
117            .expect(1)
118            .mount(&mock_server)
119            .await;
120
121        mangadex_client
122            .manga()
123            .id(manga_id)
124            .list()
125            .list_id(list_id)
126            .delete()
127            .send()
128            .await?;
129
130        Ok(())
131    }
132}