diff --git a/ProviderTests/ProviderOptionsTests.cs b/ProviderTests/ProviderOptionsTests.cs new file mode 100644 index 0000000..dfdefca --- /dev/null +++ b/ProviderTests/ProviderOptionsTests.cs @@ -0,0 +1,21 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Splitio.OpenFeature.Provider; + +namespace ProviderTests +{ + [TestClass] + public class ProviderOptionsTests + { + [TestMethod] + public void ProviderOptionsConstructorThrowsWhenSdkKeyIsNull() + { + // arrange, act + assert + Assert.ThrowsException(() => + { + new ProviderOptions( + null); + }); + } + } +} \ No newline at end of file diff --git a/ProviderTests/ProviderTests.cs b/ProviderTests/ProviderTests.cs index c93c473..eff3b2e 100644 --- a/ProviderTests/ProviderTests.cs +++ b/ProviderTests/ProviderTests.cs @@ -45,9 +45,16 @@ public ProviderTests() [TestMethod] [ExpectedException(typeof(ArgumentException), "Missing SplitClient instance or SDK ApiKey")] - public async Task InitializeWithNullTest() + public async Task InitializeWithNullDictionaryTest() { - await Api.Instance.SetProviderAsync(new Provider(null)); + await Api.Instance.SetProviderAsync(new Provider(null as Dictionary)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException), "Missing SplitClient instance or SDK ApiKey")] + public async Task InitializeWithNullProviderOptionsTest() + { + await Api.Instance.SetProviderAsync(new Provider(null as ProviderOptions)); } [TestMethod] @@ -468,6 +475,42 @@ public async Task TestTrackWithInvalidArguments() catch (Exception) { } Assert.AreEqual(0, splitClient.Invocations.Count); } + + [TestMethod] + public async Task ProviderConstructorWithOptionsIncludesConfiguration() + { + // arrange + var configuration = new ConfigurationOptions(); + var options = new ProviderOptions( + "test-sdk-key", + configuration, + 1234); + + // act + await Api.Instance.SetProviderAsync(new Provider(options)); + var featureClient = Api.Instance.GetClient(); + + // assert + Assert.IsNotNull(featureClient); + } + + [TestMethod] + public async Task ProviderConstructorWithOptionsWithoutConfigurationUsesDefaults() + { + // arrange + var options = new ProviderOptions( + "test-sdk-key", + null, + 10000); + + // act + await Api.Instance.SetProviderAsync(new Provider(options)); + var featureClient = OpenFeature.Api.Instance.GetClient(); + + // assert + Assert.IsNotNull(featureClient); + } + private static bool StructuresMatch(Structure s1, Structure s2) { diff --git a/Splitio.OpenFeature.Provider/Constants.cs b/Splitio.OpenFeature.Provider/Constants.cs index 6671880..096edd0 100644 --- a/Splitio.OpenFeature.Provider/Constants.cs +++ b/Splitio.OpenFeature.Provider/Constants.cs @@ -9,6 +9,6 @@ internal static class Constants public const string ConfigKey = "ConfigOptions"; public const string TrafficType = "trafficType"; public const string ReadyBlockTime = "ReadyBlockTime"; - + public const int DefaultReadyBlockTime = 10000; } } diff --git a/Splitio.OpenFeature.Provider/Provider.cs b/Splitio.OpenFeature.Provider/Provider.cs index 2851b92..09f0704 100644 --- a/Splitio.OpenFeature.Provider/Provider.cs +++ b/Splitio.OpenFeature.Provider/Provider.cs @@ -37,6 +37,11 @@ public Provider(Dictionary initialContext) _splitWrapper = CreateSplitWrapper(initialContext); _log = WrapperAdapter.Instance().GetLogger(typeof(Provider)); } + + public Provider(ProviderOptions options) + : this(ToInitialContext(options)) + { + } public override Metadata GetMetadata() => _metadata; @@ -315,5 +320,23 @@ private bool ValidateTrackDetails(string trackingEventName, EvaluationContext ev return true; } + + private static Dictionary ToInitialContext(ProviderOptions options) + { + var initialContext = new Dictionary(); + + if (options != null) + { + initialContext[Constants.SdkApiKey] = options.SdkKey; + initialContext[Constants.ReadyBlockTime] = options.ReadyBlockTime; + } + + if (options?.Configuration != null) + { + initialContext[Constants.ConfigKey] = options.Configuration; + } + + return initialContext; + } } } \ No newline at end of file diff --git a/Splitio.OpenFeature.Provider/ProviderOptions.cs b/Splitio.OpenFeature.Provider/ProviderOptions.cs new file mode 100644 index 0000000..63ac640 --- /dev/null +++ b/Splitio.OpenFeature.Provider/ProviderOptions.cs @@ -0,0 +1,22 @@ +using System; +using Splitio.Services.Client.Classes; + +namespace Splitio.OpenFeature.Provider +{ + public sealed class ProviderOptions + { + public string SdkKey { get; } + public ConfigurationOptions Configuration { get; } + public int ReadyBlockTime { get; } + + public ProviderOptions( + string sdkKey, + ConfigurationOptions configOptions = null, + int readyBlockTime = Constants.DefaultReadyBlockTime) + { + SdkKey = sdkKey ?? throw new ArgumentNullException(nameof(sdkKey)); + Configuration = configOptions; + ReadyBlockTime = readyBlockTime; + } + } +} \ No newline at end of file diff --git a/Splitio.OpenFeature.Provider/README.md b/Splitio.OpenFeature.Provider/README.md index 36d9bd3..dbe33a9 100644 --- a/Splitio.OpenFeature.Provider/README.md +++ b/Splitio.OpenFeature.Provider/README.md @@ -14,18 +14,18 @@ Below is a simple example that describes the instantiation of the Split Provider using OpenFeature; using Splitio.OpenFeature; -Dictionary initialContext = new Dictionary(); -var config = new ConfigurationOptions -{ - Logger = new CustomLogger() -}; -initialContext.Add("ConfigOptions", config); -initialContext.Add("SdkKey", "SPLIT SDK API KEY"); -initialContext.Add("ReadyBlockTime", 5000); +var options = new ProviderOptions( + sdkKey: "SPLIT SDK API KEY", + configuration: new ConfigurationOptions + { + Logger = new CustomLogger() + }, + readyBlockTime: 5000 +); Api api = OpenFeature.Api.Instance; -api.setProviderAsync(new Provider(initialContext)); +api.setProviderAsync(new Provider(options)); ``` If you are more familiar with Split or want access to other initialization options, you can provide a `Split Client` to the constructor. See the [Split .NET Documentation](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK) for more information. diff --git a/Splitio.OpenFeature.Provider/SplitWrapper.cs b/Splitio.OpenFeature.Provider/SplitWrapper.cs index b0ce896..b7f5f11 100644 --- a/Splitio.OpenFeature.Provider/SplitWrapper.cs +++ b/Splitio.OpenFeature.Provider/SplitWrapper.cs @@ -16,7 +16,7 @@ public SplitWrapper(ISplitClient splitClient) this.splitClient = splitClient; } - public SplitWrapper(string SdkKey, ConfigurationOptions Configs, int ReadyBlockTime=10000) + public SplitWrapper(string SdkKey, ConfigurationOptions Configs, int ReadyBlockTime=Constants.DefaultReadyBlockTime) { var factory = new SplitFactory(SdkKey, Configs); _log = WrapperAdapter.Instance().GetLogger(typeof(SplitWrapper));