1use std::time::Duration;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Environment {
8 Demo,
10 Production,
12 Custom {
17 rest_base_url: String,
19 ws_url: String,
21 },
22}
23
24impl Environment {
25 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 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#[derive(Debug, Clone)]
46pub struct KalshiConfig {
47 pub environment: Environment,
49 pub user_agent: String,
51 pub timeout: Duration,
53 pub max_retries: usize,
55 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 pub fn demo() -> Self {
74 Self {
75 environment: Environment::Demo,
76 ..Self::default()
77 }
78 }
79
80 pub fn production() -> Self {
82 Self {
83 environment: Environment::Production,
84 ..Self::default()
85 }
86 }
87
88 pub fn rest_base_url(&self) -> String {
90 self.environment
91 .rest_base_url()
92 .trim_end_matches('/')
93 .to_owned()
94 }
95
96 pub fn ws_url(&self) -> String {
98 self.environment.ws_url().to_owned()
99 }
100}