mangadex_api/v5/custom_list/id/batch_manga/
delete.rs

1//! Builder for removing a batch of manga from a custom list.
2//!
3//! NOTICE : This endpoint is not currenlty deployed yet on mangadex.org
4//! We will give more information when it will be deployed
5//!
6//! # Examples
7//!
8//! ```rust
9//! use uuid::Uuid;
10//!
11//! use mangadex_api::v5::MangaDexClient;
12//!
13//! # async fn run() -> anyhow::Result<()> {
14//! let client = MangaDexClient::default();
15//!
16//! let list_id = Uuid::new_v4();
17//! let res = client
18//!     .custom_list()
19//!     .id(list_id)
20//!     .batch_manga()
21//!     .delete()
22//!     .manga_id(Uuid::new_v4())
23//!     .manga_id(Uuid::new_v4())
24//!     .manga_id(Uuid::new_v4())
25//!     .send()
26//!     .await?;
27//!
28//! println!("custom list: {:?}", res);
29//! # Ok(())
30//! # }
31//! ```
32
33use derive_builder::Builder;
34use mangadex_api_schema::NoData;
35use serde::Serialize;
36use uuid::Uuid;
37
38use crate::HttpClientRef;
39use mangadex_api_types::error::Result;
40
41#[cfg_attr(
42    feature = "deserializable-endpoint",
43    derive(serde::Deserialize, getset::Getters, getset::Setters)
44)]
45#[derive(Debug, Serialize, Clone, Builder)]
46#[serde(rename_all = "camelCase")]
47#[builder(
48    setter(into, strip_option),
49    build_fn(error = "mangadex_api_types::error::BuilderError")
50)]
51#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
52pub struct DeleteMangaBatchViaCustomList {
53    /// This should never be set manually as this is only for internal use.
54    #[doc(hidden)]
55    #[serde(skip)]
56    #[builder(pattern = "immutable")]
57    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
58    pub http_client: HttpClientRef,
59
60    #[serde(skip_serializing)]
61    /// CustomList ID.
62    pub list_id: Uuid,
63
64    #[builder(default)]
65    #[builder(setter(each = "manga_id"))]
66    pub manga_ids: Vec<Uuid>,
67}
68
69endpoint! {
70    DELETE ("/list/{}/batch-manga", list_id),
71    #[body auth] DeleteMangaBatchViaCustomList,
72    #[flatten_result] Result<NoData>,
73    DeleteMangaBatchViaCustomListBuilder
74}
75
76#[cfg(test)]
77mod tests {
78    use serde_json::json;
79    use url::Url;
80    use uuid::Uuid;
81    use wiremock::matchers::{body_json, header, method, path_regex};
82    use wiremock::{Mock, MockServer, ResponseTemplate};
83
84    use crate::v5::AuthTokens;
85    use crate::{HttpClient, MangaDexClient};
86
87    #[tokio::test]
88    async fn delete_manga_by_batch_custom_list_fires_a_request_to_base_url() -> anyhow::Result<()> {
89        let mock_server = MockServer::start().await;
90        let http_client = HttpClient::builder()
91            .base_url(Url::parse(&mock_server.uri())?)
92            .auth_tokens(AuthTokens {
93                session: "sessiontoken".to_string(),
94                refresh: "refreshtoken".to_string(),
95            })
96            .build()?;
97        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
98
99        let custom_list_id = Uuid::new_v4();
100        let response_body = json!({
101            "result": "ok"
102        });
103
104        Mock::given(method("DELETE"))
105            .and(path_regex(r"/list/[0-9a-fA-F-]+/batch-manga"))
106            .and(header("Authorization", "Bearer sessiontoken"))
107            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
108            .expect(1)
109            .mount(&mock_server)
110            .await;
111
112        mangadex_client
113            .custom_list()
114            .id(custom_list_id)
115            .batch_manga()
116            .delete()
117            .send()
118            .await?;
119
120        Ok(())
121    }
122    #[tokio::test]
123    async fn delete_manga_by_batch_with_batch_custom_list_fires_a_request_to_base_url(
124    ) -> anyhow::Result<()> {
125        let mock_server = MockServer::start().await;
126        let http_client = HttpClient::builder()
127            .base_url(Url::parse(&mock_server.uri())?)
128            .auth_tokens(AuthTokens {
129                session: "sessiontoken".to_string(),
130                refresh: "refreshtoken".to_string(),
131            })
132            .build()?;
133        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
134
135        let custom_list_id = Uuid::new_v4();
136        let manga_ids = vec![Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4()];
137        let response_body = json!({
138            "result": "ok"
139        });
140
141        let endpoint_ = mangadex_client
142            .custom_list()
143            .id(custom_list_id)
144            .batch_manga()
145            .delete()
146            .manga_ids(manga_ids)
147            .build()?;
148        println!("{}", serde_json::to_string_pretty(&(endpoint_.clone()))?);
149        Mock::given(method("DELETE"))
150            .and(path_regex(r"/list/[0-9a-fA-F-]+/batch-manga"))
151            .and(header("Authorization", "Bearer sessiontoken"))
152            .and(body_json(endpoint_.clone()))
153            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
154            .expect(1)
155            .mount(&mock_server)
156            .await;
157
158        endpoint_.send().await?;
159
160        Ok(())
161    }
162}