mithril_aggregator_client/query/get/
get_cardano_database.rs

1use async_trait::async_trait;
2use reqwest::StatusCode;
3
4use mithril_common::messages::CardanoDatabaseSnapshotMessage;
5
6use crate::AggregatorHttpClientResult;
7use crate::query::{AggregatorQuery, QueryContext, QueryMethod, ResponseExt};
8
9/// Query to get the details of a Cardano database v2 snapshot
10pub struct GetCardanoDatabaseQuery {
11    hash: String,
12}
13
14impl GetCardanoDatabaseQuery {
15    /// Instantiate a query to get Cardano database v2 snapshot by hash
16    pub fn by_hash<H: Into<String>>(hash: H) -> Self {
17        Self { hash: hash.into() }
18    }
19}
20
21#[cfg_attr(target_family = "wasm", async_trait(?Send))]
22#[cfg_attr(not(target_family = "wasm"), async_trait)]
23impl AggregatorQuery for GetCardanoDatabaseQuery {
24    type Response = Option<CardanoDatabaseSnapshotMessage>;
25    type Body = ();
26
27    fn method() -> QueryMethod {
28        QueryMethod::Get
29    }
30
31    fn route(&self) -> String {
32        format!("artifact/cardano-database/{}", self.hash)
33    }
34
35    async fn handle_response(
36        &self,
37        context: QueryContext,
38    ) -> AggregatorHttpClientResult<Self::Response> {
39        match context.response.status() {
40            StatusCode::OK => context.response.parse_json_option().await,
41            StatusCode::NOT_FOUND => Ok(None),
42            _ => Err(context.unhandled_status_code().await),
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use serde_json::json;
50
51    use mithril_common::test::double::Dummy;
52
53    use crate::error::AggregatorHttpClientError;
54    use crate::test::{assert_error_matches, setup_server_and_client};
55
56    use super::*;
57
58    #[tokio::test]
59    async fn test_cardano_database_snapshot_ok_200() {
60        let (server, client) = setup_server_and_client();
61        let expected_message = CardanoDatabaseSnapshotMessage::dummy();
62        let _server_mock = server.mock(|when, then| {
63            when.path(format!(
64                "/artifact/cardano-database/{}",
65                expected_message.hash
66            ));
67            then.status(200).body(json!(expected_message).to_string());
68        });
69
70        let fetched_message = client
71            .send(GetCardanoDatabaseQuery::by_hash(&expected_message.hash))
72            .await
73            .unwrap();
74
75        assert_eq!(Some(expected_message), fetched_message);
76    }
77
78    #[tokio::test]
79    async fn test_cardano_database_snapshot_ok_404() {
80        let (server, client) = setup_server_and_client();
81        let _server_mock = server.mock(|when, then| {
82            when.any_request();
83            then.status(404);
84        });
85
86        let fetched_message = client
87            .send(GetCardanoDatabaseQuery::by_hash("whatever"))
88            .await
89            .unwrap();
90
91        assert_eq!(None, fetched_message);
92    }
93
94    #[tokio::test]
95    async fn test_cardano_database_snapshot_ko_500() {
96        let (server, client) = setup_server_and_client();
97        let _server_mock = server.mock(|when, then| {
98            when.any_request();
99            then.status(500).body("an error occurred");
100        });
101
102        let error = client
103            .send(GetCardanoDatabaseQuery::by_hash("whatever"))
104            .await
105            .unwrap_err();
106
107        assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_));
108    }
109}