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            name: name.to_string(),
110            version: "1.0.0".to_string(),
111            commands: vec![],
112            base_url: base_url.map(|s| s.to_string()),
113            servers: base_url.map(|s| vec![s.to_string()]).unwrap_or_default(),
114            security_schemes: HashMap::new(),
115        }
116    }
117
118    /// Test harness to isolate environment variable changes with mutual exclusion
119    fn test_with_env_isolation<F>(test_fn: F)
120    where
121        F: FnOnce() + std::panic::UnwindSafe,
122    {
123        // Acquire mutex to prevent parallel env var access
124        let _guard = ENV_TEST_MUTEX.lock().unwrap();
125
126        // Store original value
127        let original_value = std::env::var("APERTURE_BASE_URL").ok();
128
129        // Clean up first
130        std::env::remove_var("APERTURE_BASE_URL");
131
132        // Run the test with panic protection
133        let result = std::panic::catch_unwind(test_fn);
134
135        // Always restore original state, even if test panicked
136        if let Some(original) = original_value {
137            std::env::set_var("APERTURE_BASE_URL", original);
138        } else {
139            std::env::remove_var("APERTURE_BASE_URL");
140        }
141
142        // Drop the guard before re-panicking to release the mutex
143        drop(_guard);
144
145        // Re-panic if the test failed
146        if let Err(panic_info) = result {
147            std::panic::resume_unwind(panic_info);
148        }
149    }
150
151    #[test]
152    fn test_priority_1_explicit_url() {
153        test_with_env_isolation(|| {
154            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
155            let resolver = BaseUrlResolver::new(&spec);
156
157            assert_eq!(
158                resolver.resolve(Some("https://explicit.example.com")),
159                "https://explicit.example.com"
160            );
161        });
162    }
163
164    #[test]
165    fn test_priority_2_api_config_override() {
166        test_with_env_isolation(|| {
167            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
168
169            let mut api_configs = HashMap::new();
170            api_configs.insert(
171                "test-api".to_string(),
172                ApiConfig {
173                    base_url_override: Some("https://config.example.com".to_string()),
174                    environment_urls: HashMap::new(),
175                },
176            );
177
178            let global_config = GlobalConfig {
179                api_configs,
180                ..Default::default()
181            };
182
183            let resolver = BaseUrlResolver::new(&spec).with_global_config(&global_config);
184
185            assert_eq!(resolver.resolve(None), "https://config.example.com");
186        });
187    }
188
189    #[test]
190    fn test_priority_2_environment_specific() {
191        test_with_env_isolation(|| {
192            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
193
194            let mut environment_urls = HashMap::new();
195            environment_urls.insert(
196                "staging".to_string(),
197                "https://staging.example.com".to_string(),
198            );
199            environment_urls.insert("prod".to_string(), "https://prod.example.com".to_string());
200
201            let mut api_configs = HashMap::new();
202            api_configs.insert(
203                "test-api".to_string(),
204                ApiConfig {
205                    base_url_override: Some("https://config.example.com".to_string()),
206                    environment_urls,
207                },
208            );
209
210            let global_config = GlobalConfig {
211                api_configs,
212                ..Default::default()
213            };
214
215            let resolver = BaseUrlResolver::new(&spec)
216                .with_global_config(&global_config)
217                .with_environment(Some("staging".to_string()));
218
219            assert_eq!(resolver.resolve(None), "https://staging.example.com");
220        });
221    }
222
223    #[test]
224    fn test_priority_config_override_beats_env_var() {
225        // Test that config override takes precedence over environment variable
226        test_with_env_isolation(|| {
227            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
228
229            // Set env var
230            std::env::set_var("APERTURE_BASE_URL", "https://env.example.com");
231
232            let mut api_configs = HashMap::new();
233            api_configs.insert(
234                "test-api".to_string(),
235                ApiConfig {
236                    base_url_override: Some("https://config.example.com".to_string()),
237                    environment_urls: HashMap::new(),
238                },
239            );
240
241            let global_config = GlobalConfig {
242                api_configs,
243                ..Default::default()
244            };
245
246            let resolver = BaseUrlResolver::new(&spec).with_global_config(&global_config);
247
248            // Config override should win over env var
249            assert_eq!(resolver.resolve(None), "https://config.example.com");
250        });
251    }
252
253    #[test]
254    fn test_priority_3_env_var() {
255        // Use a custom test harness to isolate environment variables
256        test_with_env_isolation(|| {
257            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
258
259            // Set env var
260            std::env::set_var("APERTURE_BASE_URL", "https://env.example.com");
261
262            let resolver = BaseUrlResolver::new(&spec);
263
264            assert_eq!(resolver.resolve(None), "https://env.example.com");
265        });
266    }
267
268    #[test]
269    fn test_priority_4_spec_default() {
270        test_with_env_isolation(|| {
271            let spec = create_test_spec("test-api", Some("https://spec.example.com"));
272            let resolver = BaseUrlResolver::new(&spec);
273
274            assert_eq!(resolver.resolve(None), "https://spec.example.com");
275        });
276    }
277
278    #[test]
279    fn test_priority_5_fallback() {
280        test_with_env_isolation(|| {
281            let spec = create_test_spec("test-api", None);
282            let resolver = BaseUrlResolver::new(&spec);
283
284            assert_eq!(resolver.resolve(None), "https://api.example.com");
285        });
286    }
287}