use axum::extract::FromRequestParts;
use axum::http::request::Parts;
use axum::http::HeaderMap;
use std::collections::BTreeMap;
use std::convert::Infallible;
pub struct Request {
headers: HeaderMap,
}
impl Request {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers.get(name)?.to_str().ok()
}
pub fn headers(&self) -> BTreeMap<String, String> {
self.headers
.iter()
.map(|(name, value)| {
let value = value.to_str().unwrap_or("<non-utf8>").to_string();
(name.to_string(), value)
})
.collect()
}
}
#[axum::async_trait]
impl<S> FromRequestParts<S> for Request
where
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
Ok(Request {
headers: parts.headers.clone(),
})
}
}