mangadex_api/v5/manga/id/follow/
post.rs

1//! Builder for the follow manga endpoint.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/Manga/post-manga-id-follow>
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//!         .post()
22//!         .username(Username::parse("myusername")?)
23//!         .password(Password::parse("hunter23")?)
24//!         .send()
25//!         .await?;
26//!
27//!  */
28//!
29//!
30//! let manga_id = Uuid::new_v4();
31//! let res = client
32//!     .manga()
33//!     .id(manga_id)
34//!     .follow()
35//!     .post()
36//!     .send()
37//!     .await?;
38//!
39//! println!("follow manga: {:?}", res);
40//! # Ok(())
41//! # }
42//! ```
43
44use derive_builder::Builder;
45use serde::Serialize;
46use uuid::Uuid;
47
48use crate::HttpClientRef;
49use mangadex_api_schema::NoData;
50use mangadex_api_types::error::Result;
51
52#[cfg_attr(
53    feature = "deserializable-endpoint",
54    derive(serde::Deserialize, getset::Getters, getset::Setters)
55)]
56#[derive(Debug, Serialize, Clone, Builder)]
57#[serde(rename_all = "camelCase")]
58#[builder(
59    setter(into, strip_option),
60    build_fn(error = "mangadex_api_types::error::BuilderError")
61)]
62#[cfg_attr(
63    feature = "custom_list_v2",
64    deprecated(
65        since = "3.0.0-rc.1",
66        note = "After the introduction of the Subscription system, this endpoint will be removed in v3"
67    )
68)]
69pub struct FollowManga {
70    /// This should never be set manually as this is only for internal use.
71    #[doc(hidden)]
72    #[serde(skip)]
73    #[builder(pattern = "immutable")]
74    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
75    pub http_client: HttpClientRef,
76
77    #[serde(skip_serializing)]
78    pub manga_id: Uuid,
79}
80
81endpoint! {
82    POST ("/manga/{}/follow", manga_id),
83    #[no_data auth] FollowManga,
84    #[discard_result] Result<NoData>,
85    FollowMangaBuilder
86}
87
88#[cfg(test)]
89mod tests {
90    use serde_json::json;
91    use url::Url;
92    use uuid::Uuid;
93    use wiremock::matchers::{header, method, path_regex};
94    use wiremock::{Mock, MockServer, ResponseTemplate};
95
96    use crate::v5::AuthTokens;
97    use crate::{HttpClient, MangaDexClient};
98
99    #[tokio::test]
100    async fn follow_manga_fires_a_request_to_base_url() -> anyhow::Result<()> {
101        let mock_server = MockServer::start().await;
102        let http_client = HttpClient::builder()
103            .base_url(Url::parse(&mock_server.uri())?)
104            .auth_tokens(AuthTokens {
105                session: "sessiontoken".to_string(),
106                refresh: "refreshtoken".to_string(),
107            })
108            .build()?;
109        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
110
111        let manga_id = Uuid::new_v4();
112        let response_body = json!({
113            "result": "ok",
114        });
115
116        Mock::given(method("POST"))
117            .and(path_regex(r"/manga/[0-9a-fA-F-]+/follow"))
118            .and(header("Authorization", "Bearer sessiontoken"))
119            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
120            .expect(1)
121            .mount(&mock_server)
122            .await;
123
124        mangadex_client
125            .manga()
126            .id(manga_id)
127            .follow()
128            .post()
129            .send()
130            .await?;
131
132        Ok(())
133    }
134}