aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-03-31 20:09:32 +0200
committerivarlovlie <git@ivarlovlie.no>2021-03-31 20:09:32 +0200
commit42c353a30bf0189ea4f0340e0c8251375483f3df (patch)
treef50f411173fefb35ff717d1e70a17b4e2929a880 /src
parent2cc0a54c5a33c54688154a4594039e0760fa0aae (diff)
downloaddotnet-vipps-ecommerce-42c353a30bf0189ea4f0340e0c8251375483f3df.tar.xz
dotnet-vipps-ecommerce-42c353a30bf0189ea4f0340e0c8251375483f3df.zip
Add more options for getting configuration values
Diffstat (limited to 'src')
-rw-r--r--src/IOL.VippsEcommerce.Client/Program.cs2
-rw-r--r--src/IOL.VippsEcommerce/Models/VippsConfiguration.cs20
-rw-r--r--src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs25
3 files changed, 42 insertions, 5 deletions
diff --git a/src/IOL.VippsEcommerce.Client/Program.cs b/src/IOL.VippsEcommerce.Client/Program.cs
index b165f63..2f83dc1 100644
--- a/src/IOL.VippsEcommerce.Client/Program.cs
+++ b/src/IOL.VippsEcommerce.Client/Program.cs
@@ -1,6 +1,7 @@
using System;
using System.Text.Json;
using IOL.VippsEcommerce;
+using IOL.VippsEcommerce.Models;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
@@ -11,6 +12,7 @@ services.AddVippsEcommerceService(o => {
o.ClientId = "";
o.CacheEncryptionKey = "";
o.CacheDirectoryPath = "";
+ o.ConfigurationMode = VippsConfigurationMode.ONLY_OBJECT;
});
var provider = services.BuildServiceProvider();
var vippsEcommerceService = provider.GetService<IVippsEcommerceService>();
diff --git a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs
index e1b339e..d8b3a0f 100644
--- a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs
+++ b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs
@@ -101,19 +101,29 @@ namespace IOL.VippsEcommerce.Models
/// Use environment variables for configuration.
/// <para>If this is true, all requested properties are looked for in the environment.</para>
/// </summary>
- public bool UseEnvironment { get; set; }
+ public VippsConfigurationMode ConfigurationMode { get; set; } = VippsConfigurationMode.ONLY_OBJECT;
/// <summary>
/// Get value from configuration, either from Dependency injection or from the environment.
/// </summary>
/// <param name="key">Configuration key.</param>
- /// <param name="fallback">Fallback value if the key is not found or empty.</param>
/// <returns>A string containing the configuration value (or a fallback).</returns>
- public string GetValue(string key, string fallback = default) {
- if (UseEnvironment) {
- return Environment.GetEnvironmentVariable(key) ?? fallback;
+ public string GetValue(string key) {
+ switch (ConfigurationMode) {
+ case VippsConfigurationMode.ONLY_OBJECT:
+ return GetValueFromObject(key);
+ case VippsConfigurationMode.ONLY_ENVIRONMENT:
+ return Environment.GetEnvironmentVariable(key);
+ case VippsConfigurationMode.OBJECT_THEN_ENVIRONMENT:
+ return GetValueFromObject(key) ?? Environment.GetEnvironmentVariable(key);
+ case VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT:
+ return Environment.GetEnvironmentVariable(key) ?? GetValueFromObject(key);
+ default:
+ return default;
}
+ }
+ private string GetValueFromObject(string key) {
foreach (var prop in typeof(VippsConfiguration).GetProperties()) {
foreach (var attribute in prop.CustomAttributes) {
foreach (var argument in attribute.ConstructorArguments) {
diff --git a/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs b/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs
new file mode 100644
index 0000000..edff72f
--- /dev/null
+++ b/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs
@@ -0,0 +1,25 @@
+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