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

1//! Builder for getting a given Manga's statistics.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/Statistics/get-statistics-manga-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//! // Official Test Manga ID.
18//! let manga_id = Uuid::parse_str("f9c33607-9180-4ba6-b85c-e4b5faee7192")?;
19//!
20//! let manga_stats = client
21//!     .statistics()
22//!     .manga()
23//!     .id(manga_id)
24//!     .get()
25//!     .send()
26//!     .await?;
27//!
28//! println!("Response: {:?}", manga_stats);
29//! # Ok(())
30//! # }
31//! ```
32
33use derive_builder::Builder;
34use serde::Serialize;
35use uuid::Uuid;
36
37use crate::HttpClientRef;
38use mangadex_api_schema::v5::MangaStatisticsResponse;
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 GetMangaStatistics {
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 manga_id: Uuid,
59}
60
61endpoint! {
62    GET ("/statistics/manga/{}", manga_id),
63    // Known issue: Despite the API docs stating that authorization is required, the endpoint is
64    // available to guests.
65    #[no_data] GetMangaStatistics,
66    #[flatten_result] MangaStatisticsResponse,
67    GetMangaStatisticsBuilder
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_manga_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 response_body = json!({
91            "result": "ok",
92            "statistics": {
93                manga_id.to_string(): {
94                    "rating": {
95                        "average": 7.5,
96                        "distribution": {
97                            "1": 0,
98                            "2": 0,
99                            "3": 0,
100                            "4": 0,
101                            "5": 0,
102                            "6": 0,
103                            "7": 2,
104                            "8": 2,
105                            "9": 0,
106                            "10": 0,
107                        }
108                    },
109                    "follows": 3
110                }
111            }
112        });
113
114        Mock::given(method("GET"))
115            .and(path_regex("/statistics/manga/[0-9a-fA-F-]+"))
116            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
117            .expect(1)
118            .mount(&mock_server)
119            .await;
120
121        let res = mangadex_client
122            .statistics()
123            .manga()
124            .id(manga_id)
125            .get()
126            .send()
127            .await?;
128
129        let manga_stats = res.statistics.get(&manga_id).unwrap();
130        assert_eq!(manga_stats.rating.average, Some(7.5));
131        assert_eq!(manga_stats.rating.distribution.r1, 0);
132        assert_eq!(manga_stats.rating.distribution.r2, 0);
133        assert_eq!(manga_stats.rating.distribution.r3, 0);
134        assert_eq!(manga_stats.rating.distribution.r4, 0);
135        assert_eq!(manga_stats.rating.distribution.r5, 0);
136        assert_eq!(manga_stats.rating.distribution.r6, 0);
137        assert_eq!(manga_stats.rating.distribution.r7, 2);
138        assert_eq!(manga_stats.rating.distribution.r8, 2);
139        assert_eq!(manga_stats.rating.distribution.r9, 0);
140        assert_eq!(manga_stats.rating.distribution.r10, 0);
141        assert_eq!(manga_stats.follows, 3);
142
143        Ok(())
144    }
145}