@@ -10,67 +10,63 @@ namespace InvvardDev.Ifttt.Hosting;
1010
1111public static class IftttServiceHostingExtensions
1212{
13- public static IWebHostBuilder AddIftttToolkit ( this IWebHostBuilder hostBuilder ,
14- Action < IIftttServiceBuilder , IftttOptions > configureServicesDelegate )
13+ public static IIftttServiceBuilder AddIftttToolkit ( this IServiceCollection services , string serviceKey )
1514 {
16- ArgumentNullException . ThrowIfNull ( hostBuilder ) ;
17- ArgumentNullException . ThrowIfNull ( configureServicesDelegate ) ;
18-
19- return hostBuilder . ConfigureServices ( ( ctx , services ) =>
20- {
21- var options = new IftttOptions ( ) ;
22- configureServicesDelegate ( AddIftttToolkit ( services , options ) , options ) ;
23- } ) ;
15+ ArgumentNullException . ThrowIfNull ( services ) ;
16+ ArgumentNullException . ThrowIfNull ( serviceKey ) ;
17+
18+ return services . AddIftttToolkit ( options => options . ServiceKey = serviceKey ) ;
2419 }
2520
26- public static IWebHostBuilder ConfigureIftttToolkit ( this IWebHostBuilder hostBuilder ,
27- Action < IIftttAppBuilder > configureAppDelegate )
21+ public static IIftttServiceBuilder AddIftttToolkit ( this IServiceCollection services , Action < IftttOptions > setupAction )
2822 {
29- ArgumentNullException . ThrowIfNull ( hostBuilder ) ;
30- ArgumentNullException . ThrowIfNull ( configureAppDelegate ) ;
31-
32- return hostBuilder . Configure ( ( context , applicationBuilder ) =>
33- {
34- configureAppDelegate ( new DefaultIftttAppBuilder ( applicationBuilder ) ) ;
35- } ) ;
23+ ArgumentNullException . ThrowIfNull ( services ) ;
24+ ArgumentNullException . ThrowIfNull ( setupAction ) ;
25+
26+ var builder = new DefaultIftttServiceBuilder ( services ) ;
27+
28+ builder . Services . Configure ( setupAction ) ;
29+
30+ return AddIftttToolkitCore ( builder ) ;
3631 }
3732
38- public static IIftttServiceBuilder UseServiceKeyAuthentication ( this IIftttServiceBuilder builder , string serviceKey )
33+ public static IIftttServiceBuilder AddTestSetupService < T > ( this IIftttServiceBuilder builder )
34+ where T : class , ITestSetup
3935 {
4036 ArgumentNullException . ThrowIfNull ( builder ) ;
41- ArgumentNullException . ThrowIfNull ( serviceKey ) ;
4237
43- builder . ServiceKey = serviceKey ;
38+ builder . Services . AddScoped < ITestSetup , T > ( ) ;
4439
4540 return builder ;
4641 }
4742
48- public static IIftttAppBuilder UseAuthentication ( this IIftttAppBuilder appBuilder )
43+ private static IIftttServiceBuilder AddIftttToolkitCore ( IIftttServiceBuilder builder )
4944 {
50- ArgumentNullException . ThrowIfNull ( appBuilder ) ;
45+ builder . Services
46+ . AddControllers ( )
47+ . AddApplicationPart ( Assembly . GetAssembly ( typeof ( StatusController ) ) ?? throw new InvalidOperationException ( ) )
48+ . AddControllersAsServices ( ) ;
5149
52- appBuilder . App . UseMiddleware < ServiceKeyMiddleware > ( ) ;
50+ builder . Services
51+ . AddScoped < IAssemblyAccessor , AssemblyAccessor > ( )
52+ . AddSingleton < IProcessorRepository , ProcessorRepository > ( ) ;
5353
54- return appBuilder ;
54+ return builder ;
5555 }
5656
57- private static IIftttServiceBuilder AddIftttToolkit ( IServiceCollection services , IftttOptions options )
57+ public static IIftttAppBuilder ConfigureIftttToolkit ( this IApplicationBuilder appBuilder )
5858 {
59- if ( Uri . IsWellFormedUriString ( options . RealTimeBaseAddress , UriKind . RelativeOrAbsolute ) )
60- {
61- throw new UriFormatException ( "The RealTimeBaseAddress is not a valid URI." ) ;
62- }
63-
64- var builder = new DefaultIftttServiceBuilder ( services , options . ServiceKey , options . RealTimeBaseAddress ) ;
59+ ArgumentNullException . ThrowIfNull ( appBuilder ) ;
6560
66- var apiBuilder = builder . Services . AddControllers ( ) ;
61+ return new DefaultIftttAppBuilder ( appBuilder ) ;
62+ }
6763
68- apiBuilder . AddApplicationPart ( Assembly . GetAssembly ( typeof ( StatusController ) ) ?? throw new InvalidOperationException ( ) )
69- . AddControllersAsServices ( ) ;
64+ public static IIftttAppBuilder UseServiceKeyAuthentication ( this IIftttAppBuilder appBuilder )
65+ {
66+ ArgumentNullException . ThrowIfNull ( appBuilder ) ;
7067
71- builder . Services . AddScoped < IAssemblyAccessor , AssemblyAccessor > ( ) ;
72- builder . Services . AddSingleton < IProcessorRepository , ProcessorRepository > ( ) ;
68+ appBuilder . App . UseMiddleware < ServiceKeyMiddleware > ( ) ;
7369
74- return builder ;
70+ return appBuilder ;
7571 }
76- }
72+ }
0 commit comments