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

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