OpenFeature.Providers.Ofrep 0.1.6
OpenFeature.Providers.Ofrep
An OpenFeature provider implementation for the OpenFeature REST Evaluation Protocol (OFREP). This provider allows you to evaluate feature flags through a REST API that follows the OFREP specification.
Features
- Full implementation of the OpenFeature REST Evaluation Protocol
- Support for all standard flag types (boolean, string, integer, double, object)
Installation
Add the package to your project:
.NET Cli
dotnet add package OpenFeature.Providers.Ofrep
Basic Usage
using OpenFeature;
using OpenFeature.Providers.Ofrep;
using OpenFeature.Providers.Ofrep.Configuration;
// Configure the provider
var config = new OfrepOptions("https://zconfig.company.com/");
// Create and register the provider
var provider = new OfrepProvider(config);
await Api.Instance.SetProviderAsync(provider);
// Use feature flags
var client = Api.Instance.GetClient();
// Boolean flag
var boolFlag = await client.GetBooleanValueAsync("my-flag", false);
// String flag
var stringFlag = await client.GetStringValueAsync("greeting", "Hello");
// Integer flag
var intFlag = await client.GetIntegerValueAsync("max-retries", 3);
// Double flag
var doubleFlag = await client.GetDoubleValueAsync("sample-rate", 0.1);
Advanced Configuration
// Advanced configuration
var config = new OfrepOptions("https://feature-flags.example.com")
{
Timeout = TimeSpan.FromSeconds(10),
Headers = new Dictionary<string, string>
{
["Custom-Header"] = "value",
["Api-Version"] = "v1"
}
};
// Create and register the provider
var provider = new OfrepProvider(config);
await Api.Instance.SetProviderAsync(provider);
TimeProvider Support
For testing and scenarios where you need to control time-related behavior (such as rate limiting), you can provide a custom TimeProvider:
// Using a custom TimeProvider for testing
var customTimeProvider = new FakeTimeProvider(); // From Microsoft.Extensions.TimeProvider.Testing
var provider = new OfrepProvider(config, customTimeProvider);
When using dependency injection, the provider will automatically use any registered TimeProvider from the service container.
Configuration Options
The OfrepOptions class supports the following options:
BaseUrl(required): The base URL for the OFREP API endpointTimeout(optional): HTTP client timeout. The default value is 10 seconds.Headers(optional): Additional HTTP headers to include in requests
Environment Variable Configuration
The OFREP provider supports configuration via environment variables, enabling zero-code configuration for containerized deployments and CI/CD pipelines.
Supported Environment Variables
| Variable | Required | Description | Example |
|---|---|---|---|
OFREP_ENDPOINT |
Yes | The OFREP server endpoint URL | http://localhost:8080 |
OFREP_HEADERS |
No | HTTP headers in Key=Value,Key2=Value2 format |
Authorization=Bearer token,X-Api-Key=abc123 |
OFREP_TIMEOUT_MS |
No | Request timeout in milliseconds (default: 10000) | 5000 |
Usage with Environment Variables
using OpenFeature;
using OpenFeature.Providers.Ofrep;
// Create provider using environment variables (no explicit configuration needed)
var provider = new OfrepProvider();
await Api.Instance.SetProviderAsync(provider);
Or explicitly load from environment:
using OpenFeature.Providers.Ofrep.Configuration;
// Explicitly create options from environment variables
var options = OfrepOptions.FromEnvironment();
var provider = new OfrepProvider(options);
Header Format and URL Encoding
The OFREP_HEADERS environment variable uses a simple Key=Value format separated by commas.
Important parsing rules:
- Commas are always treated as header separators and cannot be included in values, even when URL-encoded (e.g.,
%2Cwill be decoded to,and treated as a separator). - Equals signs (
=) in the key are not supported. The first=separates the key from the value. Additional=characters in the value are allowed without encoding. - URL encoding (e.g.,
%3Dfor=) is decoded before parsing, which is useful for systems that encode the entire value, but does not change the comma/equals parsing behavior.
Examples:
# Simple headers
export OFREP_HEADERS="Authorization=Bearer token123,Content-Type=application/json"
# Header value containing equals sign (e.g., base64) - no encoding needed for additional = in value
export OFREP_HEADERS="Authorization=Bearer abc123=="
# URL-encoded equals sign in the value (decoded to =, then treated as part of the value)
export OFREP_HEADERS="X-Data=key%3Dvalue"
# Multiple headers
export OFREP_HEADERS="Authorization=Bearer token,X-Api-Key=abc123"
Dependency Injection with IConfiguration
When using dependency injection, the provider can read configuration from IConfiguration, which supports environment variables via AddEnvironmentVariables():
// In Program.cs or Startup.cs
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddOpenFeature(featureBuilder =>
{
featureBuilder.AddOfrepProvider(options =>
{
// Options will fall back to OFREP_ENDPOINT, OFREP_HEADERS, OFREP_TIMEOUT_MS
// from IConfiguration (which includes environment variables)
});
});
Configuration Precedence
When multiple configuration sources are available, the following precedence order applies (highest to lowest):
- Programmatic configuration (explicit
OfrepOptionsorOfrepProviderOptionsvalues) - IConfiguration (when using DI, includes appsettings.json, user secrets, etc.)
- Environment variables (direct
Environment.GetEnvironmentVariablefallback)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
No packages depend on OpenFeature.Providers.Ofrep.
.NET 10.0
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- OpenFeature.Hosting (>= 2.9.0 && <= 2.99999.0)
.NET Framework 4.6.2
- Microsoft.Bcl.TimeProvider (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- OpenFeature.Hosting (>= 2.9.0 && <= 2.99999.0)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 8.0.5)
.NET 8.0
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- OpenFeature.Hosting (>= 2.9.0 && <= 2.99999.0)
.NET 9.0
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- OpenFeature.Hosting (>= 2.9.0 && <= 2.99999.0)
.NET Standard 2.0
- Microsoft.Bcl.TimeProvider (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- OpenFeature.Hosting (>= 2.9.0 && <= 2.99999.0)
- System.Text.Json (>= 8.0.5)