mangadex_api/v5/statistics/chapter/id/
get.rs1use derive_builder::Builder;
34use serde::Serialize;
35use uuid::Uuid;
36
37use crate::HttpClientRef;
38use mangadex_api_schema::v5::ChapterStatisticsResponse;
39
40#[cfg_attr(
41 feature = "deserializable-endpoint",
42 derive(serde::Deserialize, getset::Getters, getset::Setters)
43)]
44#[derive(Debug, Serialize, Clone, Builder)]
45#[serde(rename_all = "camelCase")]
46#[builder(
47 setter(into, strip_option),
48 build_fn(error = "mangadex_api_types::error::BuilderError")
49)]
50#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
51pub struct GetChapterStatistics {
52 #[doc(hidden)]
53 #[serde(skip)]
54 #[builder(pattern = "immutable")]
55 #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
56 pub http_client: HttpClientRef,
57
58 pub chapter_id: Uuid,
59}
60
61endpoint! {
62 GET ("/statistics/chapter/{}", chapter_id),
63 #[no_data] GetChapterStatistics,
66 #[flatten_result] ChapterStatisticsResponse,
67 GetChapterStatisticsBuilder
68}
69
70#[cfg(test)]
71mod tests {
72 use serde_json::json;
73 use url::Url;
74 use uuid::Uuid;
75 use wiremock::matchers::{method, path_regex};
76 use wiremock::{Mock, MockServer, ResponseTemplate};
77
78 use crate::{HttpClient, MangaDexClient};
79
80 #[tokio::test]
81 async fn find_chapter_statistics_fires_a_request_to_base_url() -> anyhow::Result<()> {
82 let mock_server = MockServer::start().await;
83 let http_client = HttpClient::builder()
84 .base_url(Url::parse(&mock_server.uri())?)
85 .build()?;
86 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
87
88 let manga_id = Uuid::new_v4();
89
90 let thread_id = 4756728;
91 let replies_count = 12;
92
93 let response_body = json!({
94 "result": "ok",
95 "statistics": {
96 manga_id.to_string(): {
97 "comments": {
98 "threadId": thread_id,
99 "repliesCount": replies_count
100 }
101 }
102 }
103 });
104
105 Mock::given(method("GET"))
106 .and(path_regex("/statistics/chapter/[0-9a-fA-F-]+"))
107 .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
108 .expect(1)
109 .mount(&mock_server)
110 .await;
111
112 let res = mangadex_client
113 .statistics()
114 .chapter()
115 .id(manga_id)
116 .get()
117 .send()
118 .await?;
119 let ctt = res.statistics.get(&manga_id).ok_or(std::io::Error::new(
120 std::io::ErrorKind::NotFound,
121 "This id is not found",
122 ))?;
123 let comments = ctt.comments.ok_or(std::io::Error::new(
124 std::io::ErrorKind::NotFound,
125 "The comment is not found",
126 ))?;
127 assert_eq!(comments.thread_id, thread_id);
128 assert_eq!(comments.replies_count, replies_count);
129 Ok(())
130 }
131}