11//! http-client implementation for fetch
22
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 ;
65
76use std:: convert:: TryFrom ;
87use std:: pin:: Pin ;
98use std:: task:: { Context , Poll } ;
109
10+ use futures:: prelude:: * ;
11+
12+ use crate :: Config ;
13+
14+ use super :: { http_types:: Headers , Body , Error , HttpClient , Request , Response } ;
15+
1116/// WebAssembly HTTP Client.
1217#[ derive( Debug ) ]
1318pub struct WasmClient {
14- _priv : ( ) ,
19+ config : Config ,
1520}
1621
1722impl WasmClient {
1823 /// Create a new instance.
1924 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 ( )
2132 }
2233}
2334
@@ -30,9 +41,19 @@ impl HttpClient for WasmClient {
3041 ' a : ' async_trait ,
3142 Self : ' async_trait ,
3243 {
44+ let config = self . config . clone ( ) ;
45+
3346 InnerFuture :: new ( async move {
3447 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 ?;
3657
3758 let body = res. body_bytes ( ) ;
3859 let mut response =
@@ -46,6 +67,36 @@ impl HttpClient for WasmClient {
4667 Ok ( response)
4768 } )
4869 }
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+ }
49100}
50101
51102struct InnerFuture {
0 commit comments