Skip to content

Commit c5d4ce7

Browse files
authored
Merge pull request #15 from Cabazure/avoid-type-conflicts
Ensure AddCabazureClient is only generated if needed
2 parents 243c7dc + eb269a0 commit c5d4ce7

File tree

3 files changed

+124
-104
lines changed

3 files changed

+124
-104
lines changed

src/Cabazure.Client/ClientInitializationGenerator.cs

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

test/Cabazure.Client.Tests/ClientInitializationGeneratorTests.CanGenerate_Initialization#ClientInitialization.Implementation.g.verified.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,74 @@
22
// <auto-generated/>
33
#nullable enable
44
using System;
5+
using System.Collections.Generic;
6+
using System.Net.Http;
57
using System.Text.Json;
8+
using Azure.Core;
9+
using Cabazure.Client;
10+
using Cabazure.Client.Authentication;
611
using Cabazure.Client.Builder;
712
using Microsoft.Extensions.DependencyInjection.Extensions;
13+
using Microsoft.Extensions.Options;
814

915
namespace Microsoft.Extensions.DependencyInjection
1016
{
1117
internal static partial class ClientInitialization
1218
{
19+
internal static partial IServiceCollection AddCabazureClient<TOptions>(
20+
this IServiceCollection services,
21+
string clientName,
22+
Action<JsonSerializerOptions>? jsonOptions,
23+
Action<TOptions>? clientOptions,
24+
Action<IHttpClientBuilder>? builder)
25+
where TOptions : class, ICabazureClientOptions
26+
{
27+
if (clientOptions != null)
28+
{
29+
services
30+
.AddOptions<TOptions>()
31+
.Configure(clientOptions);
32+
}
33+
34+
void ConfigureHttpClient(IServiceProvider services, HttpClient client)
35+
=> client.BaseAddress = services
36+
.GetRequiredService<IOptions<TOptions>>()
37+
.Value
38+
.GetBaseAddress();
39+
40+
void ConfigureAuthHandler(IList<DelegatingHandler> handlers, IServiceProvider services)
41+
{
42+
var options = services
43+
.GetRequiredService<IOptions<TOptions>>()
44+
.Value;
45+
46+
if (options is ICabazureAuthClientOptions authOptions)
47+
{
48+
var scope = authOptions.GetScope();
49+
var credential = authOptions.GetCredential();
50+
51+
var tokenProvider = new BearerTokenProvider(
52+
new TokenRequestContext(new [] { scope }),
53+
credential,
54+
new DateTimeProvider());
55+
56+
handlers.Add(new AzureAuthenticationHandler(tokenProvider));
57+
}
58+
}
59+
60+
void BuildHttpClient(IHttpClientBuilder b)
61+
{
62+
b.ConfigureHttpClient(ConfigureHttpClient);
63+
b.ConfigureAdditionalHttpMessageHandlers(ConfigureAuthHandler);
64+
builder?.Invoke(b);
65+
}
66+
67+
return services.AddCabazureClient(
68+
clientName,
69+
jsonOptions,
70+
BuildHttpClient);
71+
}
72+
1373
internal static partial IServiceCollection AddCabazureClient(
1474
this IServiceCollection services,
1575
string clientName,

test/Cabazure.Client.Tests/ClientInitializationGeneratorTests.CanGenerate_Initialization#ClientInitialization.g.verified.cs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,21 @@
22
// <auto-generated/>
33
#nullable enable
44
using System;
5-
using System.Collections.Generic;
6-
using System.Net.Http;
75
using System.Text.Json;
8-
using Azure.Core;
96
using Cabazure.Client;
10-
using Cabazure.Client.Authentication;
117
using Microsoft.Extensions.Options;
128

139
namespace Microsoft.Extensions.DependencyInjection
1410
{
1511
internal static partial class ClientInitialization
1612
{
17-
internal static IServiceCollection AddCabazureClient<TOptions>(
13+
internal static partial IServiceCollection AddCabazureClient<TOptions>(
1814
this IServiceCollection services,
1915
string clientName,
2016
Action<JsonSerializerOptions>? jsonOptions,
2117
Action<TOptions>? clientOptions,
2218
Action<IHttpClientBuilder>? builder = default)
23-
where TOptions : class, ICabazureClientOptions
24-
{
25-
if (clientOptions != null)
26-
{
27-
services
28-
.AddOptions<TOptions>()
29-
.Configure(clientOptions);
30-
}
31-
32-
void ConfigureHttpClient(IServiceProvider services, HttpClient client)
33-
=> client.BaseAddress = services
34-
.GetRequiredService<IOptions<TOptions>>()
35-
.Value
36-
.GetBaseAddress();
37-
38-
void ConfigureAuthHandler(IList<DelegatingHandler> handlers, IServiceProvider services)
39-
{
40-
var options = services
41-
.GetRequiredService<IOptions<TOptions>>()
42-
.Value;
43-
44-
if (options is ICabazureAuthClientOptions authOptions)
45-
{
46-
var scope = authOptions.GetScope();
47-
var credential = authOptions.GetCredential();
48-
49-
var tokenProvider = new BearerTokenProvider(
50-
new TokenRequestContext(new [] { scope }),
51-
credential,
52-
new DateTimeProvider());
53-
54-
handlers.Add(new AzureAuthenticationHandler(tokenProvider));
55-
}
56-
}
57-
58-
void BuildHttpClient(IHttpClientBuilder b)
59-
{
60-
b.ConfigureHttpClient(ConfigureHttpClient);
61-
b.ConfigureAdditionalHttpMessageHandlers(ConfigureAuthHandler);
62-
builder?.Invoke(b);
63-
}
64-
65-
return services.AddCabazureClient(
66-
clientName,
67-
jsonOptions,
68-
BuildHttpClient);
69-
}
19+
where TOptions : class, ICabazureClientOptions;
7020

7121
internal static partial IServiceCollection AddCabazureClient(
7222
this IServiceCollection services,

0 commit comments

Comments
 (0)