use std::collections::HashMap;
use bytes::Bytes;
use serde_json::Value;
use super::HttpMethod;
#[derive(Debug, Clone)]
pub struct Request {
pub method: HttpMethod,
pub path: String,
pub prefix: String,
pub query: Value,
pub body: Bytes,
pub data: Value,
pub headers: HashMap<String, String>,
pub auth_token: Option<String>,
pub connection_id: Option<String>,
pub ip_address: Option<String>,
}
impl Request {
pub fn resource_and_operation(&self) -> (String, Option<String>) {
split_resource_and_operation(&self.path, &self.prefix)
}
pub fn header(&self, name: &str) -> Option<&str> {
self.headers.get(&name.to_ascii_lowercase()).map(String::as_str)
}
pub fn bearer(&self) -> Option<&str> {
self.auth_token.as_deref().map(|t| {
let t = t.trim_start();
if t.len() >= 7 && t[..7].eq_ignore_ascii_case("bearer ") {
t[7..].trim_start()
} else {
t
}
})
}
}
pub(crate) fn split_resource_and_operation(path: &str, prefix: &str) -> (String, Option<String>) {
let stripped = path
.strip_prefix('/')
.unwrap_or(path)
.strip_prefix(prefix)
.map(|rest| rest.trim_start_matches('/'))
.unwrap_or(path);
let mut parts = stripped.split('/');
let resource = parts.next().unwrap_or_default().to_string();
let operation = parts.next().map(|s| s.to_string());
(resource, operation)
}