1
1
use anyhow:: Context ;
2
- use reqwest:: { IntoUrl , Url } ;
2
+ use reqwest:: { Client , IntoUrl , Proxy , Url } ;
3
3
use slog:: { Logger , o} ;
4
4
use std:: collections:: HashMap ;
5
5
use std:: time:: Duration ;
@@ -15,6 +15,7 @@ pub struct AggregatorClientBuilder {
15
15
api_version_provider : Option < APIVersionProvider > ,
16
16
additional_headers : Option < HashMap < String , String > > ,
17
17
timeout_duration : Option < Duration > ,
18
+ relay_endpoint : Option < String > ,
18
19
logger : Option < Logger > ,
19
20
}
20
21
@@ -28,6 +29,7 @@ impl AggregatorClientBuilder {
28
29
api_version_provider : None ,
29
30
additional_headers : None ,
30
31
timeout_duration : None ,
32
+ relay_endpoint : None ,
31
33
logger : None ,
32
34
}
33
35
}
@@ -56,6 +58,12 @@ impl AggregatorClientBuilder {
56
58
self
57
59
}
58
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
+
59
67
/// Returns an [AggregatorClient] based on the builder configuration
60
68
pub fn build ( self ) -> StdResult < AggregatorClient > {
61
69
let aggregator_endpoint =
@@ -65,6 +73,12 @@ impl AggregatorClientBuilder {
65
73
let logger = self . logger . unwrap_or_else ( || Logger :: root ( slog:: Discard , o ! ( ) ) ) ;
66
74
let api_version_provider = self . api_version_provider . unwrap_or_default ( ) ;
67
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
+ }
68
82
69
83
Ok ( AggregatorClient {
70
84
aggregator_endpoint,
@@ -73,7 +87,9 @@ impl AggregatorClientBuilder {
73
87
. try_into ( )
74
88
. with_context ( || format ! ( "Invalid headers: '{additional_headers:?}'" ) ) ?,
75
89
timeout_duration : self . timeout_duration ,
76
- client : reqwest:: Client :: new ( ) ,
90
+ client : client_builder
91
+ . build ( )
92
+ . with_context ( || "HTTP client creation failed" ) ?,
77
93
logger,
78
94
} )
79
95
}
0 commit comments