33
44//! The public `HttpClient` struct.
55
6+ use crate :: backend:: Backend ;
67use crate :: config:: { HttpClientBuilder , HttpClientConfig , TransportConfig } ;
78use crate :: { HttpClientError , HttpRequest , HttpResponse } ;
89use std:: time:: Duration ;
910
10- #[ cfg( any ( feature = "reqwest -backend" , feature = "hyper -backend" ) ) ]
11- use crate :: backend:: Backend ;
11+ #[ cfg( all ( feature = "hyper -backend" , not ( feature = "reqwest -backend" ) ) ) ]
12+ type BackendImpl = crate :: backend:: hyper_backend :: HyperBackend ;
1213
1314#[ cfg( feature = "reqwest-backend" ) ]
14- use crate :: backend:: reqwest_backend:: ReqwestBackend ;
15-
16- #[ cfg( all( feature = "hyper-backend" , not( feature = "reqwest-backend" ) ) ) ]
17- use crate :: backend:: hyper_backend:: HyperBackend ;
15+ type BackendImpl = crate :: backend:: reqwest_backend:: ReqwestBackend ;
1816
1917/// A high-level async HTTP client.
2018///
2119/// Constructed once and reused across many [`HttpClient::send`] calls. Holds
2220/// a connection pool internally.
2321#[ derive( Debug ) ]
2422pub struct HttpClient {
25- #[ cfg( feature = "reqwest-backend" ) ]
26- backend : ReqwestBackend ,
27- #[ cfg( all( feature = "hyper-backend" , not( feature = "reqwest-backend" ) ) ) ]
28- backend : HyperBackend ,
23+ backend : BackendImpl ,
2924 config : HttpClientConfig ,
3025}
3126
@@ -51,23 +46,8 @@ impl HttpClient {
5146 config : HttpClientConfig ,
5247 transport : TransportConfig ,
5348 ) -> Result < Self , HttpClientError > {
54- #[ cfg( feature = "reqwest-backend" ) ]
55- {
56- let backend = ReqwestBackend :: new ( config. timeout ( ) , transport) ?;
57- Ok ( Self { backend, config } )
58- }
59- #[ cfg( all( feature = "hyper-backend" , not( feature = "reqwest-backend" ) ) ) ]
60- {
61- let backend = HyperBackend :: new ( config. timeout ( ) , transport) ?;
62- Ok ( Self { backend, config } )
63- }
64- #[ cfg( not( any( feature = "reqwest-backend" , feature = "hyper-backend" ) ) ) ]
65- {
66- let _ = ( config, transport) ;
67- Err ( HttpClientError :: InvalidConfig (
68- "no backend feature enabled" . to_owned ( ) ,
69- ) )
70- }
49+ let backend = BackendImpl :: new ( config. timeout ( ) , transport) ?;
50+ Ok ( Self { backend, config } )
7151 }
7252
7353 /// The client's configuration.
@@ -88,17 +68,7 @@ impl HttpClient {
8868 }
8969
9070 async fn send_once ( & self , request : HttpRequest ) -> Result < HttpResponse , HttpClientError > {
91- #[ cfg( any( feature = "reqwest-backend" , feature = "hyper-backend" ) ) ]
92- {
93- self . backend . send ( request, & self . config ) . await
94- }
95- #[ cfg( not( any( feature = "reqwest-backend" , feature = "hyper-backend" ) ) ) ]
96- {
97- let _ = request;
98- Err ( HttpClientError :: InvalidConfig (
99- "no backend feature enabled" . to_owned ( ) ,
100- ) )
101- }
71+ self . backend . send ( request, & self . config ) . await
10272 }
10373
10474 async fn send_with_retry (
0 commit comments