Skip to content
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

feat: Adding OpenFeature provider for Schematic #314

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions DotnetSdkContrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenFeature.Contrib.Provide
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenFeature.Contrib.Providers.Flipt.Test", "test\OpenFeature.Contrib.Providers.Flipt.Test\OpenFeature.Contrib.Providers.Flipt.Test.csproj", "{B446D481-B5A3-4509-8933-C4CF6DA9B147}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenFeature.Contrib.Providers.Schematic", "src\OpenFeature.Contrib.Providers.Schematic\OpenFeature.Contrib.Providers.Schematic.csproj", "{CF1AB517-1D51-455F-80C0-56B4856E6A6B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenFeature.Contrib.Providers.Schematic.Test", "test\OpenFeature.Contrib.Providers.Schematic.Test\OpenFeature.Contrib.Providers.Schematic.Test.csproj", "{08BD26A8-0C14-40F1-BFAF-7D413B76EF6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -127,6 +131,14 @@ Global
{B446D481-B5A3-4509-8933-C4CF6DA9B147}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B446D481-B5A3-4509-8933-C4CF6DA9B147}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B446D481-B5A3-4509-8933-C4CF6DA9B147}.Release|Any CPU.Build.0 = Release|Any CPU
{CF1AB517-1D51-455F-80C0-56B4856E6A6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF1AB517-1D51-455F-80C0-56B4856E6A6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF1AB517-1D51-455F-80C0-56B4856E6A6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF1AB517-1D51-455F-80C0-56B4856E6A6B}.Release|Any CPU.Build.0 = Release|Any CPU
{08BD26A8-0C14-40F1-BFAF-7D413B76EF6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08BD26A8-0C14-40F1-BFAF-7D413B76EF6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08BD26A8-0C14-40F1-BFAF-7D413B76EF6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08BD26A8-0C14-40F1-BFAF-7D413B76EF6B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -151,5 +163,7 @@ Global
{F3080350-B0AB-4D59-B416-50CC38C99087} = {B6D3230B-5E4D-4FF1-868E-2F4E325C84FE}
{5ECF7DBF-FE64-40A2-BF39-239DE173DA4B} = {0E563821-BD08-4B7F-BF9D-395CAD80F026}
{B446D481-B5A3-4509-8933-C4CF6DA9B147} = {B6D3230B-5E4D-4FF1-868E-2F4E325C84FE}
{CF1AB517-1D51-455F-80C0-56B4856E6A6B} = {0E563821-BD08-4B7F-BF9D-395CAD80F026}
{08BD26A8-0C14-40F1-BFAF-7D413B76EF6B} = {B6D3230B-5E4D-4FF1-868E-2F4E325C84FE}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@
"extra-files": [
"OpenFeature.Contrib.Providers.Flipt.csproj"
]
},
"src/OpenFeature.Contrib.Providers.Schematic": {
"package-name": "OpenFeature.Contrib.Providers.Schematic",
"release-type": "simple",
"bump-minor-pre-major": true,
"bump-patch-for-minor-pre-major": true,
"versioning": "default",
"extra-files": [
"OpenFeature.Contrib.Providers.Schematic.csproj"
]
}
},
"changelog-sections": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>OpenFeature.Contrib.Providers.Schematic</PackageId>
<VersionNumber>0.1.0</VersionNumber> <!--x-release-please-version -->
<VersionPrefix>$(VersionNumber)</VersionPrefix>
<AssemblyVersion>$(VersionNumber)</AssemblyVersion>
<FileVersion>$(VersionNumber)</FileVersion>
<Description>Schematic provider for .NET</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>Benjamin Papillon</Authors>
</PropertyGroup>
<ItemGroup>
<!-- make the internal methods visble to our test project -->
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(MSBuildProjectName).Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<PackageReference Include="SchematicHQ.Client" Version="1.0.6" />
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
</ItemGroup>

</Project>

110 changes: 110 additions & 0 deletions src/OpenFeature.Contrib.Providers.Schematic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Schematic .NET Provider

The Schematic provider allows you to connect to your Schematic instance through the OpenFeature SDK

# .Net SDK usage

## Requirements

- open-feature/dotnet-sdk v1.5.0 > v2.0.0

## Install dependencies

The first things we will do is install the **Open Feature SDK** and the **Schematic OpenFeature provider**.

### .NET Cli
```shell
dotnet add package OpenFeature.Contrib.Providers.Schematic
```
### Package Manager

```shell
NuGet\Install-Package OpenFeature.Contrib.Providers.Schematic
```
### Package Reference

```xml
<PackageReference Include="OpenFeature.Contrib.Providers.Schematic" />
```
### Paket cli

```shell
paket add OpenFeature.Contrib.Providers.Schematic
```

### Cake

```shell
// Install OpenFeature.Contrib.Providers.Schematic as a Cake Addin
#addin nuget:?package=OpenFeature.Contrib.Providers.Schematic

// Install OpenFeature.Contrib.Providers.Schematic as a Cake Tool
#tool nuget:?package=OpenFeature.Contrib.Providers.Schematic
```

## Using the Schematic Provider with the OpenFeature SDK

To use Schematic as an OpenFeature provider, define your provider and Schematic settings.

