diff --git a/ic-agent/src/agent/agent_config.rs b/ic-agent/src/agent/agent_config.rs index 50464ebd..0c9ebee3 100644 --- a/ic-agent/src/agent/agent_config.rs +++ b/ic-agent/src/agent/agent_config.rs @@ -1,3 +1,5 @@ +use reqwest::Client; + use crate::{ agent::{NonceFactory, NonceGenerator}, identity::{anonymous::AnonymousIdentity, Identity}, @@ -16,7 +18,7 @@ pub struct AgentConfig { /// See [`with_ingress_expiry`](super::AgentBuilder::with_ingress_expiry). pub ingress_expiry: Option, /// See [`with_http_client`](super::AgentBuilder::with_http_client). - pub client: Option>, + pub client: Option, /// See [`with_route_provider`](super::AgentBuilder::with_route_provider). pub route_provider: Option>, /// See [`verify_query_signatures`](super::AgentBuilder::with_verify_query_signatures). @@ -27,6 +29,8 @@ pub struct AgentConfig { pub max_response_body_size: Option, /// See [`with_max_tcp_error_retries`](super::AgentBuilder::with_max_tcp_error_retries). pub max_tcp_error_retries: usize, + /// See [`with_arc_http_middleware`](super::AgentBuilder::with_arc_http_middleware). + pub http_service: Option>, } impl Default for AgentConfig { @@ -36,6 +40,7 @@ impl Default for AgentConfig { identity: Arc::new(AnonymousIdentity {}), ingress_expiry: None, client: None, + http_service: None, verify_query_signatures: true, max_concurrent_requests: 50, route_provider: None, diff --git a/ic-agent/src/agent/builder.rs b/ic-agent/src/agent/builder.rs index 063a3fba..638befd2 100644 --- a/ic-agent/src/agent/builder.rs +++ b/ic-agent/src/agent/builder.rs @@ -106,17 +106,25 @@ impl AgentBuilder { /// Provide a pre-configured HTTP client to use. Use this to set e.g. HTTP timeouts or proxy configuration. pub fn with_http_client(mut self, client: reqwest::Client) -> Self { - self.config.client = Some(Arc::new(client)); + assert!( + self.config.http_service.is_none(), + "with_arc_http_middleware cannot be called with with_http_client" + ); + self.config.client = Some(client); self } /// Provide a custom `reqwest`-compatible HTTP service, e.g. to add per-request headers for custom boundary nodes. - /// Most users will not need this and should use `with_http_client`. + /// Most users will not need this and should use `with_http_client`. Cannot be called with `with_http_client`. /// /// The trait is automatically implemented for any `tower::Service` impl matching the one `reqwest::Client` uses, - /// including `reqwest-middleware`. Implementations must provide all automatic retry logic. + /// including `reqwest-middleware`. This is a low-level interface, and implementations must provide all automatic retry logic. pub fn with_arc_http_middleware(mut self, service: Arc) -> Self { - self.config.client = Some(service); + assert!( + self.config.client.is_none(), + "with_arc_http_middleware cannot be called with with_http_client" + ); + self.config.http_service = Some(service); self } diff --git a/ic-agent/src/agent/mod.rs b/ic-agent/src/agent/mod.rs index 780c10e1..bb65aee9 100644 --- a/ic-agent/src/agent/mod.rs +++ b/ic-agent/src/agent/mod.rs @@ -181,10 +181,10 @@ impl Agent { identity: config.identity, ingress_expiry: config.ingress_expiry.unwrap_or(DEFAULT_INGRESS_EXPIRY), root_key: Arc::new(RwLock::new(IC_ROOT_KEY.to_vec())), - client: config.client.unwrap_or_else(|| { + client: config.http_service.unwrap_or_else(|| { Arc::new(DefaultRetryLogic { _max_tcp_error_retries: config.max_tcp_error_retries, - client: { + client: config.client.unwrap_or_else(|| { #[cfg(not(target_family = "wasm"))] { Client::builder() @@ -197,7 +197,7 @@ impl Agent { { Client::new() } - }, + }), }) }), route_provider: config