use http::HeaderName;
use http::Method;
use super::plugin::IdempotencyPlugin;
#[derive(Clone, Copy)]
pub enum Scope {
KeyOnly,
MethodAndPath,
}
#[derive(Clone)]
pub struct Config {
pub header: HeaderName,
pub methods: Vec<Method>,
pub ttl_secs: u64,
pub scope: Scope,
pub coalesce_inflight: bool,
pub inflight_wait_timeout_ms: Option<u64>,
pub max_cached_body_bytes: usize,
pub max_request_body_bytes: usize,
pub verify_payload: bool,
pub cache_error_statuses: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
header: HeaderName::from_static("idempotency-key"),
methods: vec![Method::POST],
ttl_secs: 86400,
scope: Scope::MethodAndPath,
coalesce_inflight: true,
inflight_wait_timeout_ms: None,
max_cached_body_bytes: 1024 * 1024,
max_request_body_bytes: 1024 * 1024,
verify_payload: true,
cache_error_statuses: true,
}
}
}
pub struct IdempotencyBuilder(Config);
impl Default for IdempotencyBuilder {
fn default() -> Self {
Self::new()
}
}
impl IdempotencyBuilder {
pub fn new() -> Self {
Self(Config::default())
}
pub fn header(mut self, h: HeaderName) -> Self {
self.0.header = h;
self
}
pub fn methods(mut self, m: &[Method]) -> Self {
self.0.methods = m.to_vec();
self
}
pub fn ttl_secs(mut self, s: u64) -> Self {
self.0.ttl_secs = s;
self
}
pub fn scope(mut self, s: Scope) -> Self {
self.0.scope = s;
self
}
pub fn coalesce_inflight(mut self, yes: bool) -> Self {
self.0.coalesce_inflight = yes;
self
}
pub fn inflight_wait_timeout_ms(mut self, ms: Option<u64>) -> Self {
self.0.inflight_wait_timeout_ms = ms;
self
}
pub fn max_cached_body_bytes(mut self, n: usize) -> Self {
self.0.max_cached_body_bytes = n;
self
}
pub fn max_request_body_bytes(mut self, n: usize) -> Self {
self.0.max_request_body_bytes = n;
self
}
pub fn verify_payload(mut self, yes: bool) -> Self {
self.0.verify_payload = yes;
self
}
pub fn cache_error_statuses(mut self, yes: bool) -> Self {
self.0.cache_error_statuses = yes;
self
}
pub fn build(self) -> IdempotencyPlugin {
IdempotencyPlugin::new(self.0)
}
}