summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-04-02 19:16:39 +0200
committerivarlovlie <git@ivarlovlie.no>2021-04-02 22:35:57 +0200
commitaae8cac8dfd0bf904c6b8761fec1587ef467c30c (patch)
tree531011c5593f7b5884473ced2d6d5b2d5fc641e4
parent78f6e40f0df706a18fa3489e8cce9c36de55859f (diff)
downloaddotnet-vipps-ecommerce-aae8cac8dfd0bf904c6b8761fec1587ef467c30c.tar.xz
dotnet-vipps-ecommerce-aae8cac8dfd0bf904c6b8761fec1587ef467c30c.zip
Make configuration from DI-init optional
-rw-r--r--src/IOL.VippsEcommerce/Models/VippsConfiguration.cs42
-rw-r--r--src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs24
2 files changed, 48 insertions, 18 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
diff --git a/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs b/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs
index d9c9769..1637dc9 100644
--- a/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs
+++ b/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs
@@ -16,13 +16,31 @@ namespace IOL.VippsEcommerce
this IServiceCollection services,
Action<VippsConfiguration> configuration
) {
- if (services == null) throw new ArgumentNullException(nameof(services));
- if (configuration == null) throw new ArgumentNullException(nameof(configuration));
-
+ if (services == null)
+ throw new ArgumentNullException(nameof(services));
+ if (configuration == null)
+ throw new ArgumentNullException(nameof(configuration));
services.Configure(configuration);
services.AddHttpClient<IVippsEcommerceService, VippsEcommerceService>();
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;
+ }
}
} \ No newline at end of file