mangadex_api/v5/user/list/
get.rs

1//! Builder for fetching the logged-in user's custom lists.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/CustomList/get-user-list>
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//!
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//!
29//! let res = client
30//!     .user()
31//!     .list()
32//!     .get()
33//!     .limit(1_u32)
34//!     .send()
35//!     .await?;
36//!
37//! println!("custom lists: {:?}", res);
38//! # Ok(())
39//! # }
40//! ```
41
42use derive_builder::Builder;
43use serde::Serialize;
44
45use crate::HttpClientRef;
46use mangadex_api_schema::v5::CustomListListResponse;
47
48#[cfg_attr(
49    feature = "deserializable-endpoint",
50    derive(serde::Deserialize, getset::Getters, getset::Setters)
51)]
52#[derive(Debug, Serialize, Clone, Builder, Default)]
53#[serde(rename_all = "camelCase")]
54#[builder(
55    setter(into, strip_option),
56    default,
57    build_fn(error = "mangadex_api_types::error::BuilderError")
58)]
59pub struct MyCustomLists {
60    /// This should never be set manually as this is only for internal use.
61    #[doc(hidden)]
62    #[serde(skip)]
63    #[builder(pattern = "immutable")]
64    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
65    pub http_client: HttpClientRef,
66
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub limit: Option<u32>,
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub offset: Option<u32>,
71}
72
73endpoint! {
74    GET "/user/list",
75    #[query auth] MyCustomLists,
76    #[flatten_result] CustomListListResponse,
77    MyCustomListsBuilder
78}
79
80#[cfg(test)]
81mod tests {
82    use fake::faker::name::en::Name;
83    use fake::Fake;
84    use serde_json::json;
85    use url::Url;
86    use uuid::Uuid;
87    use wiremock::matchers::{header, method, path};
88    use wiremock::{Mock, MockServer, ResponseTemplate};
89
90    use crate::v5::AuthTokens;
91    use crate::{HttpClient, MangaDexClient};
92
93    #[tokio::test]
94    async fn get_my_custom_lists_fires_a_request_to_base_url() -> anyhow::Result<()> {
95        let mock_server = MockServer::start().await;
96        let http_client = HttpClient::builder()
97            .base_url(Url::parse(&mock_server.uri())?)
98            .auth_tokens(AuthTokens {
99                session: "sessiontoken".to_string(),
100                refresh: "refreshtoken".to_string(),
101            })
102            .build()?;
103        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
104
105        let list_id = Uuid::new_v4();
106        let list_name: String = Name().fake();
107        let response_body = json!({
108            "result": "ok",
109            "response": "collection",
110            "data": [
111                {
112                    "id": list_id,
113                    "type": "custom_list",
114                    "attributes": {
115                        "name": list_name,
116                        "visibility": "private",
117                        "version": 1
118                    },
119                    "relationships": []
120                }
121            ],
122            "limit": 1,
123            "offset": 0,
124            "total": 1
125        });
126
127        Mock::given(method("GET"))
128            .and(path(r"/user/list"))
129            .and(header("Authorization", "Bearer sessiontoken"))
130            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
131            .expect(1)
132            .mount(&mock_server)
133            .await;
134
135        let _ = mangadex_client
136            .user()
137            .list()
138            .get()
139            .limit(1_u32)
140            .send()
141            .await?;
142
143        Ok(())
144    }
145}