Skip to content

Commit fe9f479

Browse files
committed
refactor(aggregator-client): rename client and error types to include Http
This makes clear that this client is Http focused only and can't be extended as is to support other protocols.
1 parent d9cf7a1 commit fe9f479

File tree

7 files changed

+60
-55
lines changed

7 files changed

+60
-55
lines changed

internal/mithril-aggregator-client/src/builder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::time::Duration;
88
use mithril_common::StdResult;
99
use mithril_common::api_version::APIVersionProvider;
1010

11-
use crate::client::AggregatorClient;
11+
use crate::client::AggregatorHttpClient;
1212

13-
/// A builder of [AggregatorClient]
13+
/// A builder of [AggregatorHttpClient]
1414
pub struct AggregatorClientBuilder {
1515
aggregator_url_result: reqwest::Result<Url>,
1616
api_version_provider: Option<Arc<APIVersionProvider>>,
@@ -68,8 +68,8 @@ impl AggregatorClientBuilder {
6868
self
6969
}
7070

71-
/// Returns an [AggregatorClient] based on the builder configuration
72-
pub fn build(self) -> StdResult<AggregatorClient> {
71+
/// Returns an [AggregatorHttpClient] based on the builder configuration
72+
pub fn build(self) -> StdResult<AggregatorHttpClient> {
7373
let aggregator_endpoint =
7474
enforce_trailing_slash(self.aggregator_url_result.with_context(
7575
|| "Invalid aggregator endpoint, it must be a correctly formed url",
@@ -84,7 +84,7 @@ impl AggregatorClientBuilder {
8484
.proxy(Proxy::all(relay_endpoint).with_context(|| "Relay proxy creation failed")?)
8585
}
8686

87-
Ok(AggregatorClient {
87+
Ok(AggregatorHttpClient {
8888
aggregator_endpoint,
8989
api_version_provider,
9090
additional_headers: (&additional_headers)

internal/mithril-aggregator-client/src/client.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use std::time::Duration;
88
use mithril_common::MITHRIL_API_VERSION_HEADER;
99
use mithril_common::api_version::APIVersionProvider;
1010

11-
use crate::AggregatorClientResult;
11+
use crate::AggregatorHttpClientResult;
1212
use crate::builder::AggregatorClientBuilder;
13-
use crate::error::AggregatorClientError;
13+
use crate::error::AggregatorHttpClientError;
1414
use crate::query::{AggregatorQuery, QueryContext, QueryMethod};
1515

1616
const API_VERSION_MISMATCH_WARNING_MESSAGE: &str = "OpenAPI version may be incompatible, please update Mithril client library to the latest version.";
1717

1818
/// A client to send HTTP requests to a Mithril Aggregator
19-
pub struct AggregatorClient {
19+
pub struct AggregatorHttpClient {
2020
pub(super) aggregator_endpoint: Url,
2121
pub(super) api_version_provider: Arc<APIVersionProvider>,
2222
pub(super) additional_headers: HeaderMap,
@@ -25,7 +25,7 @@ pub struct AggregatorClient {
2525
pub(super) logger: Logger,
2626
}
2727

28-
impl AggregatorClient {
28+
impl AggregatorHttpClient {
2929
/// Creates a [AggregatorClientBuilder] to configure a `AggregatorClient`.
3030
//
3131
// This is the same as `AggregatorClient::builder()`.
@@ -34,7 +34,10 @@ impl AggregatorClient {
3434
}
3535

3636
/// Send the given query to the Mithril Aggregator
37-
pub async fn send<Q: AggregatorQuery>(&self, query: Q) -> AggregatorClientResult<Q::Response> {
37+
pub async fn send<Q: AggregatorQuery>(
38+
&self,
39+
query: Q,
40+
) -> AggregatorHttpClientResult<Q::Response> {
3841
// Todo: error handling ? Reuse the version in `warn_if_api_version_mismatch` ?
3942
let current_api_version = self.api_version_provider.compute_current_version().unwrap();
4043
let mut request_builder = match Q::method() {
@@ -62,11 +65,13 @@ impl AggregatorClient {
6265
};
6366
query.handle_response(context).await
6467
}
65-
Err(err) => Err(AggregatorClientError::RemoteServerUnreachable(anyhow!(err))),
68+
Err(err) => Err(AggregatorHttpClientError::RemoteServerUnreachable(anyhow!(
69+
err
70+
))),
6671
}
6772
}
6873

69-
fn join_aggregator_endpoint(&self, endpoint: &str) -> AggregatorClientResult<Url> {
74+
fn join_aggregator_endpoint(&self, endpoint: &str) -> AggregatorHttpClientResult<Url> {
7075
self.aggregator_endpoint
7176
.join(endpoint)
7277
.with_context(|| {
@@ -75,7 +80,7 @@ impl AggregatorClient {
7580
self.aggregator_endpoint
7681
)
7782
})
78-
.map_err(AggregatorClientError::InvalidEndpoint)
83+
.map_err(AggregatorHttpClientError::InvalidEndpoint)
7984
}
8085

8186
/// Check API version mismatch and log a warning if the aggregator's version is more recent.
@@ -141,13 +146,13 @@ mod tests {
141146
async fn handle_response(
142147
&self,
143148
context: QueryContext,
144-
) -> AggregatorClientResult<Self::Response> {
149+
) -> AggregatorHttpClientResult<Self::Response> {
145150
match context.response.status() {
146151
StatusCode::OK => context
147152
.response
148153
.json::<TestResponse>()
149154
.await
150-
.map_err(|err| AggregatorClientError::JsonParseFailed(anyhow!(err))),
155+
.map_err(|err| AggregatorHttpClientError::JsonParseFailed(anyhow!(err))),
151156
_ => Err(context.unhandled_status_code().await),
152157
}
153158
}
@@ -192,7 +197,7 @@ mod tests {
192197
async fn handle_response(
193198
&self,
194199
context: QueryContext,
195-
) -> AggregatorClientResult<Self::Response> {
200+
) -> AggregatorHttpClientResult<Self::Response> {
196201
match context.response.status() {
197202
StatusCode::CREATED => Ok(()),
198203
_ => Err(context.unhandled_status_code().await),
@@ -293,7 +298,7 @@ mod tests {
293298
let error = client.send(TestGetQuery).await.expect_err("should not fail");
294299

295300
assert!(
296-
matches!(error, AggregatorClientError::RemoteServerUnreachable(_)),
301+
matches!(error, AggregatorHttpClientError::RemoteServerUnreachable(_)),
297302
"unexpected error type: {error:?}"
298303
);
299304
}
@@ -337,7 +342,7 @@ mod tests {
337342
let aggregator_version = "2.0.0";
338343
let client_version = "1.0.0";
339344
let (logger, log_inspector) = TestLogger::memory();
340-
let client = AggregatorClient::builder("http://whatever")
345+
let client = AggregatorHttpClient::builder("http://whatever")
341346
.with_logger(logger)
342347
.with_api_version_provider(Arc::new(APIVersionProvider::new_with_default_version(
343348
Version::parse(client_version).unwrap(),
@@ -361,7 +366,7 @@ mod tests {
361366
fn test_no_warning_logged_when_versions_match() {
362367
let version = "1.0.0";
363368
let (logger, log_inspector) = TestLogger::memory();
364-
let client = AggregatorClient::builder("http://whatever")
369+
let client = AggregatorHttpClient::builder("http://whatever")
365370
.with_logger(logger)
366371
.with_api_version_provider(Arc::new(APIVersionProvider::new_with_default_version(
367372
Version::parse(version).unwrap(),
@@ -380,7 +385,7 @@ mod tests {
380385
let aggregator_version = "1.0.0";
381386
let client_version = "2.0.0";
382387
let (logger, log_inspector) = TestLogger::memory();
383-
let client = AggregatorClient::builder("http://whatever")
388+
let client = AggregatorHttpClient::builder("http://whatever")
384389
.with_logger(logger)
385390
.with_api_version_provider(Arc::new(APIVersionProvider::new_with_default_version(
386391
Version::parse(client_version).unwrap(),
@@ -403,7 +408,7 @@ mod tests {
403408
#[test]
404409
fn test_does_not_log_or_fail_when_header_is_missing() {
405410
let (logger, log_inspector) = TestLogger::memory();
406-
let client = AggregatorClient::builder("http://whatever")
411+
let client = AggregatorHttpClient::builder("http://whatever")
407412
.with_logger(logger)
408413
.with_api_version_provider(Arc::new(APIVersionProvider::default()))
409414
.build()
@@ -419,7 +424,7 @@ mod tests {
419424
#[test]
420425
fn test_does_not_log_or_fail_when_header_is_not_a_version() {
421426
let (logger, log_inspector) = TestLogger::memory();
422-
let client = AggregatorClient::builder("http://whatever")
427+
let client = AggregatorHttpClient::builder("http://whatever")
423428
.with_logger(logger)
424429
.with_api_version_provider(Arc::new(APIVersionProvider::default()))
425430
.build()
@@ -435,7 +440,7 @@ mod tests {
435440
#[test]
436441
fn test_logs_error_when_client_version_cannot_be_computed() {
437442
let (logger, log_inspector) = TestLogger::memory();
438-
let client = AggregatorClient::builder("http://whatever")
443+
let client = AggregatorHttpClient::builder("http://whatever")
439444
.with_logger(logger)
440445
.with_api_version_provider(Arc::new(APIVersionProvider::new_failing()))
441446
.build()

internal/mithril-aggregator-client/src/error.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::JSON_CONTENT_TYPE;
99

1010
/// Error structure for the Aggregator Client.
1111
#[derive(Error, Debug)]
12-
pub enum AggregatorClientError {
12+
pub enum AggregatorHttpClientError {
1313
/// Error raised when querying the aggregator returned a 5XX error.
1414
#[error("Internal error of the Aggregator")]
1515
RemoteServerTechnical(#[source] StdError),
@@ -39,7 +39,7 @@ pub enum AggregatorClientError {
3939
RegistrationRoundNotYetOpened(#[source] StdError),
4040
}
4141

42-
impl AggregatorClientError {
42+
impl AggregatorHttpClientError {
4343
/// Create an `AggregatorClientError` from a response.
4444
///
4545
/// This method is meant to be used after handling domain-specific cases leaving only
@@ -133,12 +133,12 @@ mod tests {
133133
#[tokio::test]
134134
async fn test_4xx_errors_are_handled_as_remote_server_logical() {
135135
let response = build_text_response(StatusCode::BAD_REQUEST, "error text");
136-
let handled_error = AggregatorClientError::from_response(response).await;
136+
let handled_error = AggregatorHttpClientError::from_response(response).await;
137137

138138
assert!(
139139
matches!(
140140
handled_error,
141-
AggregatorClientError::RemoteServerLogical(..)
141+
AggregatorHttpClientError::RemoteServerLogical(..)
142142
),
143143
"Expected error to be RemoteServerLogical\ngot '{handled_error:?}'",
144144
);
@@ -147,12 +147,12 @@ mod tests {
147147
#[tokio::test]
148148
async fn test_5xx_errors_are_handled_as_remote_server_technical() {
149149
let response = build_text_response(StatusCode::INTERNAL_SERVER_ERROR, "error text");
150-
let handled_error = AggregatorClientError::from_response(response).await;
150+
let handled_error = AggregatorHttpClientError::from_response(response).await;
151151

152152
assert!(
153153
matches!(
154154
handled_error,
155-
AggregatorClientError::RemoteServerTechnical(..)
155+
AggregatorHttpClientError::RemoteServerTechnical(..)
156156
),
157157
"Expected error to be RemoteServerLogical\ngot '{handled_error:?}'",
158158
);
@@ -161,12 +161,12 @@ mod tests {
161161
#[tokio::test]
162162
async fn test_550_error_is_handled_as_registration_round_not_yet_opened() {
163163
let response = build_text_response(StatusCode::from_u16(550).unwrap(), "Not yet available");
164-
let handled_error = AggregatorClientError::from_response(response).await;
164+
let handled_error = AggregatorHttpClientError::from_response(response).await;
165165

166166
assert!(
167167
matches!(
168168
handled_error,
169-
AggregatorClientError::RegistrationRoundNotYetOpened(..)
169+
AggregatorHttpClientError::RegistrationRoundNotYetOpened(..)
170170
),
171171
"Expected error to be RegistrationRoundNotYetOpened\ngot '{handled_error:?}'",
172172
);
@@ -176,12 +176,12 @@ mod tests {
176176
async fn test_non_4xx_or_5xx_errors_are_handled_as_unhandled_status_code_and_contains_response_text()
177177
{
178178
let response = build_text_response(StatusCode::OK, "ok text");
179-
let handled_error = AggregatorClientError::from_response(response).await;
179+
let handled_error = AggregatorHttpClientError::from_response(response).await;
180180

181181
assert!(
182182
matches!(
183183
handled_error,
184-
AggregatorClientError::UnhandledStatusCode(..) if format!("{handled_error:?}").contains("ok text")
184+
AggregatorHttpClientError::UnhandledStatusCode(..) if format!("{handled_error:?}").contains("ok text")
185185
),
186186
"Expected error to be UnhandledStatusCode with 'ok text' in error text\ngot '{handled_error:?}'",
187187
);
@@ -193,7 +193,7 @@ mod tests {
193193
let response = build_text_response(StatusCode::EXPECTATION_FAILED, error_text);
194194

195195
assert_error_text_contains!(
196-
AggregatorClientError::get_root_cause(response).await,
196+
AggregatorHttpClientError::get_root_cause(response).await,
197197
"expectation failed: An error occurred; please try again later."
198198
);
199199
}
@@ -205,7 +205,7 @@ mod tests {
205205
let response = build_json_response(StatusCode::BAD_REQUEST, &client_error);
206206

207207
assert_error_text_contains!(
208-
AggregatorClientError::get_root_cause(response).await,
208+
AggregatorHttpClientError::get_root_cause(response).await,
209209
"bad request: label: message"
210210
);
211211
}
@@ -217,7 +217,7 @@ mod tests {
217217
let response = build_json_response(StatusCode::BAD_REQUEST, &server_error);
218218

219219
assert_error_text_contains!(
220-
AggregatorClientError::get_root_cause(response).await,
220+
AggregatorHttpClientError::get_root_cause(response).await,
221221
"bad request: message"
222222
);
223223
}
@@ -230,7 +230,7 @@ mod tests {
230230
);
231231

232232
assert_error_text_contains!(
233-
AggregatorClientError::get_root_cause(response).await,
233+
AggregatorHttpClientError::get_root_cause(response).await,
234234
r#"internal server error: {"first":"foreign","second":"unknown"}"#
235235
);
236236
}
@@ -244,7 +244,7 @@ mod tests {
244244
.unwrap()
245245
.into();
246246

247-
let root_cause = AggregatorClientError::get_root_cause(response).await;
247+
let root_cause = AggregatorHttpClientError::get_root_cause(response).await;
248248

249249
assert_error_text_contains!(root_cause, "bad request");
250250
assert!(

internal/mithril-aggregator-client/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ pub mod query;
1010
mod test;
1111

1212
pub use builder::AggregatorClientBuilder;
13-
pub use client::AggregatorClient;
14-
pub use error::AggregatorClientError;
13+
pub use client::AggregatorHttpClient;
14+
pub use error::AggregatorHttpClientError;
1515

1616
pub(crate) const JSON_CONTENT_TYPE: reqwest::header::HeaderValue =
1717
reqwest::header::HeaderValue::from_static("application/json");
1818

1919
/// Aggregator-client result type
20-
pub type AggregatorClientResult<T> = Result<T, error::AggregatorClientError>;
20+
pub type AggregatorHttpClientResult<T> = Result<T, AggregatorHttpClientError>;

internal/mithril-aggregator-client/src/query/api.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use reqwest::Response;
22
use serde::de::DeserializeOwned;
33
use slog::Logger;
44

5-
use crate::AggregatorClientResult;
6-
use crate::error::AggregatorClientError;
5+
use crate::AggregatorHttpClientResult;
6+
use crate::error::AggregatorHttpClientError;
77

88
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99
pub enum QueryMethod {
@@ -28,7 +28,7 @@ pub trait AggregatorQuery {
2828
async fn handle_response(
2929
&self,
3030
context: QueryContext,
31-
) -> AggregatorClientResult<Self::Response>;
31+
) -> AggregatorHttpClientResult<Self::Response>;
3232
}
3333

3434
pub struct QueryContext {
@@ -37,7 +37,7 @@ pub struct QueryContext {
3737
}
3838

3939
impl QueryContext {
40-
pub async fn unhandled_status_code(self) -> AggregatorClientError {
41-
AggregatorClientError::from_response(self.response).await
40+
pub async fn unhandled_status_code(self) -> AggregatorHttpClientError {
41+
AggregatorHttpClientError::from_response(self.response).await
4242
}
4343
}

internal/mithril-aggregator-client/src/query/get/get_certificate.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use slog::debug;
55

66
use mithril_common::messages::CertificateMessage;
77

8-
use crate::AggregatorClientResult;
9-
use crate::error::AggregatorClientError;
8+
use crate::AggregatorHttpClientResult;
9+
use crate::error::AggregatorHttpClientError;
1010
use crate::query::{AggregatorQuery, QueryContext, QueryMethod};
1111

1212
/// Query to get the details of a certificate
@@ -45,13 +45,13 @@ impl AggregatorQuery for GetCertificateQuery {
4545
async fn handle_response(
4646
&self,
4747
context: QueryContext,
48-
) -> AggregatorClientResult<Self::Response> {
48+
) -> AggregatorHttpClientResult<Self::Response> {
4949
debug!(context.logger, "Retrieve certificate details"; "certificate_hash" => %self.hash);
5050

5151
match context.response.status() {
5252
StatusCode::OK => match context.response.json::<CertificateMessage>().await {
5353
Ok(message) => Ok(Some(message)),
54-
Err(err) => Err(AggregatorClientError::JsonParseFailed(anyhow!(err))),
54+
Err(err) => Err(AggregatorHttpClientError::JsonParseFailed(anyhow!(err))),
5555
},
5656
StatusCode::NOT_FOUND => Ok(None),
5757
_ => Err(context.unhandled_status_code().await),
@@ -112,7 +112,7 @@ mod tests {
112112
.await
113113
.unwrap_err();
114114

115-
assert_error_matches!(error, AggregatorClientError::RemoteServerTechnical(_));
115+
assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_));
116116
}
117117

118118
#[tokio::test]
@@ -152,6 +152,6 @@ mod tests {
152152

153153
let error = client.send(GetCertificateQuery::latest_genesis()).await.unwrap_err();
154154

155-
assert_error_matches!(error, AggregatorClientError::RemoteServerTechnical(_));
155+
assert_error_matches!(error, AggregatorHttpClientError::RemoteServerTechnical(_));
156156
}
157157
}

0 commit comments

Comments
 (0)