1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use hyper::client::{connect::Connect, HttpConnector};
use super::{DefaultConstructibleHttpClient, HttpClient};
pub type Hyper = hyper::Client<HttpConnector>;
#[cfg(feature = "hyper-native-tls")]
pub type HyperNativeTls = hyper::Client<hyper_tls::HttpsConnector<HttpConnector>>;
#[cfg(feature = "hyper-rustls")]
pub type HyperRustls = hyper::Client<hyper_rustls::HttpsConnector<HttpConnector>>;
#[async_trait]
impl<C> HttpClient for hyper::Client<C>
where
C: Connect + Clone + Send + Sync + 'static,
{
type RequestBody = BytesMut;
type ResponseBody = Bytes;
type Error = hyper::Error;
async fn send_http_request(
&self,
req: http::Request<BytesMut>,
) -> Result<http::Response<Bytes>, hyper::Error> {
let (head, body) = self
.request(req.map(|body| hyper::body::Body::from(body.freeze())))
.await?
.into_parts();
let body = hyper::body::to_bytes(body).await?;
Ok(http::Response::from_parts(head, body))
}
}
#[cfg(feature = "hyper")]
impl DefaultConstructibleHttpClient for Hyper {
fn default() -> Self {
hyper::Client::new()
}
}
#[cfg(feature = "hyper-native-tls")]
impl DefaultConstructibleHttpClient for HyperNativeTls {
fn default() -> Self {
hyper::Client::builder().build(hyper_tls::HttpsConnector::new())
}
}