use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use crate::direction::DirectionKind;
use crate::packer::Packer;
use crate::packers::JsonPacker;
#[derive(Debug, Clone)]
pub struct RocketConfig {
pub method: reqwest::Method,
pub url: String,
pub headers: HashMap<String, String>,
pub body: Option<String>,
pub http: HttpOptions,
pub direction: DirectionKind,
}
impl Default for RocketConfig {
fn default() -> Self {
Self {
method: reqwest::Method::POST,
url: String::new(),
headers: HashMap::new(),
body: None,
http: HttpOptions::default(),
direction: DirectionKind::Json,
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct HttpOptions {
pub timeout: Option<u64>,
pub connect_timeout: Option<u64>,
pub pool_idle_timeout: Option<u64>,
pub pool_max_idle_per_host: Option<usize>,
}
pub struct Rocket {
params: HashMap<String, Value>,
pub payload: HashMap<String, Value>,
pub config: RocketConfig,
pub radar: Option<reqwest::Request>,
pub destination_origin: Option<reqwest::Response>,
pub destination: Option<crate::direction::Destination>,
pub packer: Arc<dyn Packer>,
}
impl std::fmt::Debug for Rocket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Rocket")
.field("params", &self.params)
.field("payload", &self.payload)
.field("config", &self.config)
.finish()
}
}
impl Rocket {
pub fn new(params: HashMap<String, Value>) -> Self {
Self {
params,
payload: HashMap::new(),
config: RocketConfig::default(),
radar: None,
destination_origin: None,
destination: None,
packer: Arc::new(JsonPacker),
}
}
pub fn get_params(&self) -> &HashMap<String, Value> {
&self.params
}
pub fn merge_payload(&mut self, params: HashMap<String, Value>) {
self.payload.extend(params);
}
pub fn set_method(&mut self, method: reqwest::Method) {
self.config.method = method;
}
pub fn set_url(&mut self, url: impl Into<String>) {
self.config.url = url.into();
}
pub fn add_header(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.config.headers.insert(key.into(), value.into());
}
pub fn set_body(&mut self, body: impl Into<String>) {
self.config.body = Some(body.into());
}
pub fn set_timeout(&mut self, timeout: u64) {
self.config.http.timeout = Some(timeout);
}
}
impl From<HashMap<String, Value>> for Rocket {
fn from(params: HashMap<String, Value>) -> Self {
Self::new(params)
}
}