use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Request {
pub body: serde_json::Value,
pub headers: HashMap<String, String>,
pub query: HashMap<String, String>,
pub path_params: HashMap<String, String>,
}
impl Request {
pub fn new(body: serde_json::Value) -> Self {
Self {
body,
headers: HashMap::new(),
query: HashMap::new(),
path_params: HashMap::new(),
}
}
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.insert(name.into(), value.into());
self
}
pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
self.headers.extend(headers);
self
}
pub fn query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.query.insert(key.into(), value.into());
self
}
pub fn queries(mut self, query: HashMap<String, String>) -> Self {
self.query.extend(query);
self
}
pub fn path_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.path_params.insert(key.into(), value.into());
self
}
pub fn path_params(mut self, path_params: HashMap<String, String>) -> Self {
self.path_params.extend(path_params);
self
}
pub fn token(&self) -> Option<&str> {
self.headers
.get("Authorization")
.or_else(|| self.headers.get("authorization"))
.and_then(|v| v.strip_prefix("Bearer "))
}
pub fn get_header(&self, name: &str) -> Option<&str> {
self.headers.get(name).map(|s| s.as_str())
}
pub fn get_query(&self, key: &str) -> Option<&str> {
self.query.get(key).map(|s| s.as_str())
}
pub fn get_path_param(&self, key: &str) -> Option<&str> {
self.path_params.get(key).map(|s| s.as_str())
}
}
impl Default for Request {
fn default() -> Self {
Self::new(serde_json::Value::Null)
}
}