Skip to content

Commit 29cc900

Browse files
committed
client issue statistic HTTP call
1 parent b2bc7fd commit 29cc900

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

mithril-client/src/aggregator_client/http_client.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ pub trait AggregatorClient: Sync + Send {
5454
/// Get the content back from the Aggregator, the URL is a relative path for a resource
5555
async fn get_content(&self, url: &str) -> Result<String, AggregatorHTTPClientError>;
5656

57+
/// Post information to the Aggregator, the URL is a relative path for a resource
58+
async fn post_content(
59+
&self,
60+
url: &str,
61+
json: &str,
62+
) -> Result<String, AggregatorHTTPClientError>;
63+
5764
/// Download and unpack large archives on the disk
5865
async fn download_unpack(
5966
&self,
@@ -149,6 +156,27 @@ impl AggregatorHTTPClient {
149156
}
150157
}
151158

159+
/// Issue a POST HTTP request.
160+
async fn post(&self, url: &str, json: &str) -> Result<Response, AggregatorHTTPClientError> {
161+
let request_builder = Client::new().post(url.to_owned()).json(json);
162+
let current_api_version = self
163+
.compute_current_api_version()
164+
.await
165+
.unwrap()
166+
.to_string();
167+
debug!("Prepare request with version: {current_api_version}");
168+
let request_builder =
169+
request_builder.header(MITHRIL_API_VERSION_HEADER, current_api_version);
170+
171+
request_builder
172+
.send()
173+
.await
174+
.map_err(|e| AggregatorHTTPClientError::SubsystemError {
175+
message: format!("Error while posting data '{json}' to URL '{url}'."),
176+
error: e.into(),
177+
})
178+
}
179+
152180
/// API version error handling
153181
async fn handle_api_error(&self, response: &Response) -> AggregatorHTTPClientError {
154182
if let Some(version) = response.headers().get(MITHRIL_API_VERSION_HEADER) {
@@ -183,6 +211,26 @@ impl AggregatorClient for AggregatorHTTPClient {
183211
})
184212
}
185213

214+
async fn post_content(
215+
&self,
216+
url: &str,
217+
json: &str,
218+
) -> Result<String, AggregatorHTTPClientError> {
219+
let url = format!("{}/{}", self.aggregator_endpoint.trim_end_matches('/'), url);
220+
let response = self.post(&url, json).await?;
221+
222+
let body =
223+
response
224+
.text()
225+
.await
226+
.map_err(|e| AggregatorHTTPClientError::SubsystemError {
227+
message: "Could not find a text body in the response.".to_string(),
228+
error: e.into(),
229+
})?;
230+
231+
Ok(body)
232+
}
233+
186234
async fn download_unpack(
187235
&self,
188236
url: &str,

mithril-client/src/aggregator_client/snapshot_client.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ impl SnapshotClient {
7575
)
7676
.await
7777
{
78-
Ok(()) => Ok(()),
78+
Ok(()) => {
79+
// the snapshot download does not fail if the statistic call fails.
80+
let _ = self.add_statistics(snapshot).await;
81+
82+
Ok(())
83+
}
7984
Err(e) => {
8085
warn!("Failed downloading snapshot from '{url}' Error: {e}.");
8186
Err(e.into())
@@ -92,4 +97,13 @@ impl SnapshotClient {
9297
}
9398
.into())
9499
}
100+
101+
/// Increments Aggregator's download statistics
102+
pub async fn add_statistics(&self, snapshot: &Snapshot) -> StdResult<()> {
103+
let url = "statistics/snapshot";
104+
let json = serde_json::to_string(snapshot)?;
105+
let _response = self.http_client.post_content(url, &json).await?;
106+
107+
Ok(())
108+
}
95109
}

mithril-client/src/services/snapshot.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ impl SnapshotService for MithrilClientSnapshotService {
260260
DownloadProgressReporter::new(pb, progress_output_type),
261261
)
262262
.await
263-
.with_context(|| format!("Could not download file in '{}'", download_dir.display()))?;
263+
.with_context(|| {
264+
format!(
265+
"Could not download file in directory '{}'",
266+
download_dir.display()
267+
)
268+
})?;
264269

265270
// Append 'clean' file to speedup node bootstrap
266271
if let Err(error) = File::create(db_dir.join("clean")) {
@@ -541,8 +546,11 @@ mod tests {
541546
let test_path = std::env::temp_dir().join("test_download_snapshot_ok");
542547
let _ = std::fs::remove_dir_all(&test_path);
543548

544-
let (http_client, certificate_verifier, digester) =
549+
let (mut http_client, certificate_verifier, digester) =
545550
get_mocks_for_snapshot_service_configured_to_make_download_succeed();
551+
http_client
552+
.expect_post_content()
553+
.returning(|_, _| Ok(String::new()));
546554

547555
let mut builder = get_dep_builder(Arc::new(http_client));
548556
builder.certificate_verifier = Some(Arc::new(certificate_verifier));
@@ -581,8 +589,11 @@ mod tests {
581589
.join("test_download_snapshot_ok_add_clean_file_allowing_node_bootstrap_speedup");
582590
let _ = std::fs::remove_dir_all(&test_path);
583591

584-
let (http_client, certificate_verifier, digester) =
592+
let (mut http_client, certificate_verifier, digester) =
585593
get_mocks_for_snapshot_service_configured_to_make_download_succeed();
594+
http_client
595+
.expect_post_content()
596+
.returning(|_, _| Ok(String::new()));
586597

587598
let mut builder = get_dep_builder(Arc::new(http_client));
588599
builder.certificate_verifier = Some(Arc::new(certificate_verifier));
@@ -625,8 +636,11 @@ mod tests {
625636
let test_path = std::env::temp_dir().join("test_download_snapshot_invalid_digest");
626637
let _ = std::fs::remove_dir_all(&test_path);
627638

628-
let (http_client, certificate_verifier, _) =
639+
let (mut http_client, certificate_verifier, _) =
629640
get_mocks_for_snapshot_service_configured_to_make_download_succeed();
641+
http_client
642+
.expect_post_content()
643+
.returning(|_, _| Ok(String::new()));
630644
let immutable_digester = DumbImmutableDigester::new("snapshot-digest-KO", true);
631645

632646
let mut dep_builder = get_dep_builder(Arc::new(http_client));

0 commit comments

Comments
 (0)