Skip to main content

axonflow_sdk_rust/
config.rs

1use std::fmt;
2use std::time::Duration;
3
4#[derive(Default, Debug, Clone, PartialEq, Eq)]
5pub enum Mode {
6    #[default]
7    Production,
8    Sandbox,
9}
10
11#[derive(Debug, Clone)]
12pub struct RetryConfig {
13    pub enabled: bool,
14    pub max_attempts: u32,
15    pub initial_delay: Duration,
16}
17
18impl Default for RetryConfig {
19    fn default() -> Self {
20        Self {
21            enabled: true,
22            max_attempts: 3,
23            initial_delay: Duration::from_secs(1),
24        }
25    }
26}
27
28#[derive(Debug, Clone)]
29pub struct CacheConfig {
30    pub enabled: bool,
31    pub ttl: Duration,
32}
33
34impl Default for CacheConfig {
35    fn default() -> Self {
36        Self {
37            enabled: true,
38            ttl: Duration::from_secs(60),
39        }
40    }
41}
42
43#[derive(Clone)]
44pub struct AxonFlowConfig {
45    pub endpoint: String,
46    pub client_id: Option<String>,
47    pub client_secret: Option<String>,
48    pub license_key: Option<String>,
49    pub mode: Mode,
50    pub debug: bool,
51    pub timeout: Duration,
52    pub map_timeout: Duration,
53    pub retry: RetryConfig,
54    pub cache: CacheConfig,
55    pub insecure_skip_tls_verify: bool,
56}
57
58impl fmt::Debug for AxonFlowConfig {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        f.debug_struct("AxonFlowConfig")
61            .field("endpoint", &self.endpoint)
62            .field("client_id", &self.client_id)
63            .field(
64                "client_secret",
65                &self.client_secret.as_ref().map(|_| "[REDACTED]"),
66            )
67            .field(
68                "license_key",
69                &self.license_key.as_ref().map(|_| "[REDACTED]"),
70            )
71            .field("mode", &self.mode)
72            .field("debug", &self.debug)
73            .field("timeout", &self.timeout)
74            .field("map_timeout", &self.map_timeout)
75            .field("retry", &self.retry)
76            .field("cache", &self.cache)
77            .field("insecure_skip_tls_verify", &self.insecure_skip_tls_verify)
78            .finish()
79    }
80}
81
82impl Default for AxonFlowConfig {
83    fn default() -> Self {
84        Self {
85            endpoint: String::new(),
86            client_id: None,
87            client_secret: None,
88            license_key: None,
89            mode: Mode::default(),
90            debug: false,
91            timeout: Duration::from_secs(60),
92            map_timeout: Duration::from_secs(120),
93            retry: RetryConfig::default(),
94            cache: CacheConfig::default(),
95            insecure_skip_tls_verify: false,
96        }
97    }
98}
99
100impl AxonFlowConfig {
101    pub fn new(endpoint: impl Into<String>) -> Self {
102        Self {
103            endpoint: endpoint.into(),
104            ..Default::default()
105        }
106    }
107
108    /// Convenience constructor for local development. Defaults to
109    /// `http://localhost:8080` (the docker-compose agent default), sets
110    /// `mode = Mode::Sandbox`, and enables debug logging.
111    ///
112    /// Sandbox-mode clients fire anonymous telemetry tagged `stream="sandbox"`
113    /// — see `heartbeat::maybe_send_heartbeat`. Set `AXONFLOW_TELEMETRY=off`
114    /// to opt out (the SOLE opt-out lever; there is intentionally no
115    /// programmatic disable on the SDK config).
116    pub fn sandbox(client_id: impl Into<String>, client_secret: impl Into<String>) -> Self {
117        Self {
118            endpoint: "http://localhost:8080".to_string(),
119            client_id: Some(client_id.into()),
120            client_secret: Some(client_secret.into()),
121            mode: Mode::Sandbox,
122            debug: true,
123            ..Default::default()
124        }
125    }
126
127    pub fn with_auth(
128        mut self,
129        client_id: impl Into<String>,
130        client_secret: impl Into<String>,
131    ) -> Self {
132        self.client_id = Some(client_id.into());
133        self.client_secret = Some(client_secret.into());
134        self
135    }
136
137    pub fn with_license_key(mut self, license_key: impl Into<String>) -> Self {
138        self.license_key = Some(license_key.into());
139        self
140    }
141
142    pub fn with_mode(mut self, mode: Mode) -> Self {
143        self.mode = mode;
144        self
145    }
146
147    pub fn with_timeout(mut self, timeout: Duration) -> Self {
148        self.timeout = timeout;
149        self
150    }
151
152    pub fn with_map_timeout(mut self, timeout: Duration) -> Self {
153        self.map_timeout = timeout;
154        self
155    }
156
157    pub fn with_retry(mut self, retry: RetryConfig) -> Self {
158        self.retry = retry;
159        self
160    }
161
162    pub fn with_cache(mut self, cache: CacheConfig) -> Self {
163        self.cache = cache;
164        self
165    }
166}