mithril_aggregator_client/query/get/
get_cardano_transactions_list.rs

1use async_trait::async_trait;
2use reqwest::StatusCode;
3
4use mithril_common::messages::CardanoTransactionSnapshotListMessage;
5
6use crate::AggregatorHttpClientResult;
7use crate::query::{AggregatorQuery, QueryContext, QueryMethod, ResponseExt};
8
9/// Query to get a list of Cardano transactions snapshots
10pub struct GetCardanoTransactionsListQuery {}
11
12impl GetCardanoTransactionsListQuery {
13    /// Instantiate a query to get the latest Cardano transactions snapshots list
14    pub fn latest() -> Self {
15        Self {}
16    }
17}
18
19#[cfg_attr(target_family = "wasm", async_trait(?Send))]
20#[cfg_attr(not(target_family = "wasm"), async_trait)]
21impl AggregatorQuery for GetCardanoTransactionsListQuery {
22    type Response = CardanoTransactionSnapshotListMessage;
23    type Body = ();
24
25    fn method() -> QueryMethod {
26        QueryMethod::Get
27    }
28
29    fn route(&self) -> String {
30        "artifact/cardano-transactions".to_string()
31    }
32
33    async fn handle_response(
34        &self,
35        context: QueryContext,
36    ) -> AggregatorHttpClientResult<Self::Response> {
37        match context.response.status() {
38            StatusCode::OK => context.response.parse_json().await,
39            _ => Err(context.unhandled_status_code().await),
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use serde_json::json;
47
48    use mithril_common::messages::CardanoTransactionSnapshotListItemMessage;
49    use mithril_common::test::double::Dummy;
50
51    use crate::AggregatorHttpClientError;
52    use crate::test::{assert_error_matches, setup_server_and_client};
53
54    use super::*;
55
56    #[tokio::test]
57    async fn test_latest_cardano_transactions_list_ok_200() {
58        let (server, client) = setup_server_and_client();
59        let expected_list = vec![
60            CardanoTransactionSnapshotListItemMessage::dummy(),
61            CardanoTransactionSnapshotListItemMessage::dummy(),
62        ];
63        let _server_mock = server.mock(|when, then| {
64            when.path("/artifact/cardano-transactions");
65            then.status(200).body(json!(expected_list).to_string());
66        });
67
68        let fetched_list = client.send(GetCardanoTransactionsListQuery::latest()).await.unwrap();
69
70        assert_eq!(expected_list, fetched_list);
71    }
72
73    #[tokio::test]
74    async fn test_latest_cardano_transactions_list_ko_500() {
75        let (server, client) = setup_server_and_client();
76        let _server_mock = server.mock(|when, then| {
77            when.any_request();
78            then.status(500).body("an error occurred");
79        });
80
81        let error = client
82            .send(GetCardanoTransactionsListQuery::latest())
83            .await
84            .unwrap_err();
85
86        assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_));
87    }
88}