meteroid-rs 0.31.4

Official Rust SDK for Meteroid, the open-source billing platform: subscriptions, invoicing, usage-based billing and webhooks.
Documentation
// this file is @generated by Perseid; edit the shared runtime template.
//! The HTTP client: hyper over rustls or native-tls by default, or one you provide.
use std::{fmt, sync::Arc};

use hyper::body::Incoming;
use hyper_util::{
    client::legacy::{
        connect::{Connect, HttpConnector},
        Client,
    },
    rt::TokioExecutor,
};

use crate::api::{
    middleware::{BoxError, BoxFuture},
    upload::RequestBody,
};

/// Sends one HTTP request. Implemented for hyper's client over any connector, which is how
/// to bring custom TLS roots, client certificates or a proxy.
pub trait HttpClient: Send + Sync {
    /// Sends `request`, resolving once the response head arrived.
    fn send(
        &self,
        request: http::Request<RequestBody>,
    ) -> BoxFuture<'_, Result<http::Response<Incoming>, BoxError>>;
}

impl<C> HttpClient for Client<C, RequestBody>
where
    C: Connect + Clone + Send + Sync + 'static,
{
    fn send(
        &self,
        request: http::Request<RequestBody>,
    ) -> BoxFuture<'_, Result<http::Response<Incoming>, BoxError>> {
        let response = self.request(request);
        Box::pin(async move { Ok(response.await?) })
    }
}

impl fmt::Debug for dyn HttpClient {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("HttpClient")
    }
}

/// A hyper client over `connector`.
pub fn http_client<C>(connector: C) -> Arc<dyn HttpClient>
where
    C: Connect + Clone + Send + Sync + 'static,
{
    Arc::new(Client::builder(TokioExecutor::new()).build::<_, RequestBody>(connector))
}

#[cfg(not(any(feature = "native-tls", feature = "rustls-tls")))]
pub(crate) fn make_connector() -> HttpConnector {
    HttpConnector::new()
}

#[cfg(all(feature = "native-tls", not(feature = "rustls-tls")))]
pub(crate) fn make_connector() -> hyper_tls::HttpsConnector<HttpConnector> {
    let mut http = HttpConnector::new();
    http.enforce_http(false);
    hyper_tls::HttpsConnector::new_with_connector(http)
}

/// Trusts the platform's roots. Without any, requests fail with a TLS error rather than
/// the client panicking on creation.
#[cfg(feature = "rustls-tls")]
pub(crate) fn make_connector() -> hyper_rustls::HttpsConnector<HttpConnector> {
    let provider = rustls::crypto::CryptoProvider::get_default()
        .cloned()
        .unwrap_or_else(|| Arc::new(rustls::crypto::ring::default_provider()));
    let builder = hyper_rustls::HttpsConnectorBuilder::new();
    let builder = match builder.with_provider_and_native_roots(provider.clone()) {
        Ok(builder) => builder,
        Err(_) => hyper_rustls::HttpsConnectorBuilder::new().with_tls_config(
            rustls::ClientConfig::builder_with_provider(provider)
                .with_safe_default_protocol_versions()
                .expect("the default provider supports the default protocol versions")
                .with_root_certificates(rustls::RootCertStore::empty())
                .with_no_client_auth(),
        ),
    };
    let builder = builder.https_or_http();
    #[cfg(feature = "http1")]
    let builder = builder.enable_http1();
    #[cfg(feature = "http2")]
    let builder = builder.enable_http2();

    let mut http = HttpConnector::new();
    http.enforce_http(false);
    builder.wrap_connector(http)
}