diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2021-04-02 19:16:39 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2021-04-02 22:35:57 +0200 |
| commit | aae8cac8dfd0bf904c6b8761fec1587ef467c30c (patch) | |
| tree | 531011c5593f7b5884473ced2d6d5b2d5fc641e4 /src/IOL.VippsEcommerce/Models | |
| parent | 78f6e40f0df706a18fa3489e8cce9c36de55859f (diff) | |
| download | dotnet-vipps-ecommerce-aae8cac8dfd0bf904c6b8761fec1587ef467c30c.tar.xz dotnet-vipps-ecommerce-aae8cac8dfd0bf904c6b8761fec1587ef467c30c.zip | |
Make configuration from DI-init optional
Diffstat (limited to 'src/IOL.VippsEcommerce/Models')
| -rw-r--r-- | src/IOL.VippsEcommerce/Models/VippsConfiguration.cs | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs index 19ca140..2fcc91a 100644 --- a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs +++ b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs @@ -98,9 +98,9 @@ namespace IOL.VippsEcommerce.Models public string CacheEncryptionKey { get; set; } /// <summary> - /// Specify how to retrieve configuration and/or in what order. Defaults to VippsConfigurationMode.ONLY_OBJECT. + /// Specify how to retrieve configuration and/or in what order. Defaults to VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT. /// </summary> - public VippsConfigurationMode ConfigurationMode { get; set; } = VippsConfigurationMode.ONLY_OBJECT; + public VippsConfigurationMode ConfigurationMode { get; set; } = VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT; /// <summary> /// Get value from configuration, either from Dependency injection or from the environment. @@ -112,11 +112,11 @@ namespace IOL.VippsEcommerce.Models case VippsConfigurationMode.ONLY_OBJECT: return GetValueFromObject(key); case VippsConfigurationMode.ONLY_ENVIRONMENT: - return Environment.GetEnvironmentVariable(key); + return GetValueFromEnvironment(key); case VippsConfigurationMode.OBJECT_THEN_ENVIRONMENT: - return GetValueFromObject(key) ?? Environment.GetEnvironmentVariable(key); + return GetValueFromObject(key) ?? GetValueFromEnvironment(key); case VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT: - return Environment.GetEnvironmentVariable(key) ?? GetValueFromObject(key); + return GetValueFromEnvironment(key) ?? GetValueFromObject(key); default: return default; } @@ -128,28 +128,39 @@ namespace IOL.VippsEcommerce.Models /// </summary> public void Verify() { if (GetValue(VippsConfigurationKeyNames.VIPPS_API_URL).IsNullOrWhiteSpace()) { - throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_API_URL, - "VippsEcommerceService: VIPPS_API_URL is not provided in configuration."); + throw new ArgumentNullException(nameof(ApiUrl), + "VippsEcommerceService: ApiUrl is not provided in configuration."); } if (GetValue(VippsConfigurationKeyNames.VIPPS_CLIENT_ID).IsNullOrWhiteSpace()) { - throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_CLIENT_ID, - "VippsEcommerceService: VIPPS_CLIENT_ID is not provided in configuration."); + throw new ArgumentNullException(nameof(ClientId), + "VippsEcommerceService: ClientId is not provided in configuration."); } if (GetValue(VippsConfigurationKeyNames.VIPPS_CLIENT_SECRET).IsNullOrWhiteSpace()) { - throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_CLIENT_SECRET, - "VippsEcommerceService: VIPPS_CLIENT_SECRET is not provided in configuration."); + throw new ArgumentNullException(nameof(ClientSecret), + "VippsEcommerceService: ClientSecret is not provided in configuration."); } if (GetValue(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_PRIMARY).IsNullOrWhiteSpace() - && GetValue(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_SECONDARY).IsNullOrWhiteSpace()) { - throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_PRIMARY - + VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_SECONDARY, - "VippsEcommerceService: Neither VIPPS_SUBSCRIPTION_KEY_PRIMARY nor VIPPS_SUBSCRIPTION_KEY_SECONDARY was provided in configuration."); + && GetValue(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_SECONDARY).IsNullOrWhiteSpace()) { + throw new ArgumentNullException(nameof(PrimarySubscriptionKey) + + nameof(SecondarySubscriptionKey), + "VippsEcommerceService: Neither PrimarySubscriptionKey nor SecondarySubscriptionKey was provided in configuration."); } } + private string GetValueFromEnvironment(string key) { +#if DEBUG + var value = Environment.GetEnvironmentVariable(key); + Console.WriteLine("Getting VippsConfiguration value for " + key + " from environment."); + Console.WriteLine("Key: " + key + " Value: " + value); + return value; +#else + return Environment.GetEnvironmentVariable(key); +#endif + } + private string GetValueFromObject(string key) { foreach (var prop in typeof(VippsConfiguration).GetProperties()) { foreach (var attribute in prop.CustomAttributes) { @@ -157,6 +168,7 @@ namespace IOL.VippsEcommerce.Models 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 |
