1+ namespace Microsoft . Extensions . DependencyInjection
2+ {
3+ using System ;
4+ using System . Net . Http ;
5+ using System . Net . Http . Headers ;
6+
7+ using FluentValidation ;
8+
9+ using Microsoft . Extensions . Configuration ;
10+ using Microsoft . Extensions . Options ;
11+
12+ using SailthruSDK ;
13+
14+ /// <summary>
15+ /// Provides extensions for the <see cref="IServiceCollection"/>
16+ /// </summary>
17+ public static class ServiceCollectionExtensions
18+ {
19+ /// <summary>
20+ /// Adds Sailthru services to the given services collection.
21+ /// </summary>
22+ /// <param name="services">The services collection.</param>
23+ /// <param name="configure">The configure delegate.</param>
24+ /// <returns>The services collection.</returns>
25+ public static IServiceCollection AddSailthru (
26+ IServiceCollection services ,
27+ Action < SailthruSettings > configure )
28+ {
29+ Ensure . IsNotNull ( services , nameof ( services ) ) ;
30+ Ensure . IsNotNull ( configure , nameof ( configure ) ) ;
31+
32+ services . Configure ( configure ) ;
33+
34+ AddCoreServices ( services ) ;
35+
36+ return services ;
37+ }
38+
39+ /// <summary>
40+ /// Adds Sailthru services to the given services collection.
41+ /// </summary>
42+ /// <param name="services">The services collection.</param>
43+ /// <param name="settings">The Sailthru settings.</param>
44+ /// <returns>The services collection.</returns>
45+ public static IServiceCollection AddSailthru (
46+ IServiceCollection services ,
47+ SailthruSettings settings )
48+ {
49+ Ensure . IsNotNull ( services , nameof ( services ) ) ;
50+ Ensure . IsNotNull ( settings , nameof ( settings ) ) ;
51+
52+ services . AddSingleton ( Options . Create ( settings ) ) ;
53+
54+ AddCoreServices ( services ) ;
55+
56+ return services ;
57+ }
58+
59+ /// <summary>
60+ /// Adds Sailthru services to the given services collection.
61+ /// </summary>
62+ /// <param name="services">The services collection.</param>
63+ /// <param name="configurationSection">The configuration section.</param>
64+ /// <returns>The services collection.</returns>
65+ public static IServiceCollection AddSailthru (
66+ IServiceCollection services ,
67+ IConfigurationSection configurationSection )
68+ {
69+ Ensure . IsNotNull ( services , nameof ( services ) ) ;
70+ Ensure . IsNotNull ( configurationSection , nameof ( configurationSection ) ) ;
71+
72+ services . Configure < SailthruSettings > ( configurationSection ) ;
73+
74+ AddCoreServices ( services ) ;
75+
76+ return services ;
77+ }
78+
79+ static void AddCoreServices ( IServiceCollection services )
80+ {
81+ services . AddHttpClient < SailthruClient > (
82+ "Sailthru" ,
83+ ( sp , http ) => ConfigureHttpClient ( sp , http ) ) ;
84+ }
85+
86+ static void ConfigureHttpClient ( IServiceProvider services , HttpClient http )
87+ {
88+ var settings = services . GetRequiredService < IOptions < SailthruSettings > > ( ) . Value ;
89+ SailthruSettingsValidator . Instance . ValidateAndThrow ( settings ) ;
90+
91+ http . BaseAddress = new Uri ( settings . BaseUrl ) ;
92+ http . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
93+ }
94+ }
95+ }
0 commit comments