use rig_http::http_client::{
HttpClientExt, LazyBody, MultipartForm, Request, Response, Result, StreamingResponse,
};
use rig_http::wasm_compat::WasmCompatSend;
use super::{
Bytes, Error, Execute, Payload, into_response, into_streaming_response, multipart_form,
send_with,
};
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "reqwest-middleware-rustls",
feature = "reqwest-middleware-native-tls"
)))
)]
#[derive(Clone, Debug)]
pub struct ReqwestMiddlewareClient(reqwest_middleware::ClientWithMiddleware);
impl ReqwestMiddlewareClient {
#[must_use]
pub fn into_inner(self) -> reqwest_middleware::ClientWithMiddleware {
self.0
}
}
impl From<reqwest_middleware::ClientWithMiddleware> for ReqwestMiddlewareClient {
fn from(client: reqwest_middleware::ClientWithMiddleware) -> Self {
Self(client)
}
}
impl AsRef<reqwest_middleware::ClientWithMiddleware> for ReqwestMiddlewareClient {
fn as_ref(&self) -> &reqwest_middleware::ClientWithMiddleware {
&self.0
}
}
impl Execute for ReqwestMiddlewareClient {
fn execute(
&self,
parts: http::request::Parts,
payload: Result<Payload>,
) -> Result<impl Future<Output = Result<reqwest::Response>> + WasmCompatSend + 'static> {
let request = self
.0
.request(parts.method, parts.uri.to_string())
.headers(parts.headers);
let request = match payload? {
Payload::Bytes(bytes) => request.body(bytes),
Payload::Form(form) => request.multipart(form),
};
Ok(async move { request.send().await.map_err(Error::instance) })
}
}
http_client_ext!(ReqwestMiddlewareClient);