-
Notifications
You must be signed in to change notification settings - Fork 10
feat: ofrep provider #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b104b8
feat: ofrep provider
Rahul-Baradol 38c4361
test: add UTCs for resolver
Rahul-Baradol 4b2f9a9
docs: added ofrep provider docs
Rahul-Baradol c7ca05f
test: more UTCs for resolver
Rahul-Baradol 0735f38
refactor: remove code duplication, move tokio to dev dependencies
Rahul-Baradol db2e2ba
fix: handle rate limiting
Rahul-Baradol 3c09b62
fix: base url validation
Rahul-Baradol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "open-feature-ofrep" | ||
version = "0.0.1" | ||
edition = "2024" | ||
|
||
[dev-dependencies] | ||
wiremock = "0.6.3" | ||
test-log = { version = "0.2", features = ["trace"] } | ||
|
||
[dependencies] | ||
async-trait = "0.1.88" | ||
open-feature = "0.2.5" | ||
reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls"] } | ||
serde = "1.0.219" | ||
serde_json = "1.0.140" | ||
tracing = "0.1.41" | ||
thiserror = "2.0" | ||
anyhow = "1.0.98" | ||
tokio = { version = "1.45", features = ["full"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
[Generated by cargo-readme: `cargo readme --no-title --no-license > README.md`]:: | ||
# OFREP Provider for OpenFeature | ||
|
||
A Rust implementation of the OpenFeature OFREP provider, enabling dynamic | ||
feature flag evaluation in your applications. | ||
|
||
This provider allows to connect to any feature flag management system that supports OFREP. | ||
|
||
### Installation | ||
Add the dependency in your `Cargo.toml`: | ||
```bash | ||
cargo add open-feature-ofrep | ||
cargo add open-feature | ||
``` | ||
Then integrate it into your application: | ||
|
||
```rust | ||
use std::time::Duration; | ||
use open_feature::provider::FeatureProvider; | ||
use open_feature::EvaluationContext; | ||
use open_feature_ofrep::{OfrepProvider, OfrepOptions}; | ||
use reqwest::header::{HeaderMap, HeaderValue}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert("color", HeaderValue::from_static("yellow")); | ||
|
||
let provider = OfrepProvider::new(OfrepOptions { | ||
base_url: "http://localhost:8016".to_string(), | ||
headers: headers.clone(), | ||
connect_timeout: Duration::from_secs(4), | ||
..Default::default() | ||
}).await.unwrap(); | ||
|
||
let context = EvaluationContext::default() | ||
.with_targeting_key("user-123") | ||
.with_custom_field("color", "yellow"); | ||
|
||
let result = provider.resolve_bool_value("isColorYellow", &context).await.unwrap(); | ||
println!("Flag value: {}", result.value); | ||
} | ||
``` | ||
|
||
### Configuration Options | ||
Configurations can be provided as constructor options. The following options are supported: | ||
|
||
| Option | Type / Supported Value | Default | | ||
|-----------------------------------------|-----------------------------------|-------------------------------------| | ||
| base_url | string | http://localhost:8016 | | ||
| headers | HeaderMap | Empty Map | | ||
| connect_timeout | Duration | 10 seconds | | ||
|
||
### License | ||
Apache 2.0 - See [LICENSE](./../../LICENSE) for more information. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum OfrepError { | ||
#[error("Provider error: {0}")] | ||
Provider(String), | ||
#[error("Connection error: {0}")] | ||
Connection(String), | ||
#[error("Invalid configuration: {0}")] | ||
Config(String), | ||
} | ||
|
||
// Add implementations for error conversion | ||
impl From<Box<dyn std::error::Error>> for OfrepError { | ||
fn from(error: Box<dyn std::error::Error>) -> Self { | ||
OfrepError::Provider(error.to_string()) | ||
} | ||
} | ||
|
||
impl From<Box<dyn std::error::Error + Send + Sync>> for OfrepError { | ||
fn from(error: Box<dyn std::error::Error + Send + Sync>) -> Self { | ||
OfrepError::Provider(error.to_string()) | ||
} | ||
} | ||
|
||
impl From<anyhow::Error> for OfrepError { | ||
fn from(error: anyhow::Error) -> Self { | ||
OfrepError::Provider(error.to_string()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
mod error; | ||
mod resolver; | ||
|
||
use error::OfrepError; | ||
use open_feature::provider::{FeatureProvider, ProviderMetadata, ResolutionDetails}; | ||
use open_feature::{EvaluationContext, EvaluationError, StructValue}; | ||
use reqwest::header::HeaderMap; | ||
use resolver::Resolver; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
use tracing::debug; | ||
use tracing::instrument; | ||
|
||
use async_trait::async_trait; | ||
|
||
const DEFAULT_BASE_URL: &str = "http://localhost:8016"; | ||
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10); | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct OfrepOptions { | ||
pub base_url: String, | ||
pub headers: HeaderMap, | ||
pub connect_timeout: Duration, | ||
} | ||
|
||
impl Default for OfrepOptions { | ||
fn default() -> Self { | ||
OfrepOptions { | ||
base_url: DEFAULT_BASE_URL.to_string(), | ||
headers: HeaderMap::new(), | ||
connect_timeout: DEFAULT_CONNECT_TIMEOUT, | ||
} | ||
} | ||
} | ||
|
||
pub struct OfrepProvider { | ||
provider: Arc<dyn FeatureProvider + Send + Sync>, | ||
} | ||
|
||
impl OfrepProvider { | ||
#[instrument(skip(options))] | ||
pub async fn new(options: OfrepOptions) -> Result<Self, OfrepError> { | ||
debug!("Initializing OfrepProvider with options: {:?}", options); | ||
Ok(Self { | ||
provider: Arc::new(Resolver::new(&options)), | ||
}) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl FeatureProvider for OfrepProvider { | ||
fn metadata(&self) -> &ProviderMetadata { | ||
self.provider.metadata() | ||
} | ||
|
||
async fn resolve_bool_value( | ||
&self, | ||
flag_key: &str, | ||
context: &EvaluationContext, | ||
) -> Result<ResolutionDetails<bool>, EvaluationError> { | ||
let result = self.provider.resolve_bool_value(flag_key, context).await?; | ||
Ok(result) | ||
} | ||
|
||
async fn resolve_int_value( | ||
&self, | ||
flag_key: &str, | ||
context: &EvaluationContext, | ||
) -> Result<ResolutionDetails<i64>, EvaluationError> { | ||
let result = self.provider.resolve_int_value(flag_key, context).await?; | ||
Ok(result) | ||
} | ||
|
||
async fn resolve_float_value( | ||
&self, | ||
flag_key: &str, | ||
context: &EvaluationContext, | ||
) -> Result<ResolutionDetails<f64>, EvaluationError> { | ||
let result = self.provider.resolve_float_value(flag_key, context).await?; | ||
Ok(result) | ||
} | ||
|
||
async fn resolve_string_value( | ||
&self, | ||
flag_key: &str, | ||
context: &EvaluationContext, | ||
) -> Result<ResolutionDetails<String>, EvaluationError> { | ||
let result = self | ||
.provider | ||
.resolve_string_value(flag_key, context) | ||
.await?; | ||
Ok(result) | ||
} | ||
|
||
async fn resolve_struct_value( | ||
&self, | ||
flag_key: &str, | ||
context: &EvaluationContext, | ||
) -> Result<ResolutionDetails<StructValue>, EvaluationError> { | ||
let result = self | ||
.provider | ||
.resolve_struct_value(flag_key, context) | ||
.await?; | ||
Ok(result) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.