Skip to content

Commit

Permalink
fix: use the TargetingKey property in the Flagsmith provider (#227)
Browse files Browse the repository at this point in the history
Signed-off-by: ghelyar <3225358+ghelyar@users.noreply.github.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent 55efa79 commit 5e175c8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 54 deletions.
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;

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

0 comments on commit 5e175c8

Please sign in to comment.