mangadex_api/v5/author/id/
get.rs

1//! Builder for the author view endpoint.
2//!
3//! <https://api.mangadex.org/docs/swagger.html#/Author/get-author-id>
4//!
5//! # Examples
6//!
7//! ```rust
8//! use uuid::Uuid;
9//!
10//! use mangadex_api::v5::MangaDexClient;
11//!
12//! # async fn run() -> anyhow::Result<()> {
13//! let client = MangaDexClient::default();
14//!
15//! let author_id = Uuid::new_v4();
16//! let res = client
17//!     .author()
18//!     .id(author_id)
19//!     .get()
20//!     .send()
21//!     .await?;
22//!
23//! println!("author view: {:?}", res);
24//! # Ok(())
25//! # }
26//! ```
27
28use derive_builder::Builder;
29use serde::Serialize;
30use uuid::Uuid;
31
32use crate::HttpClientRef;
33use mangadex_api_schema::v5::AuthorResponse;
34use mangadex_api_types::ReferenceExpansionResource;
35
36#[cfg_attr(
37    feature = "deserializable-endpoint",
38    derive(serde::Deserialize, getset::Getters, getset::Setters)
39)]
40#[derive(Debug, Serialize, Clone, Builder)]
41#[serde(rename_all = "camelCase")]
42#[builder(
43    setter(into, strip_option),
44    build_fn(error = "mangadex_api_types::error::BuilderError")
45)]
46pub struct GetAuthor {
47    /// This should never be set manually as this is only for internal use.
48    #[doc(hidden)]
49    #[serde(skip)]
50    #[builder(pattern = "immutable")]
51    #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
52    pub http_client: HttpClientRef,
53
54    #[serde(skip_serializing)]
55    pub author_id: Uuid,
56
57    #[builder(setter(each = "include"), default)]
58    pub includes: Vec<ReferenceExpansionResource>,
59}
60
61endpoint! {
62    GET ("/author/{}", author_id),
63    #[query] GetAuthor,
64    #[flatten_result] AuthorResponse,
65    GetAuthorBuilder
66}
67
68#[cfg(test)]
69mod tests {
70    use fake::faker::lorem::en::Sentence;
71    use fake::faker::name::en::Name;
72    use fake::Fake;
73    use serde_json::json;
74    use time::OffsetDateTime;
75    use url::Url;
76    use uuid::Uuid;
77    use wiremock::matchers::{method, path_regex};
78    use wiremock::{Mock, MockServer, ResponseTemplate};
79
80    use crate::{HttpClient, MangaDexClient};
81    use mangadex_api_types::MangaDexDateTime;
82
83    #[tokio::test]
84    async fn get_author_fires_a_request_to_base_url() -> anyhow::Result<()> {
85        let mock_server = MockServer::start().await;
86        let http_client = HttpClient::builder()
87            .base_url(Url::parse(&mock_server.uri())?)
88            .build()?;
89        let mangadex_client = MangaDexClient::new_with_http_client(http_client);
90
91        let author_id = Uuid::new_v4();
92        let author_name: String = Name().fake();
93        let author_biography: String = Sentence(1..2).fake();
94
95        let datetime = MangaDexDateTime::new(&OffsetDateTime::now_utc());
96
97        let response_body = json!({
98            "result": "ok",
99            "response": "entity",
100            "data": {
101                "id": author_id,
102                "type": "author",
103                "attributes": {
104                    "name": author_name,
105                    "imageUrl": "",
106                    "biography": {
107                        "en": author_biography,
108                    },
109                    "twitter": null,
110                    "pixiv": null,
111                    "melonBook": null,
112                    "fanBox": null,
113                    "booth": null,
114                    "nicoVideo": null,
115                    "skeb": null,
116                    "fantia": null,
117                    "tumblr": null,
118                    "youtube": null,
119                    "weibo": null,
120                    "naver": null,
121                    "website": null,
122                    "version": 1,
123                    "createdAt": datetime.to_string(),
124                    "updatedAt": datetime.to_string(),
125                },
126                "relationships": []
127            }
128        });
129
130        Mock::given(method("GET"))
131            .and(path_regex(r"/author/[0-9a-fA-F-]+"))
132            .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
133            .expect(1)
134            .mount(&mock_server)
135            .await;
136
137        mangadex_client.author().id(author_id).get().send().await?;
138
139        Ok(())
140    }
141}