mangadex_api/v5/user/history/
get.rs1use derive_builder::Builder;
39use serde::Serialize;
40
41use crate::HttpClientRef;
42use mangadex_api_schema::v5::UserHistoryResponse;
43
44#[cfg_attr(
45 feature = "deserializable-endpoint",
46 derive(serde::Deserialize, getset::Getters, getset::Setters)
47)]
48#[derive(Debug, Serialize, Clone, Builder, Default)]
49#[serde(rename_all = "camelCase")]
50#[builder(
51 setter(into, strip_option),
52 default,
53 build_fn(error = "mangadex_api_types::error::BuilderError")
54)]
55pub struct GetUserHistory {
56 #[doc(hidden)]
58 #[serde(skip)]
59 #[builder(pattern = "immutable")]
60 #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
61 pub http_client: HttpClientRef,
62}
63
64endpoint! {
65 GET "/user/history",
66 #[query auth] GetUserHistory,
67 #[flatten_result] UserHistoryResponse,
68 GetUserHistoryBuilder
69}
70
71#[cfg(test)]
72mod tests {
73 use mangadex_api_schema::v5::user_history::UserHistory;
74 use mangadex_api_types::MangaDexDateTime;
75 use serde_json::json;
76 use url::Url;
77 use uuid::Uuid;
78 use wiremock::matchers::{header, method, path_regex};
79 use wiremock::{Mock, MockServer, ResponseTemplate};
80
81 use crate::v5::AuthTokens;
82 use crate::{HttpClient, MangaDexClient};
83
84 #[tokio::test]
85 async fn get_user_history_fires_a_request_to_base_url() -> anyhow::Result<()> {
86 let mock_server = MockServer::start().await;
87 let http_client = HttpClient::builder()
88 .base_url(Url::parse(&mock_server.uri())?)
89 .auth_tokens(AuthTokens {
90 session: "sessiontoken".to_string(),
91 refresh: "refreshtoken".to_string(),
92 })
93 .build()?;
94 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
95 let chapid = Uuid::new_v4();
96 let date: MangaDexDateTime = MangaDexDateTime::default();
97 let response_body = json!({
98 "result": "ok",
99 "ratings": [
100 {
101 "chapterId": chapid,
102 "readDate": date.to_string()
103 }
104 ]
105 });
106
107 println!("{:?}", response_body);
108
109 Mock::given(method("GET"))
110 .and(path_regex(r"/user/history"))
111 .and(header("Authorization", "Bearer sessiontoken"))
112 .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
113 .expect(1)
114 .mount(&mock_server)
115 .await;
116
117 let res: UserHistory = mangadex_client.user().history().get().send().await?;
118 let rating = res.ratings.first().ok_or(std::io::Error::new(
119 std::io::ErrorKind::NotFound,
120 "Entry 0 not found",
121 ))?;
122 assert_eq!(rating.chapter_id, chapid);
123 assert_eq!(rating.read_date.to_string(), date.to_string());
124
125 Ok(())
126 }
127}