aboutsummaryrefslogtreecommitdiffstats
path: root/src/IOL.VippsEcommerce/ServiceCollectionExtensions.cs
blob: 57f1833655e77af272d33f25fa540f3efb99ae72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using IOL.VippsEcommerce.Models;
using Microsoft.Extensions.DependencyInjection;

namespace IOL.VippsEcommerce
{
	public static class ServiceCollectionExtensions
	{
		/// <summary>
		/// Configures and adds the VippsEcommerceService to your DI. 
		/// </summary>
		/// <param name="services">Servicecollection to add VippsEcommerceService to.</param>
		/// <param name="configuration"></param>
		/// <returns></returns>
		public static IServiceCollection AddVippsEcommerceService(
				this IServiceCollection services,
				Action<VippsConfiguration> 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;
		}
	}
}