use std::thread;
#[cfg(feature = "reqwest")] use reqwest::blocking::{Client, Request, Response};
use crate::*;
pub trait Http
where
Self: Send + Sync,
{
type Request: Send + TryClone;
type Response: Send;
fn request(&self, request: Self::Request) -> Result<Self::Response>;
fn request_with_retries(
&self,
request: Self::Request,
retries: u32,
retry_delay_ms: u64,
) -> Result<Self::Response> {
for i in 1..=retries {
match self.request(request.try_clone().ok_or(Error::NonRetriableRequest)?) {
Ok(r) => return Ok(r),
Err(e) => {
tracing::error!("attempt {i}/{retries}, {e:?}, retrying in {retry_delay_ms}ms");
thread::sleep(Duration::from_millis(retry_delay_ms));
},
}
}
Err(Error::ExceededMaxRetries(retries))?
}
}
#[cfg(feature = "reqwest")]
impl Http for Client {
type Request = Request;
type Response = Response;
fn request(&self, request: Self::Request) -> Result<Self::Response> {
Ok(self.execute(request)?)
}
}
#[cfg(feature = "reqwest")]
impl TryClone for Request {
fn try_clone(&self) -> Option<Self> {
self.try_clone()
}
}