use super::*;
pub type RequestResult = Result<HttpResponse, RequestError>;
#[derive(Clone, Debug, Default, GetterMut)]
pub struct HttpRequest {
#[get_mut(skip)]
pub method: Method,
#[get_mut(skip)]
pub url: String,
pub headers: HashMap<String, String>,
#[get_mut(skip)]
pub body: Body,
#[get_mut(skip)]
pub config: RequestConfig,
#[get_mut(skip)]
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.get_mut_headers().remove(&normalized);
self
}
pub fn clear_headers(&mut self) -> &mut Self {
self.get_mut_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
}
pub fn get_method(&self) -> Method {
self.method.clone()
}
pub fn get_url(&self) -> String {
self.url.clone()
}
pub fn get_url_ref(&self) -> &str {
self.url.as_str()
}
pub fn get_headers(&self) -> HashMap<String, String> {
self.headers.clone()
}
pub fn get_headers_ref(&self) -> &HashMap<String, String> {
&self.headers
}
pub fn get_headers_mut(&mut self) -> &mut HashMap<String, String> {
&mut self.headers
}
pub fn get_body(&self) -> Body {
self.body.clone()
}
pub fn get_body_ref(&self) -> &Body {
&self.body
}
pub fn get_config(&self) -> RequestConfig {
self.config.clone()
}
pub fn get_config_ref(&self) -> &RequestConfig {
&self.config
}
pub fn get_config_mut(&mut self) -> &mut RequestConfig {
&mut self.config
}
pub(crate) fn get_tmp_ref(&self) -> &Tmp {
&self.tmp
}
pub(crate) fn get_tmp_mut(&mut self) -> &mut Tmp {
&mut self.tmp
}
fn normalize_header_key(key: &str) -> String {
key.to_ascii_lowercase()
}
}