|
| 1 | +mod error; |
| 2 | +mod resolver; |
| 3 | + |
| 4 | +use error::OfrepError; |
| 5 | +use open_feature::provider::{FeatureProvider, ProviderMetadata, ResolutionDetails}; |
| 6 | +use open_feature::{EvaluationContext, EvaluationError, StructValue}; |
| 7 | +use reqwest::header::HeaderMap; |
| 8 | +use resolver::Resolver; |
| 9 | +use std::fmt; |
| 10 | +use std::sync::Arc; |
| 11 | +use std::time::Duration; |
| 12 | +use tracing::debug; |
| 13 | +use tracing::instrument; |
| 14 | +use url::Url; |
| 15 | + |
| 16 | +use async_trait::async_trait; |
| 17 | + |
| 18 | +const DEFAULT_BASE_URL: &str = "http://localhost:8016"; |
| 19 | +const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10); |
| 20 | + |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct OfrepOptions { |
| 23 | + pub base_url: String, |
| 24 | + pub headers: HeaderMap, |
| 25 | + pub connect_timeout: Duration, |
| 26 | +} |
| 27 | + |
| 28 | +impl Default for OfrepOptions { |
| 29 | + fn default() -> Self { |
| 30 | + OfrepOptions { |
| 31 | + base_url: DEFAULT_BASE_URL.to_string(), |
| 32 | + headers: HeaderMap::new(), |
| 33 | + connect_timeout: DEFAULT_CONNECT_TIMEOUT, |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +pub struct OfrepProvider { |
| 39 | + provider: Arc<dyn FeatureProvider + Send + Sync>, |
| 40 | +} |
| 41 | + |
| 42 | +impl fmt::Debug for OfrepProvider { |
| 43 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 44 | + f.debug_struct("OfrepProvider") |
| 45 | + .field("provider", &"<FeatureProvider>") |
| 46 | + .finish() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl OfrepProvider { |
| 51 | + #[instrument(skip(options))] |
| 52 | + pub async fn new(options: OfrepOptions) -> Result<Self, OfrepError> { |
| 53 | + debug!("Initializing OfrepProvider with options: {:?}", options); |
| 54 | + |
| 55 | + let url = Url::parse(&options.base_url).map_err(|e| { |
| 56 | + OfrepError::Config(format!("Invalid base url: '{}' ({})", options.base_url, e)) |
| 57 | + })?; |
| 58 | + |
| 59 | + if !matches!(url.scheme(), "http" | "https") { |
| 60 | + return Err(OfrepError::Config(format!( |
| 61 | + "Invalid base url: '{}' (unsupported scheme)", |
| 62 | + url.scheme() |
| 63 | + ))); |
| 64 | + } |
| 65 | + |
| 66 | + Ok(Self { |
| 67 | + provider: Arc::new(Resolver::new(&options)), |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[async_trait] |
| 73 | +impl FeatureProvider for OfrepProvider { |
| 74 | + fn metadata(&self) -> &ProviderMetadata { |
| 75 | + self.provider.metadata() |
| 76 | + } |
| 77 | + |
| 78 | + async fn resolve_bool_value( |
| 79 | + &self, |
| 80 | + flag_key: &str, |
| 81 | + context: &EvaluationContext, |
| 82 | + ) -> Result<ResolutionDetails<bool>, EvaluationError> { |
| 83 | + self.provider.resolve_bool_value(flag_key, context).await |
| 84 | + } |
| 85 | + |
| 86 | + async fn resolve_int_value( |
| 87 | + &self, |
| 88 | + flag_key: &str, |
| 89 | + context: &EvaluationContext, |
| 90 | + ) -> Result<ResolutionDetails<i64>, EvaluationError> { |
| 91 | + self.provider.resolve_int_value(flag_key, context).await |
| 92 | + } |
| 93 | + |
| 94 | + async fn resolve_float_value( |
| 95 | + &self, |
| 96 | + flag_key: &str, |
| 97 | + context: &EvaluationContext, |
| 98 | + ) -> Result<ResolutionDetails<f64>, EvaluationError> { |
| 99 | + self.provider.resolve_float_value(flag_key, context).await |
| 100 | + } |
| 101 | + |
| 102 | + async fn resolve_string_value( |
| 103 | + &self, |
| 104 | + flag_key: &str, |
| 105 | + context: &EvaluationContext, |
| 106 | + ) -> Result<ResolutionDetails<String>, EvaluationError> { |
| 107 | + self.provider.resolve_string_value(flag_key, context).await |
| 108 | + } |
| 109 | + |
| 110 | + async fn resolve_struct_value( |
| 111 | + &self, |
| 112 | + flag_key: &str, |
| 113 | + context: &EvaluationContext, |
| 114 | + ) -> Result<ResolutionDetails<StructValue>, EvaluationError> { |
| 115 | + self.provider.resolve_struct_value(flag_key, context).await |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[cfg(test)] |
| 120 | +mod tests { |
| 121 | + use super::*; |
| 122 | + use test_log::test; |
| 123 | + |
| 124 | + #[test(tokio::test)] |
| 125 | + async fn test_ofrep_options_validation() { |
| 126 | + let provider_with_empty_host = OfrepProvider::new(OfrepOptions { |
| 127 | + base_url: "http://".to_string(), |
| 128 | + ..Default::default() |
| 129 | + }) |
| 130 | + .await; |
| 131 | + |
| 132 | + let provider_with_invalid_scheme = OfrepProvider::new(OfrepOptions { |
| 133 | + base_url: "invalid://".to_string(), |
| 134 | + ..Default::default() |
| 135 | + }) |
| 136 | + .await; |
| 137 | + |
| 138 | + assert!(provider_with_empty_host.is_err()); |
| 139 | + assert!(provider_with_invalid_scheme.is_err()); |
| 140 | + |
| 141 | + assert_eq!( |
| 142 | + provider_with_empty_host.unwrap_err(), |
| 143 | + OfrepError::Config("Invalid base url: 'http://' (empty host)".to_string()) |
| 144 | + ); |
| 145 | + assert_eq!( |
| 146 | + provider_with_invalid_scheme.unwrap_err(), |
| 147 | + OfrepError::Config("Invalid base url: 'invalid' (unsupported scheme)".to_string()) |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments