diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2021-04-04 18:50:07 -0400 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2021-04-04 18:50:07 -0400 |
commit | b76b054118f29aa8ab31722831518398b3b7f40d (patch) | |
tree | 8182e36a31ecf521ef9a81a7e516a137f10510b0 | |
parent | 6b12cd5ad2ae7212e2ad3077463cbec453da4774 (diff) |
make HyperBackend generic
-rw-r--r-- | ipfs-api-backend-hyper/src/backend.rs | 56 |
1 files changed, 35 insertions, 21 deletions
diff --git a/ipfs-api-backend-hyper/src/backend.rs b/ipfs-api-backend-hyper/src/backend.rs index 01e30de..c496de6 100644 --- a/ipfs-api-backend-hyper/src/backend.rs +++ b/ipfs-api-backend-hyper/src/backend.rs @@ -17,40 +17,54 @@ use http::{ }; use hyper::{ body, - client::{self, Builder, HttpConnector}, + client::{self, Builder, connect::Connect, HttpConnector}, }; -use hyper_tls::HttpsConnector; use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri}; use multipart::client::multipart; use serde::Serialize; -pub struct HyperBackend { +pub struct HyperBackend<C = HttpConnector> where C: Connect + Clone + Send + Sync + 'static { base: Uri, - client: client::Client<HttpsConnector<HttpConnector>, hyper::Body>, + client: client::Client<C, hyper::Body>, } -impl Default for HyperBackend { - /// Creates an `IpfsClient` connected to the endpoint specified in ~/.ipfs/api. - /// If not found, tries to connect to `localhost:5001`. - /// - fn default() -> Self { - Self::from_ipfs_config() - .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) - } +macro_rules! impl_default { + ($http_connector:path) => { + impl_default!($http_connector, <$http_connector>::new()); + }; + ($http_connector:path, $constructor:expr) => { + impl Default for HyperBackend<$http_connector> { + /// Creates an `IpfsClient` connected to the endpoint specified in ~/.ipfs/api. + /// If not found, tries to connect to `localhost:5001`. + /// + fn default() -> Self { + Self::from_ipfs_config() + .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) + } + } + + impl TryFromUri for HyperBackend<$http_connector> { + fn build_with_base_uri(base: Uri) -> Self { + let client = Builder::default() + .pool_max_idle_per_host(0) + .build($constructor); + + HyperBackend { base, client } + } + } + }; } -impl TryFromUri for HyperBackend { - fn build_with_base_uri(base: Uri) -> Self { - let client = Builder::default() - .pool_max_idle_per_host(0) - .build(HttpsConnector::new()); +impl_default!(HttpConnector); - HyperBackend { base, client } - } -} +#[cfg(feature = "with-hyper-tls")] +impl_default!(hyper_tls::HttpsConnector<HttpConnector>); + +#[cfg(feature = "with-hyper-rustls")] +impl_default!(hyper_rustls::HttpsConnector<HttpConnector>, hyper_rustls::HttpsConnector::with_native_roots()); #[async_trait(?Send)] -impl Backend for HyperBackend { +impl<C> Backend for HyperBackend<C> where C: Connect + Clone + Send + Sync + 'static { type HttpRequest = http::Request<hyper::Body>; type HttpResponse = http::Response<hyper::Body>; |