1
1
//! http-client implementation for fetch
2
2
3
- use super :: { http_types:: Headers , Body , Error , HttpClient , Request , Response } ;
4
-
5
- use futures:: prelude:: * ;
3
+ #[ cfg( feature = "unstable-config" ) ]
4
+ use std:: convert:: Infallible ;
6
5
7
6
use std:: convert:: TryFrom ;
8
7
use std:: pin:: Pin ;
9
8
use std:: task:: { Context , Poll } ;
10
9
10
+ use futures:: prelude:: * ;
11
+
12
+ use crate :: Config ;
13
+
14
+ use super :: { http_types:: Headers , Body , Error , HttpClient , Request , Response } ;
15
+
11
16
/// WebAssembly HTTP Client.
12
17
#[ derive( Debug ) ]
13
18
pub struct WasmClient {
14
- _priv : ( ) ,
19
+ config : Config ,
15
20
}
16
21
17
22
impl WasmClient {
18
23
/// Create a new instance.
19
24
pub fn new ( ) -> Self {
20
- Self { _priv : ( ) }
25
+ Self { config : Config :: default ( ) }
26
+ }
27
+ }
28
+
29
+ impl Default for WasmClient {
30
+ fn default ( ) -> Self {
31
+ Self :: new ( )
21
32
}
22
33
}
23
34
@@ -30,9 +41,19 @@ impl HttpClient for WasmClient {
30
41
' a : ' async_trait ,
31
42
Self : ' async_trait ,
32
43
{
44
+ let config = self . config . clone ( ) ;
45
+
33
46
InnerFuture :: new ( async move {
34
47
let req: fetch:: Request = fetch:: Request :: new ( req) . await ?;
35
- let mut res = req. send ( ) . await ?;
48
+ let conn = req. send ( ) ;
49
+ #[ cfg( feature = "unstable-config" ) ]
50
+ let mut res = if let Some ( timeout) = config. timeout {
51
+ async_std:: future:: timeout ( timeout, conn) . await ??
52
+ } else {
53
+ conn. await ?
54
+ } ;
55
+ #[ cfg( not( feature = "unstable-config" ) ) ]
56
+ let mut res = conn. await ?;
36
57
37
58
let body = res. body_bytes ( ) ;
38
59
let mut response =
@@ -46,6 +67,36 @@ impl HttpClient for WasmClient {
46
67
Ok ( response)
47
68
} )
48
69
}
70
+
71
+ #[ cfg_attr( feature = "docs" , doc( cfg( feature = "unstable-config" ) ) ) ]
72
+ #[ cfg( feature = "unstable-config" ) ]
73
+ /// Override the existing configuration with new configuration.
74
+ ///
75
+ /// Config options may not impact existing connections.
76
+ fn set_config ( & mut self , config : Config ) -> http_types:: Result < ( ) > {
77
+ self . config = config;
78
+
79
+ Ok ( ( ) )
80
+ }
81
+
82
+ #[ cfg_attr( feature = "docs" , doc( cfg( feature = "unstable-config" ) ) ) ]
83
+ #[ cfg( feature = "unstable-config" ) ]
84
+ /// Get the current configuration.
85
+ fn config ( & self ) -> & Config {
86
+ & self . config
87
+ }
88
+ }
89
+
90
+ #[ cfg_attr( feature = "docs" , doc( cfg( feature = "unstable-config" ) ) ) ]
91
+ #[ cfg( feature = "unstable-config" ) ]
92
+ impl TryFrom < Config > for WasmClient {
93
+ type Error = Infallible ;
94
+
95
+ fn try_from ( config : Config ) -> Result < Self , Self :: Error > {
96
+ Ok ( Self {
97
+ config,
98
+ } )
99
+ }
49
100
}
50
101
51
102
struct InnerFuture {
0 commit comments