```csharp
using OpenFeature;
using OpenFeature.Contrib.Providers.Schematic;
using System;

var schematicProvider = new SchematicFeatureProvider("your-api-key");

// Set the schematicProvider as the provider for the OpenFeature SDK
await OpenFeature.Api.Instance.SetProviderAsync(flagsmithProvider);

// Get an OpenFeature client
var client = OpenFeature.Api.Instance.GetClient("my-app");

// Set company and/or user context
var context = EvaluationContext.Builder()
.Set("company", new Dictionary<string, string> { { "id", "your-company-id" } })
.Set("user", new Dictionary<string, string> { { "id", "your-user-id" } })
.Build();

// Evaluate a flag
var val = await client.GetBooleanValueAsync("your-flag-key", false, context);

// Print the value of the 'your-flag-key' feature flag
Console.WriteLine(val);
```

You can also provide additional configuration options to the provider to manage caching behavior, offline mode, and other capabilities:

```csharp
using OpenFeature;
using OpenFeature.Contrib.Providers.Schematic;
using System;

var options = new ClientOptions
{
Offline = true,
FlagDefaults = new Dictionary<string, bool>
{
{ "some-flag-key", true }
}
};

var schematicProvider = new SchematicFeatureProvider("your-api-key", options);

// Set the schematicProvider as the provider for the OpenFeature SDK
await OpenFeature.Api.Instance.SetProviderAsync(flagsmithProvider);

// Get an OpenFeature client
var client = OpenFeature.Api.Instance.GetClient("my-app");

// Set company and/or user context
var context = EvaluationContext.Builder()
.Set("company", new Dictionary<string, string> { { "id", "your-company-id" } })
.Set("user", new Dictionary<string, string> { { "id", "your-user-id" } })
.Build();

// Evaluate a flag
var val = await client.GetBooleanValueAsync("your-flag-key", false, context);

// Print the value of the 'your-flag-key' feature flag
Console.WriteLine(val);
```
106 changes: 106 additions & 0 deletions src/OpenFeature.Contrib.Providers.Schematic/SchematicProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using OpenFeature;
using OpenFeature.Model;
using SchematicHQ.Client;

namespace OpenFeature.Contrib.Providers.Schematic
{
public class SchematicFeatureProvider : FeatureProvider
{
private readonly Schematic _schematic;
private readonly ISchematicLogger _logger;

public SchematicFeatureProvider(string apiKey, ClientOptions? options = null)
{
options ??= new ClientOptions();
_logger = options.Logger ?? new ConsoleLogger();
_schematic = new Schematic(apiKey, options);
}

public override Metadata GetMetadata() => new Metadata("schematic-provider");

public override async Task<ResolutionDetails<bool>> ResolveBooleanValueAsync(
string flagKey,
bool defaultValue,
EvaluationContext? context = null,
CancellationToken cancellationToken = default)
{
_logger.Debug("evaluating boolean flag: {0}", flagKey);
var company = context?.GetValue("company") as Dictionary<string, string>;
var user = context?.GetValue("user") as Dictionary<string, string>;

try
{
bool value = await _schematic.CheckFlag(flagKey, company, user);
_logger.Debug("evaluated flag: {0} => {1}", flagKey, value);
return new ResolutionDetails<bool>(value, value ? "on" : "off", "schematic evaluation");
}
catch (Exception ex)
{
_logger.Error("error evaluating flag {0}: {1}. using default {2}", flagKey, ex.Message, defaultValue);
return new ResolutionDetails<bool>(defaultValue, defaultValue ? "on" : "off", "error", "provider_error");
}
}

public override Task<ResolutionDetails<string>> ResolveStringValueAsync(
string flagKey,
string defaultValue,
EvaluationContext? context = null,
CancellationToken cancellationToken = default)
{
return Task.FromResult(new ResolutionDetails<string>(defaultValue, defaultValue, "unsupported type"));
}

public override Task<ResolutionDetails<int>> ResolveIntegerValueAsync(
string flagKey,
int defaultValue,
EvaluationContext? context = null,
CancellationToken cancellationToken = default)
{
return Task.FromResult(new ResolutionDetails<int>(defaultValue, defaultValue.ToString(), "unsupported type"));
}

public override Task<ResolutionDetails<double>> ResolveDoubleValueAsync(
string flagKey,
double defaultValue,
EvaluationContext? context = null,
CancellationToken cancellationToken = default)
{
return Task.FromResult(new ResolutionDetails<double>(defaultValue, defaultValue.ToString(), "unsupported type"));
}

public override Task<ResolutionDetails<object>> ResolveStructureValueAsync(
string flagKey,
object defaultValue,
EvaluationContext? context = null,
CancellationToken cancellationToken = default)
{
return Task.FromResult(new ResolutionDetails<object>(defaultValue, defaultValue?.ToString() ?? string.Empty, "unsupported type"));
}

public override Task InitializeAsync(ProviderConfiguration? configuration, CancellationToken cancellationToken = default)
{
_logger.Debug("initializing schematic provider");
return Task.CompletedTask;
}

public async Task ShutdownAsync(CancellationToken cancellationToken = default)
{
_logger.Debug("shutting down schematic provider");
await _schematic.Shutdown();
}

public Task TrackEventAsync(string eventName, EvaluationContext? context = null, CancellationToken cancellationToken = default)
{
_logger.Debug("tracking event: {0}", eventName);
var company = context?.GetValue("company") as Dictionary<string, string>;
var user = context?.GetValue("user") as Dictionary<string, string>;
var traits = context?.GetValue("traits") as Dictionary<string, object>;
_schematic.Track(eventName, company, user, traits);
return Task.CompletedTask;
}
}
}
1 change: 1 addition & 0 deletions src/OpenFeature.Contrib.Providers.Schematic/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\OpenFeature.Contrib.Providers.Schematic\OpenFeature.Contrib.Providers.Schematic.csproj" />
</ItemGroup>

</Project>
Loading