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
61
62
63
64
65
66
67
68
69
70
71
72
73
use futures::{future, Future};
use hyper::client::HttpConnector;
use hyper_tls::HttpsConnector;
mod error;
pub use error::Error;
pub use http;
pub mod content;
pub use content::{RequestContent, ResponseContent};
pub type ResponseFuture<T> =
Box<dyn Future<Item = http::Response<T>, Error = Error> + Send + 'static + Unpin>;
pub trait HttpClient
where
Self: Send + Sync + Sized + 'static,
{
fn perform(&self, r: hyper::Request<hyper::Body>) -> hyper::client::ResponseFuture;
fn send<Req, Res>(&self, r: http::Request<Req>) -> ResponseFuture<Res::Data>
where
Req: RequestContent + 'static,
Res: ResponseContent + 'static,
{
let (mut parts, body) = r.into_parts();
let hyper_body = match body.into_body() {
Ok(body) => body,
Err(err) => return Box::new(future::err(err)),
};
if let Some(suggested_content_type) = Req::content_type() {
if !parts.headers.contains_key(http::header::CONTENT_TYPE) {
parts
.headers
.insert(http::header::CONTENT_TYPE, suggested_content_type);
}
}
let request = hyper::Request::from_parts(parts, hyper_body);
let fut = self
.perform(request)
.map_err(Error::from)
.and_then(|response| Res::convert_response(response));
Box::new(fut)
}
fn https(connector_threads: usize) -> HttpsClient {
HttpsClient::new(connector_threads)
}
}
pub struct HttpsClient {
http_client: hyper::Client<HttpsConnector<HttpConnector>>,
}
impl HttpsClient {
pub fn new(connector_threads: usize) -> HttpsClient {
let https = HttpsConnector::new(connector_threads).expect("Setting up SSL connector");
let http_client = hyper::Client::builder().build::<_, hyper::Body>(https);
HttpsClient { http_client }
}
}
impl HttpClient for HttpsClient {
fn perform(&self, r: hyper::Request<hyper::Body>) -> hyper::client::ResponseFuture {
self.http_client.request(r)
}
}