Skip to main content

tycho_simulation/rfq/
constants.rs

1use std::env;
2
3use crate::rfq::errors::RFQError;
4
5pub const DEFAULT_METRIC_API_URL: &str = "http://54.199.103.16:8080";
6
7/// Hashflow authentication configuration
8pub struct HashflowAuth {
9    pub user: String,
10    pub key: String,
11}
12
13/// Bebop authentication configuration
14pub struct BebopAuth {
15    pub user: String,
16    pub key: String,
17}
18
19/// Metric API configuration
20pub struct MetricConfig {
21    pub base_url: String,
22    pub secret_key: Option<String>,
23}
24
25/// Read Hashflow authentication from environment variables
26/// Returns the HASHFLOW_USER and HASHFLOW_KEY environment variables
27pub fn get_hashflow_auth() -> Result<HashflowAuth, RFQError> {
28    let user = env::var("HASHFLOW_USER").map_err(|_| {
29        RFQError::InvalidInput("HASHFLOW_USER environment variable is required".into())
30    })?;
31
32    let key = env::var("HASHFLOW_KEY").map_err(|_| {
33        RFQError::InvalidInput("HASHFLOW_KEY environment variable is required".into())
34    })?;
35
36    Ok(HashflowAuth { user, key })
37}
38
39/// Liquorice authentication configuration
40pub struct LiquoriceAuth {
41    pub solver: String,
42    pub key: String,
43}
44
45/// Read Liquorice authentication from environment variables
46/// Returns the LIQUORICE_USER and LIQUORICE_KEY environment variables
47pub fn get_liquorice_auth() -> Result<LiquoriceAuth, RFQError> {
48    let solver = env::var("LIQUORICE_USER").map_err(|_| {
49        RFQError::InvalidInput("LIQUORICE_USER environment variable is required".into())
50    })?;
51
52    let key = env::var("LIQUORICE_KEY").map_err(|_| {
53        RFQError::InvalidInput("LIQUORICE_KEY environment variable is required".into())
54    })?;
55
56    Ok(LiquoriceAuth { solver, key })
57}
58
59/// Read Bebop authentication from environment variables
60/// Returns the BEBOP_USER and BEBOP_KEY environment variables
61pub fn get_bebop_auth() -> Result<BebopAuth, RFQError> {
62    let user = env::var("BEBOP_USER").map_err(|_| {
63        RFQError::InvalidInput("BEBOP_USER environment variable is required".into())
64    })?;
65
66    let key = env::var("BEBOP_KEY")
67        .map_err(|_| RFQError::InvalidInput("BEBOP_KEY environment variable is required".into()))?;
68
69    Ok(BebopAuth { user, key })
70}
71
72/// Read Metric API configuration from environment variables.
73/// METRIC_API_URL defaults to the public Metric endpoint; METRIC_SECRET_KEY is optional.
74pub fn get_metric_config() -> MetricConfig {
75    let base_url = env::var("METRIC_API_URL")
76        .ok()
77        .filter(|url| !url.trim().is_empty())
78        .unwrap_or_else(|| DEFAULT_METRIC_API_URL.to_string());
79    let secret_key = env::var("METRIC_SECRET_KEY")
80        .ok()
81        .filter(|key| !key.trim().is_empty());
82
83    MetricConfig { base_url, secret_key }
84}
85
86#[cfg(test)]
87mod tests {
88    use std::env;
89
90    use super::*;
91
92    #[test]
93    fn test_hashflow_auth_success() {
94        env::set_var("HASHFLOW_USER", "test_user");
95        env::set_var("HASHFLOW_KEY", "test_key");
96
97        let auth = get_hashflow_auth().unwrap();
98        assert_eq!(auth.user, "test_user");
99        assert_eq!(auth.key, "test_key");
100
101        env::remove_var("HASHFLOW_USER");
102        env::remove_var("HASHFLOW_KEY");
103    }
104
105    #[test]
106    fn test_hashflow_auth_missing_user() {
107        env::remove_var("HASHFLOW_USER");
108        env::set_var("HASHFLOW_KEY", "test_key");
109
110        let result = get_hashflow_auth();
111        assert!(result.is_err());
112
113        env::remove_var("HASHFLOW_KEY");
114    }
115
116    #[test]
117    fn test_hashflow_auth_missing_key() {
118        env::set_var("HASHFLOW_USER", "test_user");
119        env::remove_var("HASHFLOW_KEY");
120
121        let result = get_hashflow_auth();
122        assert!(result.is_err());
123
124        env::remove_var("HASHFLOW_USER");
125    }
126
127    #[test]
128    fn test_bebop_auth_success() {
129        env::set_var("BEBOP_USER", "test_user");
130        env::set_var("BEBOP_KEY", "test_key");
131
132        let auth = get_bebop_auth().unwrap();
133        assert_eq!(auth.user, "test_user");
134        assert_eq!(auth.key, "test_key");
135
136        env::remove_var("BEBOP_USER");
137        env::remove_var("BEBOP_KEY");
138    }
139
140    #[test]
141    fn test_bebop_auth_missing_user() {
142        env::remove_var("BEBOP_USER");
143        env::set_var("BEBOP_KEY", "test_key");
144
145        let result = get_bebop_auth();
146        assert!(result.is_err());
147
148        env::remove_var("BEBOP_KEY");
149    }
150
151    #[test]
152    fn test_bebop_auth_missing_key() {
153        env::set_var("BEBOP_USER", "test_user");
154        env::remove_var("BEBOP_KEY");
155
156        let result = get_bebop_auth();
157        assert!(result.is_err());
158
159        env::remove_var("BEBOP_USER");
160    }
161
162    #[test]
163    fn test_metric_config_defaults_and_reads_env() {
164        env::remove_var("METRIC_API_URL");
165        env::remove_var("METRIC_SECRET_KEY");
166
167        let config = get_metric_config();
168        assert_eq!(config.base_url, DEFAULT_METRIC_API_URL);
169        assert_eq!(config.secret_key, None);
170
171        env::set_var("METRIC_API_URL", "https://metric.example");
172        env::set_var("METRIC_SECRET_KEY", "secret");
173
174        let config = get_metric_config();
175        assert_eq!(config.base_url, "https://metric.example");
176        assert_eq!(config.secret_key.as_deref(), Some("secret"));
177
178        env::remove_var("METRIC_API_URL");
179        env::remove_var("METRIC_SECRET_KEY");
180    }
181}