mangadex_api/v5/custom_list/id/batch_manga/
post.rs1use derive_builder::Builder;
33use mangadex_api_schema::NoData;
34use serde::Serialize;
35use uuid::Uuid;
36
37use crate::HttpClientRef;
38use mangadex_api_types::error::Result;
39
40#[cfg_attr(
41 feature = "deserializable-endpoint",
42 derive(serde::Deserialize, getset::Getters, getset::Setters)
43)]
44#[derive(Debug, Serialize, Clone, Builder)]
45#[serde(rename_all = "camelCase")]
46#[builder(
47 setter(into, strip_option),
48 build_fn(error = "mangadex_api_types::error::BuilderError")
49)]
50#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
51pub struct AddMangaBatchViaCustomList {
52 #[doc(hidden)]
54 #[serde(skip)]
55 #[builder(pattern = "immutable")]
56 #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
57 pub http_client: HttpClientRef,
58
59 #[serde(skip_serializing)]
60 pub list_id: Uuid,
62
63 #[builder(default)]
64 #[builder(setter(each = "manga_id"))]
65 pub manga_ids: Vec<Uuid>,
66}
67
68endpoint! {
69 POST ("/list/{}/batch-manga", list_id),
70 #[body auth] AddMangaBatchViaCustomList,
71 #[flatten_result] Result<NoData>,
72 AddMangaBatchViaCustomListBuilder
73}
74
75#[cfg(test)]
76mod tests {
77 use serde_json::json;
78 use url::Url;
79 use uuid::Uuid;
80 use wiremock::matchers::{body_json, header, method, path_regex};
81 use wiremock::{Mock, MockServer, ResponseTemplate};
82
83 use crate::v5::AuthTokens;
84 use crate::{HttpClient, MangaDexClient};
85
86 #[tokio::test]
87 async fn post_manga_by_batch_custom_list_fires_a_request_to_base_url() -> anyhow::Result<()> {
88 let mock_server = MockServer::start().await;
89 let http_client = HttpClient::builder()
90 .base_url(Url::parse(&mock_server.uri())?)
91 .auth_tokens(AuthTokens {
92 session: "sessiontoken".to_string(),
93 refresh: "refreshtoken".to_string(),
94 })
95 .build()?;
96 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
97
98 let custom_list_id = Uuid::new_v4();
99 let response_body = json!({
100 "result": "ok"
101 });
102
103 Mock::given(method("POST"))
104 .and(path_regex(r"/list/[0-9a-fA-F-]+/batch-manga"))
105 .and(header("Authorization", "Bearer sessiontoken"))
106 .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
107 .expect(1)
108 .mount(&mock_server)
109 .await;
110
111 mangadex_client
112 .custom_list()
113 .id(custom_list_id)
114 .batch_manga()
115 .post()
116 .send()
117 .await?;
118
119 Ok(())
120 }
121 #[tokio::test]
122 async fn post_manga_by_batch_with_batch_custom_list_fires_a_request_to_base_url(
123 ) -> anyhow::Result<()> {
124 let mock_server = MockServer::start().await;
125 let http_client = HttpClient::builder()
126 .base_url(Url::parse(&mock_server.uri())?)
127 .auth_tokens(AuthTokens {
128 session: "sessiontoken".to_string(),
129 refresh: "refreshtoken".to_string(),
130 })
131 .build()?;
132 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
133
134 let custom_list_id = Uuid::new_v4();
135 let manga_ids = vec![Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4()];
136 let response_body = json!({
137 "result": "ok"
138 });
139
140 let endpoint_ = mangadex_client
141 .custom_list()
142 .id(custom_list_id)
143 .batch_manga()
144 .post()
145 .manga_ids(manga_ids)
146 .build()?;
147
148 println!("{}", serde_json::to_string_pretty(&(endpoint_.clone()))?);
149 Mock::given(method("POST"))
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}