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