11use anyhow:: Context ;
2- use reqwest:: { IntoUrl , Url } ;
2+ use reqwest:: { Client , IntoUrl , Proxy , Url } ;
33use slog:: { Logger , o} ;
44use std:: collections:: HashMap ;
55use std:: time:: Duration ;
@@ -15,6 +15,7 @@ pub struct AggregatorClientBuilder {
1515 api_version_provider : Option < APIVersionProvider > ,
1616 additional_headers : Option < HashMap < String , String > > ,
1717 timeout_duration : Option < Duration > ,
18+ relay_endpoint : Option < String > ,
1819 logger : Option < Logger > ,
1920}
2021
@@ -28,6 +29,7 @@ impl AggregatorClientBuilder {
2829 api_version_provider : None ,
2930 additional_headers : None ,
3031 timeout_duration : None ,
32+ relay_endpoint : None ,
3133 logger : None ,
3234 }
3335 }
@@ -56,6 +58,12 @@ impl AggregatorClientBuilder {
5658 self
5759 }
5860
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+
5967 /// Returns an [AggregatorClient] based on the builder configuration
6068 pub fn build ( self ) -> StdResult < AggregatorClient > {
6169 let aggregator_endpoint =
@@ -65,6 +73,12 @@ impl AggregatorClientBuilder {
6573 let logger = self . logger . unwrap_or_else ( || Logger :: root ( slog:: Discard , o ! ( ) ) ) ;
6674 let api_version_provider = self . api_version_provider . unwrap_or_default ( ) ;
6775 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+ }
6882
6983 Ok ( AggregatorClient {
7084 aggregator_endpoint,
@@ -73,7 +87,9 @@ impl AggregatorClientBuilder {
7387 . try_into ( )
7488 . with_context ( || format ! ( "Invalid headers: '{additional_headers:?}'" ) ) ?,
7589 timeout_duration : self . timeout_duration ,
76- client : reqwest:: Client :: new ( ) ,
90+ client : client_builder
91+ . build ( )
92+ . with_context ( || "HTTP client creation failed" ) ?,
7793 logger,
7894 } )
7995 }
0 commit comments