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

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