mangadex_api/v5/report/
post.rs1use 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 pub reason: Uuid,
78 pub object_id: Uuid,
82 #[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}