use crate::client::WriteMode;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Could initialize the http client")]
InitHttpClient {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
},
#[error("{description}")]
Detail { description: String },
#[error("An IO error occurred while uploading the body of a POST request")]
PostBody(#[from] std::io::Error),
}
impl crate::IsSpuriousError for Error {
fn is_spurious(&self) -> bool {
match self {
Error::PostBody(err) => err.is_spurious(),
#[cfg(any(feature = "http-client-reqwest", feature = "http-client-curl"))]
Error::InitHttpClient { source } => {
#[cfg(feature = "http-client-curl")]
if let Some(err) = source.downcast_ref::<crate::client::http::curl::Error>() {
return err.is_spurious();
};
#[cfg(feature = "http-client-reqwest")]
if let Some(err) = source.downcast_ref::<crate::client::http::reqwest::remote::Error>() {
return err.is_spurious();
};
false
}
_ => false,
}
}
}
pub struct GetResponse<H, B> {
pub headers: H,
pub body: B,
}
pub struct PostResponse<H, B, PB> {
pub post_body: PB,
pub headers: H,
pub body: B,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
pub enum PostBodyDataKind {
BoundedAndFitsIntoMemory,
Unbounded,
}
impl From<WriteMode> for PostBodyDataKind {
fn from(m: WriteMode) -> Self {
match m {
WriteMode::Binary => PostBodyDataKind::Unbounded,
WriteMode::OneLfTerminatedLinePerWriteCall => PostBodyDataKind::BoundedAndFitsIntoMemory,
}
}
}
impl<A, B, C> From<PostResponse<A, B, C>> for GetResponse<A, B> {
fn from(v: PostResponse<A, B, C>) -> Self {
GetResponse {
headers: v.headers,
body: v.body,
}
}
}
#[allow(clippy::type_complexity)]
pub trait Http {
type Headers: std::io::BufRead + Unpin;
type ResponseBody: std::io::BufRead;
type PostBody: std::io::Write;
fn get(
&mut self,
url: &str,
base_url: &str,
headers: impl IntoIterator<Item = impl AsRef<str>>,
) -> Result<GetResponse<Self::Headers, Self::ResponseBody>, Error>;
fn post(
&mut self,
url: &str,
base_url: &str,
headers: impl IntoIterator<Item = impl AsRef<str>>,
body: PostBodyDataKind,
) -> Result<PostResponse<Self::Headers, Self::ResponseBody, Self::PostBody>, Error>;
fn configure(
&mut self,
config: &dyn std::any::Any,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;
}