#[cfg(feature = "aws-auth")]
use reqwest::{Method, Response};
#[cfg(feature = "aws-auth")]
use serde::Deserialize;
#[derive(Clone, Debug, Default)]
pub(crate) struct HttpClient {
#[cfg(feature = "aws-auth")]
inner: reqwest::Client,
}
#[cfg(feature = "aws-auth")]
impl HttpClient {
pub(crate) async fn get_and_deserialize_json<'a, T>(
&self,
uri: &str,
headers: impl IntoIterator<Item = &'a (&'a str, &'a str)>,
) -> reqwest::Result<T>
where
T: for<'de> Deserialize<'de>,
{
let value = self
.request(Method::GET, uri, headers)
.await?
.json()
.await?;
Ok(value)
}
pub(crate) async fn get_and_read_string<'a>(
&self,
uri: &str,
headers: impl IntoIterator<Item = &'a (&'a str, &'a str)>,
) -> reqwest::Result<String> {
self.request_and_read_string(Method::GET, uri, headers)
.await
}
pub(crate) async fn put_and_read_string<'a>(
&self,
uri: &str,
headers: impl IntoIterator<Item = &'a (&'a str, &'a str)>,
) -> reqwest::Result<String> {
self.request_and_read_string(Method::PUT, uri, headers)
.await
}
pub(crate) async fn request_and_read_string<'a>(
&self,
method: Method,
uri: &str,
headers: impl IntoIterator<Item = &'a (&'a str, &'a str)>,
) -> reqwest::Result<String> {
let text = self.request(method, uri, headers).await?.text().await?;
Ok(text)
}
pub(crate) async fn request<'a>(
&self,
method: Method,
uri: &str,
headers: impl IntoIterator<Item = &'a (&'a str, &'a str)>,
) -> reqwest::Result<Response> {
let response = headers
.into_iter()
.fold(self.inner.request(method, uri), |request, (k, v)| {
request.header(*k, *v)
})
.send()
.await?;
Ok(response)
}
}