|
| 1 | +using OpenFeature.Constant; |
| 2 | +using OpenFeature.Error; |
| 3 | +using OpenFeature.Model; |
| 4 | +using Statsig; |
| 5 | +using Statsig.Server; |
| 6 | +using System; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace OpenFeature.Contrib.Providers.Statsig |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// An OpenFeature <see cref="FeatureProvider"/> which enables the use of the Statsig Server-Side SDK for .NET |
| 13 | + /// with OpenFeature. |
| 14 | + /// </summary> |
| 15 | + /// <example> |
| 16 | + /// var provider = new StatsigProvider("my-sdk-key"), new StatsigProviderOptions(){LocalMode = false}); |
| 17 | + /// |
| 18 | + /// OpenFeature.Api.Instance.SetProvider(provider); |
| 19 | + /// |
| 20 | + /// var client = OpenFeature.Api.Instance.GetClient(); |
| 21 | + /// </example> |
| 22 | + public sealed class StatsigProvider : FeatureProvider |
| 23 | + { |
| 24 | + volatile bool initialized = false; |
| 25 | + private readonly Metadata _providerMetadata = new Metadata("Statsig provider"); |
| 26 | + private readonly string _sdkKey = "secret-"; //Dummy sdk key that works with local mode |
| 27 | + private readonly StatsigServerOptions _options; |
| 28 | + internal readonly ServerDriver ServerDriver; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Creates new instance of <see cref="StatsigProvider"/> |
| 32 | + /// </summary> |
| 33 | + /// <param name="sdkKey">SDK Key to access Statsig.</param> |
| 34 | + /// <param name="configurationAction">The action used to configure the client.</param> |
| 35 | + public StatsigProvider(string sdkKey = null, Action<StatsigServerOptions> configurationAction = null) |
| 36 | + { |
| 37 | + if (sdkKey != null) |
| 38 | + { |
| 39 | + _sdkKey = sdkKey; |
| 40 | + } |
| 41 | + _options = new StatsigServerOptions(); |
| 42 | + configurationAction?.Invoke(_options); |
| 43 | + ServerDriver = new ServerDriver(_sdkKey, _options); |
| 44 | + } |
| 45 | + |
| 46 | + /// <inheritdoc/> |
| 47 | + public override Metadata GetMetadata() => _providerMetadata; |
| 48 | + |
| 49 | + /// <inheritdoc/> |
| 50 | + public override Task<ResolutionDetails<bool>> ResolveBooleanValue(string flagKey, bool defaultValue, EvaluationContext context = null) |
| 51 | + { |
| 52 | + //TODO: defaultvalue = true not yet supported due to https://github.com/statsig-io/dotnet-sdk/issues/33 |
| 53 | + if (defaultValue == true) |
| 54 | + throw new FeatureProviderException(ErrorType.General, "defaultvalue = true not supported (https://github.com/statsig-io/dotnet-sdk/issues/33)"); |
| 55 | + if (GetStatus() != ProviderStatus.Ready) |
| 56 | + return Task.FromResult(new ResolutionDetails<bool>(flagKey, defaultValue, ErrorType.ProviderNotReady)); |
| 57 | + var result = ServerDriver.CheckGateSync(context.AsStatsigUser(), flagKey); |
| 58 | + return Task.FromResult(new ResolutionDetails<bool>(flagKey, result)); |
| 59 | + } |
| 60 | + |
| 61 | + /// <inheritdoc/> |
| 62 | + public override Task<ResolutionDetails<double>> ResolveDoubleValue(string flagKey, double defaultValue, EvaluationContext context = null) |
| 63 | + { |
| 64 | + throw new NotImplementedException(); |
| 65 | + } |
| 66 | + |
| 67 | + /// <inheritdoc/> |
| 68 | + public override Task<ResolutionDetails<int>> ResolveIntegerValue(string flagKey, int defaultValue, EvaluationContext context = null) |
| 69 | + { |
| 70 | + throw new NotImplementedException(); |
| 71 | + } |
| 72 | + |
| 73 | + /// <inheritdoc/> |
| 74 | + public override Task<ResolutionDetails<string>> ResolveStringValue(string flagKey, string defaultValue, EvaluationContext context = null) |
| 75 | + { |
| 76 | + throw new NotImplementedException(); |
| 77 | + } |
| 78 | + |
| 79 | + /// <inheritdoc/> |
| 80 | + public override Task<ResolutionDetails<Value>> ResolveStructureValue(string flagKey, Value defaultValue, EvaluationContext context = null) |
| 81 | + { |
| 82 | + throw new NotImplementedException(); |
| 83 | + } |
| 84 | + |
| 85 | + /// <inheritdoc/> |
| 86 | + public override ProviderStatus GetStatus() |
| 87 | + { |
| 88 | + return initialized ? ProviderStatus.Ready : ProviderStatus.NotReady; |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc/> |
| 92 | + public override async Task Initialize(EvaluationContext context) |
| 93 | + { |
| 94 | + var initResult = await ServerDriver.Initialize(); |
| 95 | + if (initResult == InitializeResult.Success || initResult == InitializeResult.LocalMode || initResult == InitializeResult.AlreadyInitialized) |
| 96 | + { |
| 97 | + initialized = true; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// <inheritdoc/> |
| 102 | + public override Task Shutdown() |
| 103 | + { |
| 104 | + return ServerDriver.Shutdown(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments