-
Notifications
You must be signed in to change notification settings - Fork 52
add shared aggregator client #2661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,293
−14
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0554dbb
chore: scaffold `internal/mithril-aggregator-client` crate
Alenar a402fdb
feat: prototype aggregator client query system
Alenar bfc1b82
refactor(aggregator-client): move query related types to query module
Alenar 6607cb0
test(aggregator-client): add aggregatorClientError tests
Alenar 7ad948b
feat(aggregator-client): add `AggregatorClientBuilder`
Alenar c891308
test(aggregator-client): add a minimal post test
Alenar ae9e2d9
feat(common): impl `Default` to `APIVersionProvider`
Alenar d567e99
test(common): add `APIVersionProviderTestExtension`
Alenar f494875
feat(aggregator-client): add warning when client and aggregator api v…
Alenar f2f621a
feat(aggregator-client): send mithril api version header on all requests
Alenar 50c9473
feat(aggregator-client): add timeout support
Alenar c8b90d2
doc(aggregator-client): add missing doc
Alenar 8c5a9ee
feat(aggregator-client): add ability to set additional headers
Alenar cfa402c
feat(aggregator-client): add `ClientBuilder::with_relay_endpoint`
Alenar e9ba5ce
feat(aggregator-client): make the crate wasm compatible
Alenar 20479ce
test(aggregator-client): simplify client tests
Alenar 7131667
test(aggregator-client): add test to `CertificateDetailsQuery` and ge…
Alenar 70c6c51
chore: upgrade crate versions
Alenar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| [package] | ||
| name = "mithril-aggregator-client" | ||
| version = "0.1.0" | ||
| description = "Client to request data from a Mithril Aggregator" | ||
| authors.workspace = true | ||
| documentation.workspace = true | ||
| edition.workspace = true | ||
| homepage.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
| include = ["**/*.rs", "Cargo.toml", "README.md"] | ||
|
|
||
| [lib] | ||
| crate-type = ["lib", "cdylib", "staticlib"] | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| async-trait = { workspace = true } | ||
| mithril-common = { path = "../../mithril-common", version = ">=0.5" } | ||
| reqwest = { workspace = true } | ||
| semver = { workspace = true } | ||
| serde = { workspace = true } | ||
| serde_json = { workspace = true } | ||
| slog = { workspace = true } | ||
| thiserror = { workspace = true } | ||
| tokio = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| http = "1.3.1" | ||
| httpmock = "0.7.0" | ||
| mithril-common = { path = "../../mithril-common", version = ">=0.5", features = ["test_tools"] } | ||
| mockall = { workspace = true } | ||
| slog-async = { workspace = true } | ||
| slog-term = { workspace = true } | ||
| tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| .PHONY: all build test check doc | ||
|
|
||
| CARGO = cargo | ||
|
|
||
| all: test build | ||
|
|
||
| build: | ||
| ${CARGO} build --release | ||
|
|
||
| test: | ||
| ${CARGO} test | ||
|
|
||
| check: | ||
| ${CARGO} check --release --all-features --all-targets | ||
| ${CARGO} clippy --release --all-features --all-targets | ||
| ${CARGO} fmt --check | ||
|
|
||
| doc: | ||
| ${CARGO} doc --no-deps --open |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Mithril-aggregator-client [](https://github.com/input-output-hk/mithril/actions/workflows/ci.yml) [](https://github.com/input-output-hk/mithril/blob/main/LICENSE) [](https://discord.gg/5kaErDKDRq) | ||
|
|
||
| This crate provides a client to request data from a Mithril Aggregator. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| use anyhow::Context; | ||
| use reqwest::{Client, IntoUrl, Proxy, Url}; | ||
| use slog::{Logger, o}; | ||
| use std::collections::HashMap; | ||
| use std::time::Duration; | ||
|
|
||
| use mithril_common::StdResult; | ||
| use mithril_common::api_version::APIVersionProvider; | ||
|
|
||
| use crate::client::AggregatorClient; | ||
|
|
||
| /// A builder of [AggregatorClient] | ||
| pub struct AggregatorClientBuilder { | ||
| aggregator_url_result: reqwest::Result<Url>, | ||
| api_version_provider: Option<APIVersionProvider>, | ||
| additional_headers: Option<HashMap<String, String>>, | ||
| timeout_duration: Option<Duration>, | ||
| relay_endpoint: Option<String>, | ||
| logger: Option<Logger>, | ||
| } | ||
|
|
||
| impl AggregatorClientBuilder { | ||
| /// Constructs a new `AggregatorClientBuilder`. | ||
| // | ||
| // This is the same as `AggregatorClient::builder()`. | ||
| pub fn new<U: IntoUrl>(aggregator_url: U) -> Self { | ||
| Self { | ||
| aggregator_url_result: aggregator_url.into_url(), | ||
| api_version_provider: None, | ||
| additional_headers: None, | ||
| timeout_duration: None, | ||
| relay_endpoint: None, | ||
| logger: None, | ||
| } | ||
| } | ||
|
|
||
| /// Set the [Logger] to use. | ||
| pub fn with_logger(mut self, logger: Logger) -> Self { | ||
| self.logger = Some(logger); | ||
| self | ||
| } | ||
|
|
||
| /// Set the [APIVersionProvider] to use. | ||
| pub fn with_api_version_provider(mut self, api_version_provider: APIVersionProvider) -> Self { | ||
| self.api_version_provider = Some(api_version_provider); | ||
| self | ||
| } | ||
|
|
||
| /// Set a timeout to enforce on each request | ||
| pub fn with_timeout(mut self, timeout: Duration) -> Self { | ||
| self.timeout_duration = Some(timeout); | ||
| self | ||
| } | ||
|
|
||
| /// Add a set of http headers that will be sent on client requests | ||
| pub fn with_headers(mut self, custom_headers: HashMap<String, String>) -> Self { | ||
| self.additional_headers = Some(custom_headers); | ||
| self | ||
| } | ||
|
|
||
| /// Set the address of the relay | ||
| pub fn with_relay_endpoint(mut self, relay_endpoint: String) -> Self { | ||
| self.relay_endpoint = Some(relay_endpoint); | ||
| self | ||
| } | ||
|
|
||
| /// Returns an [AggregatorClient] based on the builder configuration | ||
| pub fn build(self) -> StdResult<AggregatorClient> { | ||
| let aggregator_endpoint = | ||
| enforce_trailing_slash(self.aggregator_url_result.with_context( | ||
| || "Invalid aggregator endpoint, it must be a correctly formed url", | ||
| )?); | ||
| let logger = self.logger.unwrap_or_else(|| Logger::root(slog::Discard, o!())); | ||
| let api_version_provider = self.api_version_provider.unwrap_or_default(); | ||
| let additional_headers = self.additional_headers.unwrap_or_default(); | ||
| let mut client_builder = Client::builder(); | ||
|
|
||
| if let Some(relay_endpoint) = self.relay_endpoint { | ||
| client_builder = client_builder | ||
| .proxy(Proxy::all(relay_endpoint).with_context(|| "Relay proxy creation failed")?) | ||
| } | ||
|
|
||
| Ok(AggregatorClient { | ||
| aggregator_endpoint, | ||
| api_version_provider, | ||
| additional_headers: (&additional_headers) | ||
| .try_into() | ||
| .with_context(|| format!("Invalid headers: '{additional_headers:?}'"))?, | ||
| timeout_duration: self.timeout_duration, | ||
| client: client_builder | ||
| .build() | ||
| .with_context(|| "HTTP client creation failed")?, | ||
| logger, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| fn enforce_trailing_slash(url: Url) -> Url { | ||
| // Trailing slash is significant because url::join | ||
| // (https://docs.rs/url/latest/url/struct.Url.html#method.join) will remove | ||
| // the 'path' part of the url if it doesn't end with a trailing slash. | ||
| if url.as_str().ends_with('/') { | ||
| url | ||
| } else { | ||
| let mut url = url.clone(); | ||
| url.set_path(&format!("{}/", url.path())); | ||
| url | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn enforce_trailing_slash_for_aggregator_url() { | ||
Alenar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let url_without_trailing_slash = Url::parse("http://localhost:8080").unwrap(); | ||
| let url_with_trailing_slash = Url::parse("http://localhost:8080/").unwrap(); | ||
|
|
||
| assert_eq!( | ||
| url_with_trailing_slash, | ||
| enforce_trailing_slash(url_without_trailing_slash.clone()) | ||
| ); | ||
| assert_eq!( | ||
| url_with_trailing_slash, | ||
| enforce_trailing_slash(url_with_trailing_slash.clone()) | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.