use std::future::Future;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method {
Get,
Post,
}
#[derive(Debug, Clone)]
pub struct HttpRequest {
pub method: Method,
pub url: String,
pub headers: Vec<(String, String)>,
}
impl HttpRequest {
pub fn get(url: impl Into<String>) -> Self {
Self {
method: Method::Get,
url: url.into(),
headers: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct HttpResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
impl HttpResponse {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|(key, _)| key.eq_ignore_ascii_case(name))
.map(|(_, value)| value.as_str())
}
}
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct TransportError(pub String);
pub trait Http {
fn send(
&self,
request: HttpRequest,
) -> impl Future<Output = Result<HttpResponse, TransportError>> + Send;
}