mangadex_api/v5/custom_list/id/
put.rs1use 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 #[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}