use crate::error::Error;
use async_trait::async_trait;
use reqwest::Response;
#[async_trait]
pub trait HttpClient: Send + Sync {
async fn get(&self, url: &str) -> Result<Response, Error>;
async fn post(
&self,
url: &str,
body: impl Into<reqwest::Body> + Send,
) -> Result<Response, Error>;
}
pub struct ReqwestClient(pub reqwest::Client);
impl ReqwestClient {
pub async fn get(&self, url: &str) -> Result<reqwest::Response, crate::error::Error> {
self.0
.get(url)
.send()
.await
.map_err(crate::error::Error::from)
}
}
#[async_trait]
impl HttpClient for ReqwestClient {
async fn get(&self, url: &str) -> Result<Response, Error> {
self.0.get(url).send().await.map_err(Error::NetworkError)
}
async fn post(
&self,
url: &str,
body: impl Into<reqwest::Body> + Send,
) -> Result<Response, Error> {
self.0
.post(url)
.body(body)
.send()
.await
.map_err(Error::NetworkError)
}
}