mangadex_api/v5/statistics/group/id/
get.rs

1//! Builder for getting a given group's statistics.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/Statistics/get-statistics-group-uuid>
4//!
5//! This only gets statistics for a single Manga.
6//!
7//! # Examples
8//!
9//! ```rust
10//! use mangadex_api_types::MangaStatus;
11//! use mangadex_api::v5::MangaDexClient;
12//! use uuid::Uuid;
13//!
14//! # async fn run() -> anyhow::Result<()> {
15//! let client = MangaDexClient::default();
16//!
17//! // Galaxy Degen Scans
18//! let group_id = Uuid::parse_str("ab24085f-b16c-4029-8c05-38fe16592a85")?;
19//!
20//! let group_stats = client
21//!     .statistics()
22//!     .group()
23//!     .id(group_id)
24//!     .get()
25//!     .send()
26//!     .await?;
27//!
28//! println!("Response: {:?}", group_stats);
29//! # Ok(())
30//! # }
31//! ```
32use derive_builder::Builder;
33use serde::Serialize;
34use uuid::Uuid;
35
36use crate::HttpClientRef;
37use mangadex_api_schema::v5::GroupStatisticsResponse;
38
39#[cfg_attr(
40    feature = "deserializable-endpoint",
41    derive(serde::Deserialize, getset::Getters, getset::Setters)
42)]
43#[derive(Debug, Serialize, Clone, Builder)]
44#[serde(rename_all = "camelCase")]
45#[builder(
46    setter(into, strip_option),
47    build_fn(error = "mangadex_api_types::error::BuilderError")
48)]
49#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
50pub struct GetGroupStatistics {
51    #[doc(hidden)]
52    #[serde(skip)]
53    #[builder(pattern = "immutable")]
54    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
55    pub http_client: HttpClientRef,
56
57    pub group_id: Uuid,
58}
59
60endpoint! {
61    GET ("/statistics/group/{}", group_id),
62    // Known issue: Despite the API docs stating that authorization is required, the endpoint is
63    // available to guests.
64    #[no_data] GetGroupStatistics,
65    #[flatten_result] GroupStatisticsResponse,
66    GetGroupStatisticsBuilder
67}
68
69#[cfg(test)]
70mod tests {
71    use serde_json::json;
72    use url::Url;
73    use uuid::Uuid;
74    use wiremock::matchers::{method, path_regex};
75    use wiremock::{Mock, MockServer, ResponseTemplate};
76
77    use crate::{HttpClient, MangaDexClient};
78
79    #[tokio::test]
80    async fn find_group_statistics_fires_a_request_to_base_url() -> anyhow::Result<()> {
81        let mock_server = MockServer::start().await;
82        let http_client = HttpClient::builder()
83            .base_url(Url::parse(&mock_server.uri())?)
84            .build()?;
85        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
86
87        let manga_id = Uuid::new_v4();
88
89        let thread_id = 4756728;
90        let replies_count = 12;
91
92        let response_body = json!({
93            "result": "ok",
94            "statistics": {
95                manga_id.to_string(): {
96                    "comments": {
97                      "threadId": thread_id,
98                      "repliesCount": replies_count
99                    }
100                }
101            }
102        });
103
104        Mock::given(method("GET"))
105            .and(path_regex("/statistics/group/[0-9a-fA-F-]+"))
106            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
107            .expect(1)
108            .mount(&mock_server)
109            .await;
110
111        let res = mangadex_client
112            .statistics()
113            .group()
114            .id(manga_id)
115            .get()
116            .send()
117            .await?;
118        let ctt = res.statistics.get(&manga_id).ok_or(std::io::Error::new(
119            std::io::ErrorKind::NotFound,
120            "This id is not found",
121        ))?;
122        let comments = ctt.comments.ok_or(std::io::Error::new(
123            std::io::ErrorKind::NotFound,
124            "The comment is not found",
125        ))?;
126        assert_eq!(comments.thread_id, thread_id);
127        assert_eq!(comments.replies_count, replies_count);
128        Ok(())
129    }
130}