mangadex_api/v5/custom_list/id/
put.rs

1//! Builder for the CustomList update endpoint.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/CustomList/put-list-id>
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//!         .username(Username::parse("myusername")?)
22//!         .password(Password::parse("hunter23")?)
23//!         .build()?
24//!         .send()
25//!         .await?;
26//! */
27//!
28//! let list_id = Uuid::new_v4();
29//! let res = client
30//!     .custom_list()
31//!     .id(list_id)
32//!     .put()
33//!     .name("Updated List Name")
34//!     .version(2u32)
35//!     .send()
36//!     .await?;
37//!
38//! println!("update: {:?}", res);
39//! # Ok(())
40//! # }
41//! ```
42
43use derive_builder::Builder;
44use serde::Serialize;
45use uuid::Uuid;
46
47use crate::HttpClientRef;
48use mangadex_api_schema::v5::CustomListResponse;
49use mangadex_api_types::CustomListVisibility;
50
51#[cfg_attr(
52    feature = "deserializable-endpoint",
53    derive(serde::Deserialize, getset::Getters, getset::Setters)
54)]
55#[derive(Debug, Serialize, Clone, Builder)]
56#[serde(rename_all = "camelCase")]
57#[builder(
58    setter(into, strip_option),
59    build_fn(error = "mangadex_api_types::error::BuilderError")
60)]
61#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
62pub struct UpdateCustomList {
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 list_id: Uuid,
72
73    #[serde(skip_serializing_if = "Option::is_none")]
74    #[builder(default)]
75    pub name: Option<String>,
76
77    #[serde(skip_serializing_if = "Option::is_none")]
78    #[builder(default)]
79    pub visibility: Option<CustomListVisibility>,
80
81    #[serde(skip_serializing_if = "Vec::is_empty")]
82    #[builder(setter(each = "add_manga_id"), default)]
83    pub manga: Vec<Uuid>,
84
85    pub version: u32,
86}
87
88endpoint! {
89    PUT ("/list/{}", list_id),
90    #[body auth] UpdateCustomList,
91    #[flatten_result] CustomListResponse,
92    UpdateCustomListBuilder
93}
94
95#[cfg(test)]
96mod tests {
97    use fake::faker::name::en::Name;
98    use fake::Fake;
99    use serde_json::json;
100    use url::Url;
101    use uuid::Uuid;
102    use wiremock::matchers::{body_json, header, method, path_regex};
103    use wiremock::{Mock, MockServer, ResponseTemplate};
104
105    use crate::v5::AuthTokens;
106    use crate::{HttpClient, MangaDexClient};
107
108    #[tokio::test]
109    async fn update_custom_list_fires_a_request_to_base_url() -> anyhow::Result<()> {
110        let mock_server = MockServer::start().await;
111        let http_client = HttpClient::builder()
112            .base_url(Url::parse(&mock_server.uri())?)
113            .auth_tokens(AuthTokens {
114                session: "sessiontoken".to_string(),
115                refresh: "refreshtoken".to_string(),
116            })
117            .build()?;
118        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
119
120        let list_id = Uuid::new_v4();
121        let list_name: String = Name().fake();
122        let expected_body = json!({
123            "version": 2
124        });
125        let response_body = json!({
126            "result": "ok",
127            "response": "entity",
128            "data": {
129                "id": list_id,
130                "type": "custom_list",
131                "attributes": {
132                    "name": list_name,
133                    "visibility": "private",
134                    "version": 2
135                },
136                "relationships": []
137            }
138        });
139
140        Mock::given(method("PUT"))
141            .and(path_regex(r"/list/[0-9a-fA-F-]+"))
142            .and(header("Authorization", "Bearer sessiontoken"))
143            .and(header("Content-Type", "application/json"))
144            .and(body_json(expected_body))
145            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
146            .expect(1)
147            .mount(&mock_server)
148            .await;
149
150        let _ = mangadex_client
151            .custom_list()
152            .id(list_id)
153            .put()
154            .version(2u32)
155            .send()
156            .await?;
157
158        Ok(())
159    }
160}