From e513f0b7483022b0ec0d13b8a7df178d7f5ab88c Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Fri, 28 Jan 2022 21:17:59 +0100 Subject: feat!: Remove support for different configuration stories Configuration values is required to be explicitly specified when adding to DI --- .../Models/VippsConfiguration.cs | 68 ++-------------------- .../Models/VippsConfigurationMode.cs | 25 -------- 2 files changed, 5 insertions(+), 88 deletions(-) delete mode 100644 src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs (limited to 'src/IOL.VippsEcommerce/Models') 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 @@ -98,90 +98,32 @@ namespace IOL.VippsEcommerce.Models [VippsConfigurationKeyName(VippsConfigurationKeyNames.CACHE_KEY)] public string CacheEncryptionKey { get; set; } - /// - /// Specify how to retrieve configuration and/or in what order. Defaults to VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT. - /// - public VippsConfigurationMode ConfigurationMode { get; set; } = VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT; - - /// - /// Get value from configuration, either from Dependency injection or from the environment. - /// - /// Configuration key. - /// A string containing the configuration value. - 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; - } - } - /// /// Ensure that the configuration can be used to issue requests to the vipps api. /// Throws if a required value is null or whitespace. /// 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 - { - /// - /// Only check for values in the configuration object. - /// - ONLY_OBJECT = 0, - - /// - /// Check for values in environment, then in the configuration object. - /// - ENVIRONMENT_THEN_OBJECT = 1, - - /// - /// Check for values in the configuration object, then in environment. - /// - OBJECT_THEN_ENVIRONMENT = 2, - - /// - /// Only check for values in environment. - /// - ONLY_ENVIRONMENT = 3, - } -} \ No newline at end of file -- cgit v1.3