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
74
75
76
77
78
79
use hyper::client::{Client, Connect, HttpConnector};
use hyper::Body;
use std::io;
use tokio_core::reactor::Handle;
pub trait ClientCreator: Send + 'static {
type Connect: Connect;
type Error: ::std::error::Error + Send;
fn create(&self, handle: &Handle) -> Result<Client<Self::Connect, Body>, Self::Error>;
}
#[derive(Debug, Default)]
pub struct DefaultClient;
impl ClientCreator for DefaultClient {
type Connect = HttpConnector;
type Error = io::Error;
fn create(&self, handle: &Handle) -> Result<Client<HttpConnector, Body>, io::Error> {
Ok(Client::new(handle))
}
}
impl<C, E, F> ClientCreator for F
where
C: Connect,
E: ::std::error::Error + Send,
F: Fn(&Handle) -> Result<Client<C, Body>, E>,
F: Send + 'static,
{
type Connect = C;
type Error = E;
fn create(&self, handle: &Handle) -> Result<Client<C, Body>, E> {
(self)(handle)
}
}
#[cfg(feature = "tls")]
mod tls {
use super::*;
use hyper_tls::HttpsConnector;
use native_tls::Error;
static DNS_THREADS: usize = 2;
#[derive(Debug, Default)]
pub struct DefaultTlsClient;
impl ClientCreator for DefaultTlsClient {
type Connect = HttpsConnector<HttpConnector>;
type Error = Error;
fn create(
&self,
handle: &Handle,
) -> Result<Client<HttpsConnector<HttpConnector>, Body>, Error> {
let connector = HttpsConnector::new(DNS_THREADS, handle)?;
let client = Client::configure().connector(connector).build(handle);
Ok(client)
}
}
}
#[cfg(feature = "tls")]
pub use self::tls::*;