use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiRequest {
pub method: String,
pub path: String,
#[serde(default)]
pub query: HashMap<String, String>,
#[serde(default)]
pub headers: HashMap<String, String>,
pub body: Option<String>,
}
impl ApiRequest {
pub fn new(method: impl Into<String>, path: impl Into<String>) -> Self {
Self {
method: method.into(),
path: path.into(),
query: HashMap::new(),
headers: HashMap::new(),
body: None,
}
}
pub fn with_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.query.insert(key.into(), value.into());
self
}
pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.insert(key.into(), value.into());
self
}
pub fn with_body(mut self, body: impl Into<String>) -> Self {
self.body = Some(body.into());
self
}
pub fn parse_body<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
match &self.body {
Some(body) => serde_json::from_str(body),
None => serde_json::from_str("{}"), }
}
pub fn get_query(&self, key: &str) -> Option<&String> {
self.query.get(key)
}
pub fn get_header(&self, key: &str) -> Option<&String> {
self.headers.get(key)
}
}