@@ -6,46 +6,63 @@ use http_types::headers::{HeaderName, HeaderValue};
6
6
use http_types:: StatusCode ;
7
7
use hyper:: body:: HttpBody ;
8
8
use hyper:: client:: connect:: Connect ;
9
- use hyper:: client:: HttpConnector ;
10
9
use hyper_tls:: HttpsConnector ;
11
10
use std:: convert:: TryFrom ;
12
11
use std:: fmt:: Debug ;
13
12
use std:: io;
14
13
use std:: str:: FromStr ;
15
14
use std:: sync:: Arc ;
16
15
16
+ // Avoid leaking Hyper generics into HttpClient by hiding it behind a dynamic trait object pointer.
17
+ trait HyperClientObject : Debug + Send + Sync + ' static {
18
+ fn dyn_request ( & self , req : hyper:: Request < hyper:: Body > ) -> hyper:: client:: ResponseFuture ;
19
+ }
20
+
21
+ impl < C : Clone + Connect + Debug + Send + Sync + ' static > HyperClientObject for hyper:: Client < C > {
22
+ fn dyn_request ( & self , req : hyper:: Request < hyper:: Body > ) -> hyper:: client:: ResponseFuture {
23
+ self . request ( req)
24
+ }
25
+ }
26
+
17
27
/// Hyper-based HTTP Client.
18
- #[ derive( Clone , Debug ) ]
19
- pub struct HyperClient < C : Clone + Connect + Debug + Send + Sync + ' static > ( Arc < hyper :: Client < C > > ) ;
28
+ #[ derive( Debug ) ]
29
+ pub struct HyperClient ( Arc < dyn HyperClientObject > ) ;
20
30
21
- impl HyperClient < HttpsConnector < HttpConnector > > {
31
+ impl HyperClient {
22
32
/// Create a new client instance.
23
33
pub fn new ( ) -> Self {
24
34
let https = HttpsConnector :: new ( ) ;
25
35
let client = hyper:: Client :: builder ( ) . build ( https) ;
26
36
Self ( Arc :: new ( client) )
27
37
}
28
- }
29
38
30
- impl < C : Clone + Connect + Debug + Send + Sync + ' static > HyperClient < C > {
31
39
/// Create from externally initialized and configured client.
32
- pub fn from_client ( client : hyper:: Client < C > ) -> Self {
40
+ pub fn from_client < C > ( client : hyper:: Client < C > ) -> Self
41
+ where
42
+ C : Clone + Connect + Debug + Send + Sync + ' static ,
43
+ {
33
44
Self ( Arc :: new ( client) )
34
45
}
35
46
}
36
47
37
- impl Default for HyperClient < HttpsConnector < HttpConnector > > {
48
+ impl Clone for HyperClient {
49
+ fn clone ( & self ) -> Self {
50
+ Self ( self . 0 . clone ( ) )
51
+ }
52
+ }
53
+
54
+ impl Default for HyperClient {
38
55
fn default ( ) -> Self {
39
56
Self :: new ( )
40
57
}
41
58
}
42
59
43
60
#[ async_trait]
44
- impl < C : Clone + Connect + Debug + Send + Sync + ' static > HttpClient for HyperClient < C > {
61
+ impl HttpClient for HyperClient {
45
62
async fn send ( & self , req : Request ) -> Result < Response , Error > {
46
63
let req = HyperHttpRequest :: try_from ( req) . await ?. into_inner ( ) ;
47
64
48
- let response = self . 0 . request ( req) . await ?;
65
+ let response = self . 0 . dyn_request ( req) . await ?;
49
66
50
67
let res = HttpTypesResponse :: try_from ( response) . await ?. into_inner ( ) ;
51
68
Ok ( res)
0 commit comments