aperture_cli/config/
url_resolver.rs

1use crate::cache::models::CachedSpec;
2use crate::config::models::{ApiConfig, GlobalConfig};
3
4/// Resolves the base URL for an API based on a priority hierarchy
5pub struct BaseUrlResolver<'a> {
6    /// The cached API specification
7    spec: &'a CachedSpec,
8    /// Global configuration containing API overrides
9    global_config: Option<&'a GlobalConfig>,
10    /// Optional environment override (if None, reads from `APERTURE_ENV` at resolve time)
11    environment_override: Option<String>,
12}
13
14impl<'a> BaseUrlResolver<'a> {
15    /// Creates a new URL resolver for the given spec
16    #[must_use]
17    pub const fn new(spec: &'a CachedSpec) -> Self {
18        Self {
19            spec,
20            global_config: None,
21            environment_override: None,
22        }
23    }
24
25    /// Sets the global configuration for API-specific overrides
26    #[must_use]
27    #[allow(clippy::missing_const_for_fn)]
28    pub fn with_global_config(mut self, config: &'a GlobalConfig) -> Self {
29        self.global_config = Some(config);
30        self
31    }
32
33    /// Sets the environment explicitly (overrides `APERTURE_ENV`)
34    #[must_use]
35    pub fn with_environment(mut self, env: Option<String>) -> Self {
36        self.environment_override = env;
37        self
38    }
39
40    /// Resolves the base URL according to the priority hierarchy:
41    /// 1. Explicit parameter (for testing)
42    /// 2. Per-API config override with environment support
43    /// 3. Environment variable: `APERTURE_BASE_URL`
44    /// 4. Cached spec default
45    /// 5. Fallback: <https://api.example.com>
46    #[must_use]
47    pub fn resolve(&self, explicit_url: Option<&str>) -> String {
48        // Priority 1: Explicit parameter (for testing)
49        if let Some(url) = explicit_url {
50            return url.to_string();
51        }
52
53        // Priority 2: Per-API config override
54        if let Some(config) = self.global_config {
55            if let Some(api_config) = config.api_configs.get(&self.spec.name) {
56                // Check environment-specific URL first
57                let env_to_check = self.environment_override.as_ref().map_or_else(
58                    || std::env::var("APERTURE_ENV").unwrap_or_default(),
59                    std::clone::Clone::clone,
60                );
61
62                if !env_to_check.is_empty() {
63                    if let Some(env_url) = api_config.environment_urls.get(&env_to_check) {
64                        return env_url.clone();
65                    }
66                }
67
68                // Then check general override
69                if let Some(override_url) = &api_config.base_url_override {
70                    return override_url.clone();
71                }
72            }
73        }
74
75        // Priority 3: Environment variable
76        if let Ok(url) = std::env::var("APERTURE_BASE_URL") {
77            return url;
78        }
79
80        // Priority 4: Cached spec default
81        if let Some(base_url) = &self.spec.base_url {
82            return base_url.clone();
83        }
84
85        // Priority 5: Fallback
86        "https://api.example.com".to_string()
87    }
88
89    /// Gets the API config if available
90    #[must_use]
91    pub fn get_api_config(&self) -> Option<&ApiConfig> {
92        self.global_config
93            .and_then(|config| config.api_configs.get(&self.spec.name))
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100    use crate::cache::models::CachedSpec;
101    use std::collections::HashMap;
102    use std::sync::Mutex;
103
104    // Static mutex to ensure only one test can modify environment variables at a time
105    static ENV_TEST_MUTEX: Mutex<()> = Mutex::new(());
106
107    fn create_test_spec(name: &str, base_url: Option<&str>) -> CachedSpec {
108        CachedSpec {
109            cache_format_version: crate::cache::models::CACHE_FORMAT_VERSION,
110            name: name.to_string(),
111            version: "1.0.0".to_string(),
112            commands: vec![],
113            base_url: base_url.map(|s| s.to_string()),
114            servers: base_url.map(|s| vec![s.to_string()]).unwrap_or_default(),
115            security_schemes: HashMap::new(),
116        }
117    }
118
119    /// Test harness to isolate environment variable changes with mutual exclusion
120    fn test_with_env_isolation<F>(test_fn: F)
121    where
122        F: FnOnce() + std::panic::UnwindSafe,
123    {
124        // Acquire mutex to prevent parallel env var access
125        let _guard = ENV_TEST_MUTEX.lock().unwrap();
126
127        // Store original value
128        let original_value = std::env::var("APERTURE_BASE_URL").ok();
129
130        // Clean up first
131        std::env::remove_var("APERTURE_BASE_URL");
132
133        // Run the test with panic protection
134        let result = std::panic::catch_unwind(test_fn);
135
136        // Always restore original state, even if test panicked
137        if let Some(original) = original_value {
138            std::env::set_var("APERTURE_BASE_URL", original);
139        } else {
140            std::env::remove_var("APERTURE_BASE_URL");
141        }
142
143        // Drop the guard before re-panicking to release the mutex
144        drop(_guard);
145
146        // Re-panic if the test failed
147        if let Err(panic_info) = result {
148            std::panic::resume_unwind(panic_info);
149        }
150    }
151
152    #[test]
153    fn test_priority_1_explicit_url() {
154        test_with_env_isolation(|| {
155            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
156            let resolver = BaseUrlResolver::new(&spec);
157
158            assert_eq!(
159                resolver.resolve(Some("https://explicit.example.com")),
160                "https://explicit.example.com"
161            );
162        });
163    }
164
165    #[test]
166    fn test_priority_2_api_config_override() {
167        test_with_env_isolation(|| {
168            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
169
170            let mut api_configs = HashMap::new();
171            api_configs.insert(
172                "test-api".to_string(),
173                ApiConfig {
174                    base_url_override: Some("https://config.example.com".to_string()),
175                    environment_urls: HashMap::new(),
176                },
177            );
178
179            let global_config = GlobalConfig {
180                api_configs,
181                ..Default::default()
182            };
183
184            let resolver = BaseUrlResolver::new(&spec).with_global_config(&global_config);
185
186            assert_eq!(resolver.resolve(None), "https://config.example.com");
187        });
188    }
189
190    #[test]
191    fn test_priority_2_environment_specific() {
192        test_with_env_isolation(|| {
193            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
194
195            let mut environment_urls = HashMap::new();
196            environment_urls.insert(
197                "staging".to_string(),
198                "https://staging.example.com".to_string(),
199            );
200            environment_urls.insert("prod".to_string(), "https://prod.example.com".to_string());
201
202            let mut api_configs = HashMap::new();
203            api_configs.insert(
204                "test-api".to_string(),
205                ApiConfig {
206                    base_url_override: Some("https://config.example.com".to_string()),
207                    environment_urls,
208                },
209            );
210
211            let global_config = GlobalConfig {
212                api_configs,
213                ..Default::default()
214            };
215
216            let resolver = BaseUrlResolver::new(&spec)
217                .with_global_config(&global_config)
218                .with_environment(Some("staging".to_string()));
219
220            assert_eq!(resolver.resolve(None), "https://staging.example.com");
221        });
222    }
223
224    #[test]
225    fn test_priority_config_override_beats_env_var() {
226        // Test that config override takes precedence over environment variable
227        test_with_env_isolation(|| {
228            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
229
230            // Set env var
231            std::env::set_var("APERTURE_BASE_URL", "https://env.example.com");
232
233            let mut api_configs = HashMap::new();
234            api_configs.insert(
235                "test-api".to_string(),
236                ApiConfig {
237                    base_url_override: Some("https://config.example.com".to_string()),
238                    environment_urls: HashMap::new(),
239                },
240            );
241
242            let global_config = GlobalConfig {
243                api_configs,
244                ..Default::default()
245            };
246
247            let resolver = BaseUrlResolver::new(&spec).with_global_config(&global_config);
248
249            // Config override should win over env var
250            assert_eq!(resolver.resolve(None), "https://config.example.com");
251        });
252    }
253
254    #[test]
255    fn test_priority_3_env_var() {
256        // Use a custom test harness to isolate environment variables
257        test_with_env_isolation(|| {
258            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
259
260            // Set env var
261            std::env::set_var("APERTURE_BASE_URL", "https://env.example.com");
262
263            let resolver = BaseUrlResolver::new(&spec);
264
265            assert_eq!(resolver.resolve(None), "https://env.example.com");
266        });
267    }
268
269    #[test]
270    fn test_priority_4_spec_default() {
271        test_with_env_isolation(|| {
272            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
273            let resolver = BaseUrlResolver::new(&spec);
274
275            assert_eq!(resolver.resolve(None), "https://spec.example.com");
276        });
277    }
278
279    #[test]
280    fn test_priority_5_fallback() {
281        test_with_env_isolation(|| {
282            let spec = create_test_spec("test-api", None);
283            let resolver = BaseUrlResolver::new(&spec);
284
285            assert_eq!(resolver.resolve(None), "https://api.example.com");
286        });
287    }
288}