|
| 1 | +use anyhow::Context; |
| 2 | +use reqwest::{Client, IntoUrl, Proxy, Url}; |
| 3 | +use slog::{Logger, o}; |
| 4 | +use std::collections::HashMap; |
| 5 | +use std::time::Duration; |
| 6 | + |
| 7 | +use mithril_common::StdResult; |
| 8 | +use mithril_common::api_version::APIVersionProvider; |
| 9 | + |
| 10 | +use crate::client::AggregatorClient; |
| 11 | + |
| 12 | +/// A builder of [AggregatorClient] |
| 13 | +pub struct AggregatorClientBuilder { |
| 14 | + aggregator_url_result: reqwest::Result<Url>, |
| 15 | + api_version_provider: Option<APIVersionProvider>, |
| 16 | + additional_headers: Option<HashMap<String, String>>, |
| 17 | + timeout_duration: Option<Duration>, |
| 18 | + relay_endpoint: Option<String>, |
| 19 | + logger: Option<Logger>, |
| 20 | +} |
| 21 | + |
| 22 | +impl AggregatorClientBuilder { |
| 23 | + /// Constructs a new `AggregatorClientBuilder`. |
| 24 | + // |
| 25 | + // This is the same as `AggregatorClient::builder()`. |
| 26 | + pub fn new<U: IntoUrl>(aggregator_url: U) -> Self { |
| 27 | + Self { |
| 28 | + aggregator_url_result: aggregator_url.into_url(), |
| 29 | + api_version_provider: None, |
| 30 | + additional_headers: None, |
| 31 | + timeout_duration: None, |
| 32 | + relay_endpoint: None, |
| 33 | + logger: None, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Set the [Logger] to use. |
| 38 | + pub fn with_logger(mut self, logger: Logger) -> Self { |
| 39 | + self.logger = Some(logger); |
| 40 | + self |
| 41 | + } |
| 42 | + |
| 43 | + /// Set the [APIVersionProvider] to use. |
| 44 | + pub fn with_api_version_provider(mut self, api_version_provider: APIVersionProvider) -> Self { |
| 45 | + self.api_version_provider = Some(api_version_provider); |
| 46 | + self |
| 47 | + } |
| 48 | + |
| 49 | + /// Set a timeout to enforce on each request |
| 50 | + pub fn with_timeout(mut self, timeout: Duration) -> Self { |
| 51 | + self.timeout_duration = Some(timeout); |
| 52 | + self |
| 53 | + } |
| 54 | + |
| 55 | + /// Add a set of http headers that will be sent on client requests |
| 56 | + pub fn with_headers(mut self, custom_headers: HashMap<String, String>) -> Self { |
| 57 | + self.additional_headers = Some(custom_headers); |
| 58 | + self |
| 59 | + } |
| 60 | + |
| 61 | + /// Set the address of the relay |
| 62 | + pub fn with_relay_endpoint(mut self, relay_endpoint: String) -> Self { |
| 63 | + self.relay_endpoint = Some(relay_endpoint); |
| 64 | + self |
| 65 | + } |
| 66 | + |
| 67 | + /// Returns an [AggregatorClient] based on the builder configuration |
| 68 | + pub fn build(self) -> StdResult<AggregatorClient> { |
| 69 | + let aggregator_endpoint = |
| 70 | + enforce_trailing_slash(self.aggregator_url_result.with_context( |
| 71 | + || "Invalid aggregator endpoint, it must be a correctly formed url", |
| 72 | + )?); |
| 73 | + let logger = self.logger.unwrap_or_else(|| Logger::root(slog::Discard, o!())); |
| 74 | + let api_version_provider = self.api_version_provider.unwrap_or_default(); |
| 75 | + let additional_headers = self.additional_headers.unwrap_or_default(); |
| 76 | + let mut client_builder = Client::builder(); |
| 77 | + |
| 78 | + if let Some(relay_endpoint) = self.relay_endpoint { |
| 79 | + client_builder = client_builder |
| 80 | + .proxy(Proxy::all(relay_endpoint).with_context(|| "Relay proxy creation failed")?) |
| 81 | + } |
| 82 | + |
| 83 | + Ok(AggregatorClient { |
| 84 | + aggregator_endpoint, |
| 85 | + api_version_provider, |
| 86 | + additional_headers: (&additional_headers) |
| 87 | + .try_into() |
| 88 | + .with_context(|| format!("Invalid headers: '{additional_headers:?}'"))?, |
| 89 | + timeout_duration: self.timeout_duration, |
| 90 | + client: client_builder |
| 91 | + .build() |
| 92 | + .with_context(|| "HTTP client creation failed")?, |
| 93 | + logger, |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn enforce_trailing_slash(url: Url) -> Url { |
| 99 | + // Trailing slash is significant because url::join |
| 100 | + // (https://docs.rs/url/latest/url/struct.Url.html#method.join) will remove |
| 101 | + // the 'path' part of the url if it doesn't end with a trailing slash. |
| 102 | + if url.as_str().ends_with('/') { |
| 103 | + url |
| 104 | + } else { |
| 105 | + let mut url = url.clone(); |
| 106 | + url.set_path(&format!("{}/", url.path())); |
| 107 | + url |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod tests { |
| 113 | + use super::*; |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn enforce_trailing_slash_for_aggregator_url() { |
| 117 | + let url_without_trailing_slash = Url::parse("http://localhost:8080").unwrap(); |
| 118 | + let url_with_trailing_slash = Url::parse("http://localhost:8080/").unwrap(); |
| 119 | + |
| 120 | + assert_eq!( |
| 121 | + url_with_trailing_slash, |
| 122 | + enforce_trailing_slash(url_without_trailing_slash.clone()) |
| 123 | + ); |
| 124 | + assert_eq!( |
| 125 | + url_with_trailing_slash, |
| 126 | + enforce_trailing_slash(url_with_trailing_slash.clone()) |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments