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
//! A few common connectors for making requests.

#[cfg(feature = "proxy")]
pub use hyper_proxy as proxy;

use crate::internal;
use hyper::{
    client::{connect::Connect, HttpConnector},
    Body, Client,
};
use hyper_tls::HttpsConnector;

#[cfg(feature = "proxy")]
use proxy::ProxyConnector;

/// The default HTTPS connector.
pub type Https = HttpsConnector<HttpConnector>;

#[cfg(feature = "proxy")]
/// The default proxy connector.
pub type Proxy = ProxyConnector<Https>;

/// Constructs a HTTPS connector.
pub fn https() -> Https {
    HttpsConnector::new(num_cpus::get()).unwrap_or_else(|error| {
        panic!(
            "[tbot] Failed to construct an HTTPS connector: {:#?}",
            error,
        )
    })
}

#[cfg(feature = "proxy")]
/// Constructs a proxy connector.
pub fn proxy(proxy: proxy::Proxy) -> Proxy {
    ProxyConnector::from_proxy(https(), proxy).unwrap_or_else(|error| {
        panic!("[tbot] Failed to construct a proxy connector: {:#?}", error)
    })
}

pub(crate) fn create_client<C>(connector: C) -> internal::Client<C>
where
    C: Connect + Sync + 'static,
    C::Transport: 'static,
    C::Future: 'static,
{
    Client::builder()
        .keep_alive(false)
        .build::<C, Body>(connector)
}

pub(crate) fn default() -> internal::Client<Https> {
    create_client(https())
}