use std::collections::BTreeMap;
pub struct Request {
pub method: String,
pub url: String,
pub body: Vec<u8>,
pub headers: BTreeMap<String, String>,
}
impl Request {
#[allow(clippy::needless_pass_by_value)]
pub fn get(url: impl ToString) -> Self {
Self {
method: "GET".to_owned(),
url: url.to_string(),
body: vec![],
headers: crate::headers(&[("Accept", "*/*")]),
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn post(url: impl ToString, body: Vec<u8>) -> Self {
Self {
method: "POST".to_owned(),
url: url.to_string(),
body,
headers: crate::headers(&[
("Accept", "*/*"),
("Content-Type", "text/plain; charset=utf-8"),
]),
}
}
}
#[derive(Clone, PartialEq)]
pub struct Response {
pub url: String,
pub ok: bool,
pub status: u16,
pub status_text: String,
pub bytes: Vec<u8>,
pub headers: BTreeMap<String, String>,
}
impl Response {
pub fn text(&self) -> Option<&str> {
std::str::from_utf8(&self.bytes).ok()
}
pub fn content_type(&self) -> Option<&str> {
self.headers.get("content-type").map(|s| s.as_str())
}
}
impl std::fmt::Debug for Response {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.debug_struct("Response")
.field("url", &self.url)
.field("ok", &self.ok)
.field("status", &self.status)
.field("status_text", &self.status_text)
.field("headers", &self.headers)
.finish_non_exhaustive()
}
}
pub type Error = String;
pub type Result<T> = std::result::Result<T, Error>;