Skip to content

Commit 32891c9

Browse files
author
Damien LACHAUME / PALO-IT
committed
PR review: move http request body into AggregatorRequest
1 parent 5f4ff94 commit 32891c9

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

mithril-client/src/aggregator_client.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ pub enum AggregatorRequest {
7070
ListSnapshots,
7171

7272
/// Increments the aggregator snapshot download statistics
73-
IncrementSnapshotStatistic,
73+
IncrementSnapshotStatistic {
74+
/// Snapshot as HTTP request body
75+
snapshot: String,
76+
},
7477
}
7578

7679
impl AggregatorRequest {
@@ -91,7 +94,19 @@ impl AggregatorRequest {
9194
format!("artifact/snapshot/{}", digest)
9295
}
9396
AggregatorRequest::ListSnapshots => "artifact/snapshots".to_string(),
94-
AggregatorRequest::IncrementSnapshotStatistic => "statistics/snapshot".to_string(),
97+
AggregatorRequest::IncrementSnapshotStatistic { snapshot: _ } => {
98+
"statistics/snapshot".to_string()
99+
}
100+
}
101+
}
102+
103+
/// Get the request body to send to the aggregator
104+
pub fn get_body(&self) -> Option<String> {
105+
match self {
106+
AggregatorRequest::IncrementSnapshotStatistic { snapshot } => {
107+
Some(snapshot.to_string())
108+
}
109+
_ => None,
95110
}
96111
}
97112
}
@@ -110,7 +125,6 @@ pub trait AggregatorClient: Sync + Send {
110125
async fn post_content(
111126
&self,
112127
request: AggregatorRequest,
113-
content: &str,
114128
) -> Result<String, AggregatorClientError>;
115129
}
116130

@@ -312,10 +326,12 @@ impl AggregatorClient for AggregatorHTTPClient {
312326
async fn post_content(
313327
&self,
314328
request: AggregatorRequest,
315-
content: &str,
316329
) -> Result<String, AggregatorClientError> {
317330
let response = self
318-
.post(self.get_url_for_route(&request.route())?, content)
331+
.post(
332+
self.get_url_for_route(&request.route())?,
333+
&request.get_body().unwrap_or_default(),
334+
)
319335
.await?;
320336

321337
response.text().await.map_err(|e| {

mithril-client/src/snapshot_client.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,9 @@ impl SnapshotClient {
243243
pub async fn add_statistics(&self, snapshot: &Snapshot) -> MithrilResult<()> {
244244
let _response = self
245245
.aggregator_client
246-
.post_content(
247-
AggregatorRequest::IncrementSnapshotStatistic,
248-
&serde_json::to_string(snapshot)?,
249-
)
246+
.post_content(AggregatorRequest::IncrementSnapshotStatistic {
247+
snapshot: serde_json::to_string(snapshot)?,
248+
})
250249
.await?;
251250

252251
Ok(())

mithril-client/tests/snapshot_list_get_show_download_verify.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ async fn snapshot_list_get_show_download_verify() {
103103
fake_aggregator.get_last_call().await,
104104
Some(format!(
105105
"/{}",
106-
AggregatorRequest::IncrementSnapshotStatistic.route()
106+
AggregatorRequest::IncrementSnapshotStatistic {
107+
snapshot: "whatever".to_string()
108+
}
109+
.route()
107110
))
108111
);
109112

0 commit comments

Comments
 (0)