mangadex_api/v5/manga/id/list/
get.rs

1//! Builder for getting the CustomLists where the manga is is.
2//!
3//! NOTICE: This endpoint is not deployed yet on <https://mangadex.org>
4//! We'll notice if it's 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 manga_id = Uuid::new_v4();
17//! let manga_res = client
18//!     .manga()
19//!     .id(manga_id)
20//!     .list()
21//!     .get()
22//!     .send()
23//!     .await?;
24//!
25//! println!("manga view: {:?}", manga_res);
26//! # Ok(())
27//! # }
28//! ```
29use derive_builder::Builder;
30use serde::Serialize;
31use uuid::Uuid;
32
33use crate::HttpClientRef;
34use mangadex_api_schema::v5::CustomListListResponse;
35
36#[cfg_attr(
37    feature = "deserializable-endpoint",
38    derive(serde::Deserialize, getset::Getters, getset::Setters)
39)]
40#[derive(Debug, Serialize, Clone, Builder)]
41#[serde(rename_all = "camelCase")]
42#[builder(
43    setter(into, strip_option),
44    build_fn(error = "mangadex_api_types::error::BuilderError")
45)]
46pub struct GetMangaCustomLists {
47    /// This should never be set manually as this is only for internal use.
48    #[doc(hidden)]
49    #[serde(skip)]
50    #[builder(pattern = "immutable")]
51    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
52    pub http_client: HttpClientRef,
53
54    #[serde(skip_serializing)]
55    pub manga_id: Uuid,
56
57    #[serde(skip_serializing_if = "Option::is_none")]
58    #[builder(default)]
59    limit: Option<u32>,
60    #[serde(skip_serializing_if = "Option::is_none")]
61    #[builder(default)]
62    offset: Option<u32>,
63}
64
65endpoint! {
66    GET ("/manga/{}/list", manga_id),
67    #[query] GetMangaCustomLists,
68    #[flatten_result] CustomListListResponse,
69    GetMangaCustomListsBuilder
70}
71
72#[cfg(test)]
73mod tests {
74    use fake::faker::name::en::Name;
75    use fake::Fake;
76    use serde_json::json;
77    use url::Url;
78    use uuid::Uuid;
79    use wiremock::matchers::{method, path_regex};
80    use wiremock::{Mock, MockServer, ResponseTemplate};
81
82    use crate::{HttpClient, MangaDexClient};
83
84    #[tokio::test]
85    async fn get_manga_custom_lists_fires_a_request_to_base_url() -> anyhow::Result<()> {
86        let mock_server = MockServer::start().await;
87        let http_client = HttpClient::builder()
88            .base_url(Url::parse(&mock_server.uri())?)
89            .build()?;
90        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
91
92        let manga_id = Uuid::new_v4();
93        let list_id = Uuid::new_v4();
94        let list_name: String = Name().fake();
95        let response_body = json!({
96            "result": "ok",
97            "response": "collection",
98            "data": [
99                {
100                    "id": list_id,
101                    "type": "custom_list",
102                    "attributes": {
103                        "name": list_name,
104                        "visibility": "public",
105                        "version": 1
106                    },
107                    "relationships": []
108                }
109            ],
110            "limit": 1,
111            "offset": 0,
112            "total": 1
113        });
114
115        Mock::given(method("GET"))
116            .and(path_regex(r"/manga/[0-9a-fA-F-]+/list"))
117            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
118            .expect(1)
119            .mount(&mock_server)
120            .await;
121
122        let _ = mangadex_client
123            .manga()
124            .id(manga_id)
125            .list()
126            .get()
127            .limit(1_u32)
128            .send()
129            .await?;
130
131        Ok(())
132    }
133}