diff options
| -rw-r--r-- | src/IOL.VippsEcommerce.Client/Program.cs | 9 | ||||
| -rw-r--r-- | src/IOL.VippsEcommerce.Tests/ConfigurationTests.cs | 34 | ||||
| -rw-r--r-- | src/IOL.VippsEcommerce/IOL.VippsEcommerce.csproj | 4 | ||||
| -rw-r--r-- | src/IOL.VippsEcommerce/Models/VippsConfiguration.cs | 68 | ||||
| -rw-r--r-- | src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs | 25 | ||||
| -rw-r--r-- | src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs | 16 | ||||
| -rw-r--r-- | src/IOL.VippsEcommerce/VippsEcommerceService.cs | 24 |
7 files changed, 25 insertions, 155 deletions
diff --git a/src/IOL.VippsEcommerce.Client/Program.cs b/src/IOL.VippsEcommerce.Client/Program.cs index 7488da2..9062956 100644 --- a/src/IOL.VippsEcommerce.Client/Program.cs +++ b/src/IOL.VippsEcommerce.Client/Program.cs @@ -1,12 +1,15 @@ using System; using System.Text.Json; using IOL.VippsEcommerce; -using IOL.VippsEcommerce.Models; using Microsoft.Extensions.DependencyInjection; var services = new ServiceCollection(); services.AddVippsEcommerceService(o => { - o.ConfigurationMode = VippsConfigurationMode.ONLY_ENVIRONMENT; + o.ClientSecret = "asdf"; + o.ClientId = "asdf"; + o.ApiUrl = "sadf"; + o.PrimarySubscriptionKey = ""; + o.Verify(); }); var provider = services.BuildServiceProvider(); var vippsEcommerceService = provider.GetService<IVippsEcommerceService>(); @@ -14,4 +17,4 @@ if (vippsEcommerceService == default) { return; } -Console.WriteLine(JsonSerializer.Serialize(vippsEcommerceService.Configuration));
\ No newline at end of file +Console.WriteLine(JsonSerializer.Serialize(vippsEcommerceService.Configuration)); diff --git a/src/IOL.VippsEcommerce.Tests/ConfigurationTests.cs b/src/IOL.VippsEcommerce.Tests/ConfigurationTests.cs index 41c0342..0890043 100644 --- a/src/IOL.VippsEcommerce.Tests/ConfigurationTests.cs +++ b/src/IOL.VippsEcommerce.Tests/ConfigurationTests.cs @@ -1,17 +1,9 @@ -using IOL.VippsEcommerce.Models; using Xunit; -using Xunit.Abstractions; namespace IOL.VippsEcommerce.Tests { public class InitialisationTests { - private readonly ITestOutputHelper _helper; - - public InitialisationTests(ITestOutputHelper helper) { - _helper = helper; - } - [Fact] public void Succeed_On_Valid_Minimal_Configuration() { var vippsEcommerceService = Helpers.GetVippsEcommerceService(o => { @@ -23,31 +15,5 @@ namespace IOL.VippsEcommerce.Tests vippsEcommerceService.Configuration.Verify(); } - - [Fact] - public void Configuration_Follows_Through_Initialisation() { - var vippsEcommerceService = Helpers.GetVippsEcommerceService(o => { - o.ApiUrl = "https://validuri.no"; - o.ClientId = "asdf"; - o.ClientSecret = "asdf"; - o.SecondarySubscriptionKey = "asdf"; - o.PrimarySubscriptionKey = "asdf"; - o.SystemName = "asdf"; - o.SystemVersion = "asdf"; - o.SystemPluginName = "asdf"; - o.SystemPluginVersion = "asdf"; - o.MerchantSerialNumber = "asdf"; - o.CacheDirectoryPath = "asdf"; - o.CacheEncryptionKey = "asdf"; - o.ConfigurationMode = VippsConfigurationMode.ONLY_OBJECT; - }); - - foreach (var prop in typeof(VippsConfiguration).GetProperties()) { - var value = prop.GetValue(vippsEcommerceService.Configuration, null); - _helper.WriteLine(prop.Name); - _helper.WriteLine(value?.ToString() ?? "EMPTY"); - Assert.False(value == default); - } - } } } diff --git a/src/IOL.VippsEcommerce/IOL.VippsEcommerce.csproj b/src/IOL.VippsEcommerce/IOL.VippsEcommerce.csproj index 1097744..ad21f7e 100644 --- a/src/IOL.VippsEcommerce/IOL.VippsEcommerce.csproj +++ b/src/IOL.VippsEcommerce/IOL.VippsEcommerce.csproj @@ -11,8 +11,8 @@ <RepositoryType>git</RepositoryType> <PackageTags>dotnet;vipps-api</PackageTags> <TargetFramework>net5.0</TargetFramework> - <PackageVersion>1.1.0</PackageVersion> - <PackageReleaseNotes>Update package info.</PackageReleaseNotes> + <PackageVersion>1.2.0</PackageVersion> + <PackageReleaseNotes>Use Microsoft.Extensions.Configuration to read environment variables</PackageReleaseNotes> </PropertyGroup> <ItemGroup> diff --git a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs index c9d9b1e..fb597dd 100644 --- a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs +++ b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs @@ -99,89 +99,31 @@ namespace IOL.VippsEcommerce.Models public string CacheEncryptionKey { get; set; } /// <summary> - /// Specify how to retrieve configuration and/or in what order. Defaults to VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT. - /// </summary> - public VippsConfigurationMode ConfigurationMode { get; set; } = VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT; - - /// <summary> - /// Get value from configuration, either from Dependency injection or from the environment. - /// </summary> - /// <param name="key">Configuration key.</param> - /// <returns>A string containing the configuration value.</returns> - public string GetValue(string key) { - switch (ConfigurationMode) { - case VippsConfigurationMode.ONLY_OBJECT: - return GetValueFromObject(key); - case VippsConfigurationMode.ONLY_ENVIRONMENT: - return GetValueFromEnvironment(key); - case VippsConfigurationMode.OBJECT_THEN_ENVIRONMENT: - return GetValueFromObject(key) ?? GetValueFromEnvironment(key); - case VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT: - return GetValueFromEnvironment(key) ?? GetValueFromObject(key); - default: - return default; - } - } - - /// <summary> /// Ensure that the configuration can be used to issue requests to the vipps api. /// <exception cref="ArgumentNullException">Throws if a required value is null or whitespace.</exception> /// </summary> public void Verify() { - if (GetValue(VippsConfigurationKeyNames.API_URL).IsNullOrWhiteSpace()) { + if (ApiUrl.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(ApiUrl), "VippsEcommerceService: ApiUrl is not provided in configuration."); } - if (GetValue(VippsConfigurationKeyNames.CLIENT_ID).IsNullOrWhiteSpace()) { + if (ClientId.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(ClientId), "VippsEcommerceService: ClientId is not provided in configuration."); } - if (GetValue(VippsConfigurationKeyNames.CLIENT_SECRET).IsNullOrWhiteSpace()) { + if (ClientSecret.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(ClientSecret), "VippsEcommerceService: ClientSecret is not provided in configuration."); } - if (GetValue(VippsConfigurationKeyNames.SUBSCRIPTION_KEY_PRIMARY).IsNullOrWhiteSpace() - && GetValue(VippsConfigurationKeyNames.SUBSCRIPTION_KEY_SECONDARY).IsNullOrWhiteSpace()) { + if (PrimarySubscriptionKey.IsNullOrWhiteSpace() + && SecondarySubscriptionKey.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(PrimarySubscriptionKey) + nameof(SecondarySubscriptionKey), "VippsEcommerceService: Neither PrimarySubscriptionKey nor SecondarySubscriptionKey was provided in configuration."); } } - - private string GetValueFromEnvironment(string key) { - var config = new ConfigurationBuilder().AddEnvironmentVariables().Build(); -#if DEBUG - var value = config[key]; - Console.WriteLine("Getting VippsConfiguration value for " + key + " from environment."); - Console.WriteLine("Key: " + key + " Value: " + value); - return value; -#else - return config[key]; -#endif - } - - private string GetValueFromObject(string key) { - foreach (var prop in typeof(VippsConfiguration).GetProperties()) { - foreach (var attribute in prop.CustomAttributes) { - foreach (var argument in attribute.ConstructorArguments) { - if (argument.Value as string == key) { -#if DEBUG - var value = prop.GetValue(this, null)?.ToString(); - Console.WriteLine("Getting VippsConfiguration value for " + key + " from object."); - Console.WriteLine("Key: " + key + " Value: " + value); - return value; -#else - return prop.GetValue(this, null)?.ToString(); -#endif - } - } - } - } - - return default; - } } } diff --git a/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs b/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs deleted file mode 100644 index edff72f..0000000 --- a/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace IOL.VippsEcommerce.Models -{ - public enum VippsConfigurationMode - { - /// <summary> - /// Only check for values in the configuration object. - /// </summary> - ONLY_OBJECT = 0, - - /// <summary> - /// Check for values in environment, then in the configuration object. - /// </summary> - ENVIRONMENT_THEN_OBJECT = 1, - - /// <summary> - /// Check for values in the configuration object, then in environment. - /// </summary> - OBJECT_THEN_ENVIRONMENT = 2, - - /// <summary> - /// Only check for values in environment. - /// </summary> - ONLY_ENVIRONMENT = 3, - } -}
\ No newline at end of file diff --git a/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs b/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs index 57f1833..e6a4fcd 100644 --- a/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs +++ b/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs @@ -29,21 +29,5 @@ namespace IOL.VippsEcommerce services.AddScoped<IVippsEcommerceService, VippsEcommerceService>(); return services; } - - /// <summary> - /// Adds the VippsEcommerceService to your DI, and expects configuration values from environment variables. - /// </summary> - /// <param name="services">Servicecollection to add VippsEcommerceService to.</param> - /// <returns></returns> - public static IServiceCollection AddVippsEcommerceService(this IServiceCollection services) { - if (services == null) { - throw new ArgumentNullException(nameof(services)); - } - - services.Configure(new Action<VippsConfiguration>(o => o.ConfigurationMode = VippsConfigurationMode.ONLY_ENVIRONMENT)); - services.AddHttpClient<IVippsEcommerceService, VippsEcommerceService>(); - services.AddScoped<IVippsEcommerceService, VippsEcommerceService>(); - return services; - } } } diff --git a/src/IOL.VippsEcommerce/VippsEcommerceService.cs b/src/IOL.VippsEcommerce/VippsEcommerceService.cs index 91229ef..84bbf79 100644 --- a/src/IOL.VippsEcommerce/VippsEcommerceService.cs +++ b/src/IOL.VippsEcommerce/VippsEcommerceService.cs @@ -42,44 +42,44 @@ namespace IOL.VippsEcommerce ) { Configuration = options.Value; Configuration.Verify(); - var vippsApiUrl = Configuration.GetValue(VippsConfigurationKeyNames.API_URL); + var vippsApiUrl = Configuration.ApiUrl; client.BaseAddress = new Uri(vippsApiUrl); _client = client; _logger = logger; - _vippsClientId = Configuration.GetValue(VippsConfigurationKeyNames.CLIENT_ID); - _vippsClientSecret = Configuration.GetValue(VippsConfigurationKeyNames.CLIENT_SECRET); + _vippsClientId = Configuration.ClientId; + _vippsClientSecret = Configuration.ClientSecret; client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", - Configuration.GetValue(VippsConfigurationKeyNames.SUBSCRIPTION_KEY_PRIMARY) - ?? Configuration.GetValue(VippsConfigurationKeyNames.SUBSCRIPTION_KEY_SECONDARY)); + Configuration.PrimarySubscriptionKey + ?? Configuration.SecondarySubscriptionKey); - var msn = Configuration.GetValue(VippsConfigurationKeyNames.MSN); + var msn = Configuration.MerchantSerialNumber; if (msn.IsPresent()) { client.DefaultRequestHeaders.Add("Merchant-Serial-Number", msn); _vippsMsn = msn; } - var systemName = Configuration.GetValue(VippsConfigurationKeyNames.SYSTEM_NAME); + var systemName = Configuration.SystemName; if (systemName.IsPresent()) { client.DefaultRequestHeaders.Add("Vipps-System-Name", systemName); } - var systemVersion = Configuration.GetValue(VippsConfigurationKeyNames.SYSTEM_VERSION); + var systemVersion = Configuration.SystemVersion; if (systemVersion.IsPresent()) { client.DefaultRequestHeaders.Add("Vipps-System-Version", systemVersion); } - var systemPluginName = Configuration.GetValue(VippsConfigurationKeyNames.SYSTEM_PLUGIN_NAME); + var systemPluginName = Configuration.SystemPluginName; if (systemPluginName.IsPresent()) { client.DefaultRequestHeaders.Add("Vipps-System-Plugin-Name", systemPluginName); } - var systemPluginVersion = Configuration.GetValue(VippsConfigurationKeyNames.SYSTEM_PLUGIN_VERSION); + var systemPluginVersion = Configuration.SystemPluginVersion; if (systemPluginVersion.IsPresent()) { client.DefaultRequestHeaders.Add("Vipps-System-Plugin-Version", systemPluginVersion); } - _cacheEncryptionKey = Configuration.GetValue(VippsConfigurationKeyNames.CACHE_KEY); - _cacheDirectoryPath = Configuration.GetValue(VippsConfigurationKeyNames.CACHE_PATH); + _cacheEncryptionKey = Configuration.CacheEncryptionKey; + _cacheDirectoryPath = Configuration.CacheDirectoryPath; if (_cacheDirectoryPath.IsPresent()) { if (!_cacheDirectoryPath.IsDirectoryWritable()) { _logger.LogError("Could not write to cache file directory (" |
