use std::ops::Deref;
use reqwest::{header::{HeaderMap, HeaderValue, InvalidHeaderValue}, Request, Response};
#[derive(Debug, thiserror::Error)]
#[allow(clippy::module_name_repetitions)]
pub enum ClientError {
#[error("failed to set access key header: {0}")]
AccessKey(#[from] InvalidHeaderValue),
#[error("failed to build client: {0}")]
BuildClient(#[from] reqwest::Error),
}
#[derive(Debug)]
pub struct Client(reqwest::Client);
impl Client {
pub fn new(access_key: &str) -> Result<Self, ClientError> {
let access_key = HeaderValue::from_str(access_key)?;
let mut headers = HeaderMap::with_capacity(1);
headers.insert("AccessKey", access_key);
let inner = reqwest::Client::builder()
.default_headers(headers)
.https_only(true)
.build()?;
Ok(Self(inner))
}
pub(crate) async fn send_logged(&self, req: Request) -> Result<Response, reqwest::Error> {
#[cfg(feature = "tracing")]
tracing::debug!(
method=%req.method(),
url=%req.url(),
headers=?req.headers(),
"about to send request",
);
self.execute(req).await
}
}
impl Deref for Client {
type Target = reqwest::Client;
fn deref(&self) -> &Self::Target {
&self.0
}
}