mithril_aggregator_client/query/get/
get_snapshots_list.rs1use async_trait::async_trait;
2use reqwest::StatusCode;
3
4use mithril_common::messages::SnapshotListMessage;
5
6use crate::AggregatorHttpClientResult;
7use crate::query::{AggregatorQuery, QueryContext, QueryMethod, ResponseExt};
8
9pub struct GetSnapshotsListQuery {}
11
12impl GetSnapshotsListQuery {
13 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 GetSnapshotsListQuery {
22 type Response = SnapshotListMessage;
23 type Body = ();
24
25 fn method() -> QueryMethod {
26 QueryMethod::Get
27 }
28
29 fn route(&self) -> String {
30 "artifact/snapshots".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::SnapshotListItemMessage;
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_snapshots_list_ok_200() {
58 let (server, client) = setup_server_and_client();
59 let expected_list =
60 vec![SnapshotListItemMessage::dummy(), SnapshotListItemMessage::dummy()];
61 let _server_mock = server.mock(|when, then| {
62 when.path("/artifact/snapshots");
63 then.status(200).body(json!(expected_list).to_string());
64 });
65
66 let fetched_list = client.send(GetSnapshotsListQuery::latest()).await.unwrap();
67
68 assert_eq!(expected_list, fetched_list);
69 }
70
71 #[tokio::test]
72 async fn test_latest_snapshots_list_ko_500() {
73 let (server, client) = setup_server_and_client();
74 let _server_mock = server.mock(|when, then| {
75 when.any_request();
76 then.status(500).body("an error occurred");
77 });
78
79 let error = client.send(GetSnapshotsListQuery::latest()).await.unwrap_err();
80
81 assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_));
82 }
83}