mangadex_api/v5/report/
post.rs

1//! Builder for creating a new report.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/Report/post-report>
4//!
5//! # Examples
6//!
7//! ```rust
8//! use uuid::Uuid;
9//!
10//! use mangadex_api_types::ReportCategory;
11//! use mangadex_api::MangaDexClient;
12//! // use mangadex_api_types::{Password, Username};
13//!
14//! # async fn run() -> anyhow::Result<()> {
15//! let client = MangaDexClient::default();
16//!
17//! /*
18//!
19//!     let _login_res = client
20//!         .auth()
21//!         .login()
22//!         .post()
23//!         .username(Username::parse("myusername")?)
24//!         .password(Password::parse("hunter23")?)
25//!         .send()
26//!         .await?;
27//!
28//!  */
29//!
30//!
31//! let reason_id = Uuid::new_v4();
32//! let manga_id = Uuid::new_v4();
33//!
34//! let res = client
35//!     .report()
36//!     .post()
37//!     .category(ReportCategory::Manga)
38//!     .reason(reason_id)
39//!     .object_id(manga_id)
40//!     .send()
41//!     .await?;
42//!
43//! println!("report reasons: {:?}", res);
44//! # Ok(())
45//! # }
46//! ```
47
48use derive_builder::Builder;
49use serde::Serialize;
50use uuid::Uuid;
51
52use crate::HttpClientRef;
53use mangadex_api_schema::NoData;
54use mangadex_api_types::ReportCategory;
55
56#[cfg_attr(
57    feature = "deserializable-endpoint",
58    derive(serde::Deserialize, getset::Getters, getset::Setters)
59)]
60#[derive(Debug, Serialize, Clone, Builder)]
61#[serde(rename_all = "camelCase")]
62#[builder(
63    setter(into, strip_option),
64    build_fn(error = "mangadex_api_types::error::BuilderError")
65)]
66pub struct CreateReport {
67    #[doc(hidden)]
68    #[serde(skip)]
69    #[builder(pattern = "immutable")]
70    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
71    pub http_client: HttpClientRef,
72
73    pub category: ReportCategory,
74    /// The report reason ID for sub-categorization.
75    ///
76    /// For example, if a manga was being reported for being a troll entry, the specific reason ID should be used, obtained from the [list report reasons endpoint](crate::v5::report::get).
77    pub reason: Uuid,
78    /// The ID from the category type.
79    ///
80    /// For example, if the category is "manga", this should be a manga UUID.
81    pub object_id: Uuid,
82    /// Optional notes about why this is being reported.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    #[builder(default)]
85    pub details: Option<String>,
86}
87
88endpoint! {
89    POST "/report",
90    #[body auth] CreateReport,
91    #[rate_limited] NoData,
92    CreateReportBuilder
93}
94
95#[cfg(test)]
96mod tests {
97    use serde_json::json;
98    use url::Url;
99    use uuid::Uuid;
100    use wiremock::matchers::{body_json, header, method, path};
101    use wiremock::{Mock, MockServer, ResponseTemplate};
102
103    use crate::v5::AuthTokens;
104    use crate::{HttpClient, MangaDexClient};
105    use mangadex_api_types::ReportCategory;
106
107    #[tokio::test]
108    async fn create_report_reasons_fires_a_request_to_base_url() -> anyhow::Result<()> {
109        let mock_server = MockServer::start().await;
110        let http_client = HttpClient::builder()
111            .base_url(Url::parse(&mock_server.uri())?)
112            .auth_tokens(AuthTokens {
113                session: "sessiontoken".to_string(),
114                refresh: "refreshtoken".to_string(),
115            })
116            .build()?;
117        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
118
119        let reason_id = Uuid::new_v4();
120        let manga_id = Uuid::new_v4();
121        let expected_body = json!({
122            "category": "manga",
123            "reason": reason_id,
124            "objectId": manga_id,
125        });
126        let response_body = json!({
127            "result": "ok"
128        });
129
130        Mock::given(method("POST"))
131            .and(path("/report"))
132            .and(header("Authorization", "Bearer sessiontoken"))
133            .and(header("Content-Type", "application/json"))
134            .and(body_json(expected_body))
135            .respond_with(
136                ResponseTemplate::new(200)
137                    .insert_header("x-ratelimit-retry-after", "1698723860")
138                    .insert_header("x-ratelimit-limit", "40")
139                    .insert_header("x-ratelimit-remaining", "39")
140                    .set_body_json(response_body),
141            )
142            .expect(1)
143            .mount(&mock_server)
144            .await;
145
146        mangadex_client
147            .report()
148            .post()
149            .category(ReportCategory::Manga)
150            .reason(reason_id)
151            .object_id(manga_id)
152            .send()
153            .await?;
154
155        Ok(())
156    }
157}