use super::*;
pub type RequestResult = Result<HttpResponse, RequestError>;
#[derive(Clone, Debug, Default)]
pub struct HttpRequest {
pub method: Method,
pub url: String,
pub headers: HashMap<String, String>,
pub body: Body,
pub config: RequestConfig,
pub(crate) tmp: Tmp,
}
impl HttpRequest {
pub fn get(url: impl Into<String>) -> Self {
Self {
method: Method::Get,
url: url.into(),
headers: HashMap::new(),
body: Body::default(),
config: RequestConfig::default(),
tmp: Tmp::default(),
}
}
pub fn post(url: impl Into<String>) -> Self {
Self {
method: Method::Post,
url: url.into(),
headers: HashMap::new(),
body: Body::default(),
config: RequestConfig::default(),
tmp: Tmp::default(),
}
}
pub fn set_method(&mut self, method: Method) -> &mut Self {
self.method = method;
self
}
pub fn set_url(&mut self, url: impl Into<String>) -> &mut Self {
self.url = url.into();
self
}
pub fn set_header<K: AsRef<str>, V: AsRef<str>>(&mut self, key: K, value: V) -> &mut Self {
let normalized = Self::normalize_header_key(key.as_ref());
self.headers.insert(normalized, value.as_ref().to_owned());
self
}
pub fn remove_header<K: AsRef<str>>(&mut self, key: K) -> &mut Self {
let normalized = Self::normalize_header_key(key.as_ref());
self.headers.remove(&normalized);
self
}
pub fn clear_headers(&mut self) -> &mut Self {
self.headers.clear();
self
}
pub fn set_body(&mut self, body: Body) -> &mut Self {
self.body = body;
self
}
pub fn set_config(&mut self, config: RequestConfig) -> &mut Self {
self.config = config;
self
}
fn normalize_header_key(key: &str) -> String {
key.to_ascii_lowercase()
}
}