use crate::error::Result;
use std::future::Future;
pub trait Response {
fn status(&self) -> u16;
fn body(self) -> String;
}
pub trait Request: Send + Sync {
type Response: Response;
fn new() -> Self
where
Self: Sized;
fn get(&self, url: &str) -> impl Future<Output = Result<Self::Response>> + Send;
fn post(&self, url: &str, body: &str) -> impl Future<Output = Result<Self::Response>> + Send;
}
#[cfg(feature = "reqwest")]
pub struct HttpResponse {
status: u16,
body: String,
}
#[cfg(feature = "reqwest")]
impl Response for HttpResponse {
fn status(&self) -> u16 {
self.status
}
fn body(self) -> String {
self.body
}
}
#[cfg(feature = "reqwest")]
impl Request for reqwest::Client {
type Response = HttpResponse;
fn new() -> Self {
reqwest::Client::new()
}
async fn get(&self, url: &str) -> Result<Self::Response> {
let response = self.get(url).send().await?;
let status = response.status().as_u16();
let body = response.text().await?;
Ok(HttpResponse { status, body })
}
async fn post(&self, url: &str, body: &str) -> Result<Self::Response> {
let response = self.post(url).body(body.to_string()).send().await?;
let status = response.status().as_u16();
let body = response.text().await?;
Ok(HttpResponse { status, body })
}
}