mangadex_api/v5/statistics/manga/
get.rs

1//! Builder for finding Manga statistics.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/Statistics/get-statistics-manga>
4//!
5//! This allows querying for multiple 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//!     .get()
24//!     .manga_id(&manga_id)
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, Default)]
45#[serde(rename_all = "camelCase")]
46#[builder(
47    setter(into, strip_option),
48    build_fn(error = "mangadex_api_types::error::BuilderError"),
49    default
50)]
51#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
52pub struct FindMangaStatistics {
53    #[doc(hidden)]
54    #[serde(skip)]
55    #[builder(pattern = "immutable")]
56    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
57    pub http_client: HttpClientRef,
58
59    #[builder(setter(each = "manga_id"))]
60    pub manga: Vec<Uuid>,
61}
62
63endpoint! {
64    GET "/statistics/manga",
65    // Known issue: Despite the API docs stating that authorization is required, the endpoint is
66    // available to guests.
67    #[query] FindMangaStatistics,
68    #[flatten_result] MangaStatisticsResponse,
69    FindMangaStatisticsBuilder
70}
71
72#[cfg(test)]
73mod tests {
74    use serde_json::json;
75    use url::Url;
76    use uuid::Uuid;
77    use wiremock::matchers::{method, path};
78    use wiremock::{Mock, MockServer, ResponseTemplate};
79
80    use crate::{HttpClient, MangaDexClient};
81
82    #[tokio::test]
83    async fn find_manga_statistics_fires_a_request_to_base_url() -> anyhow::Result<()> {
84        let mock_server = MockServer::start().await;
85        let http_client = HttpClient::builder()
86            .base_url(Url::parse(&mock_server.uri())?)
87            .build()?;
88        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
89
90        let manga_id = Uuid::new_v4();
91
92        let response_body = json!({
93            "result": "ok",
94            "statistics": {
95                manga_id.to_string(): {
96                    "rating": {
97                        "average": 7.5
98                    },
99                    "follows": 3
100                }
101            }
102        });
103
104        Mock::given(method("GET"))
105            .and(path("/statistics/manga"))
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            .manga()
114            .get()
115            .manga_id(&manga_id)
116            .send()
117            .await?;
118
119        let manga_stats = res.statistics.get(&manga_id).unwrap();
120        assert_eq!(manga_stats.rating.average, Some(7.5));
121        assert_eq!(manga_stats.rating.distribution.r1, 0);
122        assert_eq!(manga_stats.rating.distribution.r2, 0);
123        assert_eq!(manga_stats.rating.distribution.r3, 0);
124        assert_eq!(manga_stats.rating.distribution.r4, 0);
125        assert_eq!(manga_stats.rating.distribution.r5, 0);
126        assert_eq!(manga_stats.rating.distribution.r6, 0);
127        assert_eq!(manga_stats.rating.distribution.r7, 0);
128        assert_eq!(manga_stats.rating.distribution.r8, 0);
129        assert_eq!(manga_stats.rating.distribution.r9, 0);
130        assert_eq!(manga_stats.rating.distribution.r10, 0);
131        assert_eq!(manga_stats.follows, 3);
132
133        Ok(())
134    }
135}