@@ -17,71 +17,21 @@ public void Initialize(GeneratorInitializationContext context)
1717 // <auto-generated/>
1818 #nullable enable
1919 using System;
20- using System.Collections.Generic;
21- using System.Net.Http;
2220 using System.Text.Json;
23- using Azure.Core;
2421 using Cabazure.Client;
25- using Cabazure.Client.Authentication;
2622 using Microsoft.Extensions.Options;
2723
2824 namespace Microsoft.Extensions.DependencyInjection
2925 {
3026 internal static partial class ClientInitialization
3127 {
32- internal static IServiceCollection AddCabazureClient<TOptions>(
28+ internal static partial IServiceCollection AddCabazureClient<TOptions>(
3329 this IServiceCollection services,
3430 string clientName,
3531 Action<JsonSerializerOptions>? jsonOptions,
3632 Action<TOptions>? clientOptions,
3733 Action<IHttpClientBuilder>? builder = default)
38- where TOptions : class, ICabazureClientOptions
39- {
40- if (clientOptions != null)
41- {
42- services
43- .AddOptions<TOptions>()
44- .Configure(clientOptions);
45- }
46-
47- void ConfigureHttpClient(IServiceProvider services, HttpClient client)
48- => client.BaseAddress = services
49- .GetRequiredService<IOptions<TOptions>>()
50- .Value
51- .GetBaseAddress();
52-
53- void ConfigureAuthHandler(IList<DelegatingHandler> handlers, IServiceProvider services)
54- {
55- var options = services
56- .GetRequiredService<IOptions<TOptions>>()
57- .Value;
58-
59- if (options is ICabazureAuthClientOptions authOptions)
60- {
61- var scope = authOptions.GetScope();
62- var credential = authOptions.GetCredential();
63-
64- var tokenProvider = new BearerTokenProvider(
65- new TokenRequestContext(new [] { scope }),
66- credential,
67- new DateTimeProvider());
68-
69- handlers.Add(new AzureAuthenticationHandler(tokenProvider));
70- }
71- }
72-
73- void BuildHttpClient(IHttpClientBuilder b)
74- {
75- b.ConfigureHttpClient(ConfigureHttpClient);
76- b.ConfigureAdditionalHttpMessageHandlers(ConfigureAuthHandler);
77- builder?.Invoke(b);
78- }
79-
80- return services.AddCabazureClient(
81- clientName,
82- jsonOptions,
83- BuildHttpClient);
84- }
34+ where TOptions : class, ICabazureClientOptions;
8535
8636 internal static partial IServiceCollection AddCabazureClient(
8737 this IServiceCollection services,
@@ -114,9 +64,15 @@ public void Execute(GeneratorExecutionContext context)
11464 . Select ( e => e . Namespace )
11565 . OfType < string > ( )
11666 . Append ( "System" )
67+ . Append ( "System.Net.Http" )
11768 . Append ( "System.Text.Json" )
69+ . Append ( "System.Collections.Generic" )
70+ . Append ( "Azure.Core" )
71+ . Append ( "Cabazure.Client" )
72+ . Append ( "Cabazure.Client.Authentication" )
11873 . Append ( "Cabazure.Client.Builder" )
11974 . Append ( "Microsoft.Extensions.DependencyInjection.Extensions" )
75+ . Append ( "Microsoft.Extensions.Options" )
12076 . Distinct ( )
12177 . OrderByDescending ( us => us . StartsWith ( "System" ) )
12278 . ThenBy ( us => us )
@@ -135,6 +91,60 @@ namespace Microsoft.Extensions.DependencyInjection
13591 {
13692 internal static partial class ClientInitialization
13793 {
94+ internal static partial IServiceCollection AddCabazureClient<TOptions>(
95+ this IServiceCollection services,
96+ string clientName,
97+ Action<JsonSerializerOptions>? jsonOptions,
98+ Action<TOptions>? clientOptions,
99+ Action<IHttpClientBuilder>? builder)
100+ where TOptions : class, ICabazureClientOptions
101+ {
102+ if (clientOptions != null)
103+ {
104+ services
105+ .AddOptions<TOptions>()
106+ .Configure(clientOptions);
107+ }
108+
109+ void ConfigureHttpClient(IServiceProvider services, HttpClient client)
110+ => client.BaseAddress = services
111+ .GetRequiredService<IOptions<TOptions>>()
112+ .Value
113+ .GetBaseAddress();
114+
115+ void ConfigureAuthHandler(IList<DelegatingHandler> handlers, IServiceProvider services)
116+ {
117+ var options = services
118+ .GetRequiredService<IOptions<TOptions>>()
119+ .Value;
120+
121+ if (options is ICabazureAuthClientOptions authOptions)
122+ {
123+ var scope = authOptions.GetScope();
124+ var credential = authOptions.GetCredential();
125+
126+ var tokenProvider = new BearerTokenProvider(
127+ new TokenRequestContext(new [] { scope }),
128+ credential,
129+ new DateTimeProvider());
130+
131+ handlers.Add(new AzureAuthenticationHandler(tokenProvider));
132+ }
133+ }
134+
135+ void BuildHttpClient(IHttpClientBuilder b)
136+ {
137+ b.ConfigureHttpClient(ConfigureHttpClient);
138+ b.ConfigureAdditionalHttpMessageHandlers(ConfigureAuthHandler);
139+ builder?.Invoke(b);
140+ }
141+
142+ return services.AddCabazureClient(
143+ clientName,
144+ jsonOptions,
145+ BuildHttpClient);
146+ }
147+
138148 internal static partial IServiceCollection AddCabazureClient(
139149 this IServiceCollection services,
140150 string clientName,
0 commit comments