@@ -6,19 +6,29 @@ 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 ( ) ;
@@ -27,25 +37,33 @@ impl HyperClient<HttpsConnector<HttpConnector>> {
27
37
}
28
38
}
29
39
30
- impl < C : Clone + Connect + Debug + Send + Sync + ' static > HyperClient < C > {
40
+ impl Clone for HyperClient {
41
+ fn clone ( & self ) -> Self {
42
+ Self ( self . 0 . clone ( ) )
43
+ }
44
+ }
45
+
46
+ impl HyperClient {
31
47
/// Create from externally initialized and configured client.
32
- pub fn from_client ( client : hyper:: Client < C > ) -> Self {
48
+ pub fn from_client < C : Clone + Connect + Debug + Send + Sync + ' static > (
49
+ client : hyper:: Client < C > ,
50
+ ) -> Self {
33
51
Self ( Arc :: new ( client) )
34
52
}
35
53
}
36
54
37
- impl Default for HyperClient < HttpsConnector < HttpConnector > > {
55
+ impl Default for HyperClient {
38
56
fn default ( ) -> Self {
39
57
Self :: new ( )
40
58
}
41
59
}
42
60
43
61
#[ async_trait]
44
- impl < C : Clone + Connect + Debug + Send + Sync + ' static > HttpClient for HyperClient < C > {
62
+ impl HttpClient for HyperClient {
45
63
async fn send ( & self , req : Request ) -> Result < Response , Error > {
46
64
let req = HyperHttpRequest :: try_from ( req) . await ?. into_inner ( ) ;
47
65
48
- let response = self . 0 . request ( req) . await ?;
66
+ let response = self . 0 . dyn_request ( req) . await ?;
49
67
50
68
let res = HttpTypesResponse :: try_from ( response) . await ?. into_inner ( ) ;
51
69
Ok ( res)
0 commit comments