mithril_aggregator_client/query/get/
get_cardano_transaction.rs1use async_trait::async_trait;
2use reqwest::StatusCode;
3
4use mithril_common::messages::CardanoTransactionSnapshotMessage;
5
6use crate::AggregatorHttpClientResult;
7use crate::query::{AggregatorQuery, QueryContext, QueryMethod, ResponseExt};
8
9pub struct GetCardanoTransactionQuery {
11 hash: String,
12}
13
14impl GetCardanoTransactionQuery {
15 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 GetCardanoTransactionQuery {
24 type Response = Option<CardanoTransactionSnapshotMessage>;
25 type Body = ();
26
27 fn method() -> QueryMethod {
28 QueryMethod::Get
29 }
30
31 fn route(&self) -> String {
32 format!("artifact/cardano-transaction/{}", 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::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_transaction_details_ok_200() {
60 let (server, client) = setup_server_and_client();
61 let expected_message = CardanoTransactionSnapshotMessage::dummy();
62 let _server_mock = server.mock(|when, then| {
63 when.path(format!(
64 "/artifact/cardano-transaction/{}",
65 expected_message.hash
66 ));
67 then.status(200).body(json!(expected_message).to_string());
68 });
69
70 let fetched_message = client
71 .send(GetCardanoTransactionQuery::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_transaction_details_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(GetCardanoTransactionQuery::by_hash("whatever"))
88 .await
89 .unwrap();
90
91 assert_eq!(None, fetched_message);
92 }
93
94 #[tokio::test]
95 async fn test_cardano_transaction_details_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(GetCardanoTransactionQuery::by_hash("whatever"))
104 .await
105 .unwrap_err();
106
107 assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_));
108 }
109}