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
use async_trait::async_trait; 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}; #[async_trait] pub trait HttpClient where Self: Send + Sync + Sized, { async fn perform( &self, r: hyper::Request<hyper::Body>, ) -> Result<hyper::Response<hyper::Body>, hyper::Error>; async fn send<Req, Res>( &self, r: http::Request<Req>, ) -> Result<http::Response<Res::Data>, Error> where Req: RequestContent + Send, Res: ResponseContent, { let (mut parts, body) = r.into_parts(); body.apply_headers(&mut parts.headers); let hyper_body: hyper::Body = body.into_body()?; let request = hyper::Request::from_parts(parts, hyper_body); let http_res = self.perform(request).await?; let converted = Res::convert_response(http_res).await; converted } fn https() -> HttpsClient { HttpsClient::new() } } pub struct HttpsClient { http_client: hyper::Client<HttpsConnector<HttpConnector>>, } impl HttpsClient { pub fn new() -> HttpsClient { let http_client = hyper::Client::builder().build::<_, hyper::Body>(HttpsConnector::new()); HttpsClient { http_client } } } #[async_trait] impl HttpClient for HttpsClient { async fn perform( &self, r: hyper::Request<hyper::Body>, ) -> Result<hyper::Response<hyper::Body>, hyper::Error> { self.http_client.request(r).await } }