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

fix: use the TargetingKey property in the Flagsmith provider #227

Merged
merged 6 commits into from
Jul 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,12 @@ public FlagsmithProvider(IFlagsmithProviderConfiguration providerOptions, IFlags

private Task<IFlags> GetFlags(EvaluationContext ctx)
{
string key = null;
if (ctx != null && ctx.TryGetValue(Configuration.TargetingKey, out var value))
{
key = value?.AsString;
}
var key = ctx?.TargetingKey;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: open-feature/dotnet-sdk#235

Though I think everything should be backwards compatible when we make this change.


return string.IsNullOrEmpty(key)
? _flagsmithClient.GetEnvironmentFlags()
: _flagsmithClient.GetIdentityFlags(key, ctx
.AsDictionary()
.Where(x => x.Key != Configuration.TargetingKey)
.Select(x => new Trait(x.Key, x.Value.AsObject) as ITrait)
.ToList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
/// </summary>
public class FlagsmithProviderConfiguration : IFlagsmithProviderConfiguration
{
/// <summary>
/// Default value for targeting key
/// </summary>
public const string DefaultTargetingKey = "targetingKey";

/// <summary>
/// Key that will be used as identity for Flagsmith requests. Default: "targetingKey"
/// </summary>
public string TargetingKey { get; set; } = DefaultTargetingKey;

/// <inheritdoc/>
public bool UsingBooleanConfigValue { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ namespace OpenFeature.Contrib.Providers.Flagsmith;
/// </summary>
public interface IFlagsmithProviderConfiguration
{
/// <summary>
/// Key that will be used as identity for Flagsmith requests.
/// </summary>
public string TargetingKey { get; }

/// <summary>
/// Determines whether to resolve a feature value as a boolean or use
/// the isFeatureEnabled as the flag itself. These values will be false
Expand Down
66 changes: 34 additions & 32 deletions src/OpenFeature.Contrib.Providers.Flagsmith/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,42 @@ packet add OpenFeature.Contrib.Providers.Flagsmith
To create a Flagmith provider you should define provider and Flagsmith settings.

```csharp
using OpenFeature.Contrib.Providers.Flagd;
using Flagsmith;
using OpenFeature.Contrib.Providers.Flagsmith;
using OpenFeature.Model;

namespace OpenFeatureTestApp
// Additional configs for provider
var providerConfig = new FlagsmithProviderConfiguration();

// Flagsmith client configuration
var flagsmithConfig = new FlagsmithConfiguration
{
class Hello {
static void Main(string[] args) {

// Additional configs for provider
var providerConfig = new FlagsmithProviderConfiguration();

//Flagsmith client configuration
var flagsmithConfig = new FlagsmithConfiguration
{
ApiUrl = "https://edge.api.flagsmith.com/api/v1/",
EnvironmentKey = string.Empty,
EnableClientSideEvaluation = false,
EnvironmentRefreshIntervalSeconds = 60,
EnableAnalytics = false,
Retries = 1
};
var flagsmithProvider = new FlagsmithProvider(providerConfig, flagsmithConfig);\

// Set the flagsmithProvider as the provider for the OpenFeature SDK
OpenFeature.Api.Instance.SetProvider(flagsmithProvider);

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

var val = client.GetBooleanValue("myBoolFlag", false, null);

// Print the value of the 'myBoolFlag' feature flag
System.Console.WriteLine(val.Result.ToString());
}
}
}
ApiUrl = "https://edge.api.flagsmith.com/api/v1/",
EnvironmentKey = "",
EnableClientSideEvaluation = false,
EnvironmentRefreshIntervalSeconds = 60,
EnableAnalytics = false,
Retries = 1,
};
var flagsmithProvider = new FlagsmithProvider(providerConfig, flagsmithConfig);

// Set the flagsmithProvider 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");

// Optional: set a targeting key and traits to use segment and/or identity overrides
var context = EvaluationContext.Builder()
.SetTargetingKey("my-flagsmith-identity-ID")
.Set("my-trait-key", "my-trait-value")
.Build();

// Evaluate a flag
var val = await client.GetBooleanValue("myBoolFlag", false, context);

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

You also can create Flagsmith provider using ```HttpClient``` or precreated ```FlagsmithClient```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task GetValue_ForEnabledFeatureWithEvaluationContext_ReturnCorrectV
.Set("key4", date)
.Set("key5", Structure.Empty)
.Set("key6", 1.0)
.Set(FlagsmithProviderConfiguration.DefaultTargetingKey, "233");
.SetTargetingKey("233");
// Act
var result = await flagsmithProvider.ResolveBooleanValue("example-feature", false, contextBuilder.Build());

Expand Down