2
2
#![ cfg( feature = "reqwest" ) ]
3
3
4
4
use crate :: { agent:: agent_error:: HttpErrorPayload , ic_types:: Principal , AgentError , RequestId } ;
5
- use reqwest:: Method ;
6
- use std:: { future:: Future , pin:: Pin , sync:: Arc } ;
5
+ use reqwest:: { ClientBuilder , Method } ;
6
+ use std:: { future:: Future , pin:: Pin , sync:: Arc , time :: Duration } ;
7
7
8
8
/// Implemented by the Agent environment to cache and update an HTTP Auth password.
9
9
/// It returns a tuple of `(username, password)`.
@@ -30,6 +30,25 @@ pub struct ReqwestHttpReplicaV2Transport {
30
30
31
31
impl ReqwestHttpReplicaV2Transport {
32
32
pub fn create < U : Into < String > > ( url : U ) -> Result < Self , AgentError > {
33
+ Self :: create_with_modified_http_options ( url, |cb| cb)
34
+ }
35
+
36
+ pub fn create_with_timeout < U : Into < String > > (
37
+ url : U ,
38
+ http_timeout : Duration ,
39
+ ) -> Result < Self , AgentError > {
40
+ Self :: create_with_modified_http_options ( url, |cb| cb. timeout ( http_timeout) )
41
+ }
42
+
43
+ /// This method allows you to create a `ReqwestHttpReplicaV2Transport`
44
+ /// with arbitrary http options by using `Reqwest::ClientBuilder`.
45
+ pub fn create_with_modified_http_options <
46
+ U : Into < String > ,
47
+ F : FnOnce ( ClientBuilder ) -> ClientBuilder ,
48
+ > (
49
+ url : U ,
50
+ modify_http_options : F ,
51
+ ) -> Result < Self , AgentError > {
33
52
let mut tls_config = rustls:: ClientConfig :: new ( ) ;
34
53
35
54
// Advertise support for HTTP/2
@@ -45,10 +64,11 @@ impl ReqwestHttpReplicaV2Transport {
45
64
url : reqwest:: Url :: parse ( & url)
46
65
. and_then ( |url| url. join ( "api/v2/" ) )
47
66
. map_err ( |_| AgentError :: InvalidReplicaUrl ( url. clone ( ) ) ) ?,
48
- client : reqwest:: Client :: builder ( )
49
- . use_preconfigured_tls ( tls_config)
50
- . build ( )
51
- . expect ( "Could not create HTTP client." ) ,
67
+ client : modify_http_options (
68
+ reqwest:: Client :: builder ( ) . use_preconfigured_tls ( tls_config) ,
69
+ )
70
+ . build ( )
71
+ . expect ( "Could not create HTTP client." ) ,
52
72
password_manager : None ,
53
73
} )
54
74
}
0 commit comments