use super::HttpError;
#[derive(Debug, Clone)]
pub struct HttpRequest {
pub method: http::Method,
pub url: url::Url,
pub headers: http::HeaderMap,
pub body: Option<Vec<u8>>,
}
impl HttpRequest {
#[must_use]
pub fn new(method: http::Method, url: url::Url) -> Self {
Self {
method,
url,
headers: http::HeaderMap::new(),
body: None,
}
}
#[must_use]
pub fn get(url: url::Url) -> Self {
Self::new(http::Method::GET, url)
}
#[must_use]
pub fn post(url: url::Url) -> Self {
Self::new(http::Method::POST, url)
}
#[must_use]
pub fn with_body(mut self, body: Vec<u8>) -> Self {
self.body = Some(body);
self
}
#[must_use]
pub fn with_header(mut self, name: http::HeaderName, value: http::HeaderValue) -> Self {
self.headers.append(name, value);
self
}
}
#[derive(Debug, Clone)]
pub struct HttpResponse {
pub status: http::StatusCode,
pub headers: http::HeaderMap,
pub body: Vec<u8>,
}
impl HttpResponse {
#[must_use]
pub const fn new(status: http::StatusCode, headers: http::HeaderMap, body: Vec<u8>) -> Self {
Self {
status,
headers,
body,
}
}
#[must_use]
pub fn is_success(&self) -> bool {
self.status.is_success()
}
#[must_use]
pub fn body_text(&self) -> Option<&str> {
std::str::from_utf8(&self.body).ok()
}
}
pub trait HttpClient: Send + Sync {
fn request(
&self,
req: HttpRequest,
) -> impl std::future::Future<Output = Result<HttpResponse, HttpError>> + Send;
}