Skip to main content

kalshi_pmx/
config.rs

1//! Runtime configuration for REST and WebSocket clients.
2
3use std::time::Duration;
4
5/// Kalshi API environment.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Environment {
8    /// Kalshi demo environment.
9    Demo,
10    /// Kalshi production environment.
11    Production,
12    /// A custom REST and WebSocket endpoint pair.
13    ///
14    /// Use this for tests, proxies, or any future Kalshi-compatible endpoint that keeps the same
15    /// Trade API v2 path shape.
16    Custom {
17        /// REST base URL, usually ending in `/trade-api/v2`.
18        rest_base_url: String,
19        /// WebSocket URL, usually ending in `/trade-api/ws/v2`.
20        ws_url: String,
21    },
22}
23
24impl Environment {
25    /// Returns the REST base URL for this environment.
26    pub fn rest_base_url(&self) -> &str {
27        match self {
28            Self::Demo => "https://external-api.demo.kalshi.co/trade-api/v2",
29            Self::Production => "https://external-api.kalshi.com/trade-api/v2",
30            Self::Custom { rest_base_url, .. } => rest_base_url,
31        }
32    }
33
34    /// Returns the authenticated WebSocket URL for this environment.
35    pub fn ws_url(&self) -> &str {
36        match self {
37            Self::Demo => "wss://external-api-ws.demo.kalshi.co/trade-api/ws/v2",
38            Self::Production => "wss://external-api-ws.kalshi.com/trade-api/ws/v2",
39            Self::Custom { ws_url, .. } => ws_url,
40        }
41    }
42}
43
44/// Runtime configuration for REST and WebSocket clients.
45#[derive(Debug, Clone)]
46pub struct KalshiConfig {
47    /// API environment.
48    pub environment: Environment,
49    /// HTTP user-agent header.
50    pub user_agent: String,
51    /// HTTP request timeout and WebSocket connect timeout.
52    pub timeout: Duration,
53    /// Number of REST retries after `429 Too Many Requests`.
54    pub max_retries: usize,
55    /// Path used when signing WebSocket authentication headers.
56    pub ws_sign_path: String,
57}
58
59impl Default for KalshiConfig {
60    fn default() -> Self {
61        Self {
62            environment: Environment::Demo,
63            user_agent: format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
64            timeout: Duration::from_secs(30),
65            max_retries: 2,
66            ws_sign_path: "/trade-api/ws/v2".to_owned(),
67        }
68    }
69}
70
71impl KalshiConfig {
72    /// Returns default configuration for the demo environment.
73    pub fn demo() -> Self {
74        Self {
75            environment: Environment::Demo,
76            ..Self::default()
77        }
78    }
79
80    /// Returns default configuration for the production environment.
81    pub fn production() -> Self {
82        Self {
83            environment: Environment::Production,
84            ..Self::default()
85        }
86    }
87
88    /// Returns the normalized REST base URL without a trailing slash.
89    pub fn rest_base_url(&self) -> String {
90        self.environment
91            .rest_base_url()
92            .trim_end_matches('/')
93            .to_owned()
94    }
95
96    /// Returns the configured WebSocket URL.
97    pub fn ws_url(&self) -> String {
98        self.environment.ws_url().to_owned()
99    }
100}