use async_trait::async_trait;
#[derive(Debug, Clone)]
pub struct HttpResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
impl HttpResponse {
pub fn is_success(&self) -> bool {
(200..300).contains(&self.status)
}
pub fn header(&self, name: &str) -> Option<&str> {
let name = name.to_ascii_lowercase();
self.headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(&name))
.map(|(_, v)| v.as_str())
}
}
#[derive(Debug, Clone)]
pub struct TransportError(pub String);
impl core::fmt::Display for TransportError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for TransportError {}
#[async_trait(?Send)]
pub trait HttpTransport {
async fn get(&self, url: &str) -> Result<HttpResponse, TransportError>;
async fn post_json(&self, url: &str, body: String) -> Result<HttpResponse, TransportError>;
}