use async_trait::async_trait;
use axum::response::IntoResponse;
use hyper_util::{client::legacy::Client, rt::TokioExecutor};
pub type HyperClient = Client<
hyper_tls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>,
axum::body::Body,
>;
#[async_trait]
pub trait HttpClient: std::fmt::Debug {
async fn request(
&self,
req: axum::extract::Request,
) -> Result<axum::response::Response, Box<dyn std::error::Error + Send + Sync>>;
}
#[async_trait]
impl HttpClient for HyperClient {
async fn request(
&self,
req: axum::extract::Request,
) -> Result<axum::response::Response, Box<dyn std::error::Error + Send + Sync>> {
self.request(req)
.await
.map(|res| res.into_response())
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
}
}
pub fn create_hyper_client() -> HyperClient {
let https = hyper_tls::HttpsConnector::new();
Client::builder(TokioExecutor::new()).build(https)
}