Repository navigation
Expand file tree
/
Copy pathHttpClientFactoryCore.cs
More file actions
162 lines (141 loc) · 7.86 KB
/
Copy pathHttpClientFactoryCore.cs
File metadata and controls
162 lines (141 loc) · 7.86 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved.
// ReactiveUI and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
namespace Refit;
/// <summary>Shared registration logic backing the Refit HttpClientFactory extension methods.</summary>
internal static class HttpClientFactoryCore
{
/// <summary>Registers a strongly typed, source-generated Refit client without any reflection fallback.</summary>
/// <typeparam name="T">The Refit interface type to register.</typeparam>
/// <param name="services">The service collection to register into.</param>
/// <param name="settings">A factory that produces the Refit settings, or null.</param>
/// <param name="httpClientName">A name for the underlying HTTP client, or null.</param>
/// <returns>The HTTP client builder for further configuration.</returns>
[SuppressMessage(
"Design",
"SST2307:Generic method type parameters should be inferable from the parameters",
Justification = "The Refit interface type is intentionally specified explicitly by callers.")]
internal static IHttpClientBuilder AddRefitGeneratedClientCore<T>(
IServiceCollection services,
Func<IServiceProvider, RefitSettings?>? settings,
string? httpClientName)
where T : class
{
ArgumentExceptionHelper.ThrowIfNull(services);
// register settings
_ = services.AddSingleton(provider => new SettingsFor<T>(settings?.Invoke(provider)));
// create HttpClientBuilder
var builder = services.AddHttpClient(httpClientName ?? UniqueName.ForType<T>());
// configure the primary handler from the supplied settings (or fall back to the default)
_ = builder.ConfigurePrimaryHttpMessageHandler(static serviceProvider =>
CreateInnerHandlerIfProvided(
serviceProvider.GetRequiredService<SettingsFor<T>>().Settings)
?? new HttpClientHandler());
// add typed client backed by the source generator only; throws clearly if no generated client exists
return builder.AddTypedClient(static (client, serviceProvider) =>
RestService.ForGenerated<T>(
client,
serviceProvider.GetRequiredService<SettingsFor<T>>().Settings ?? new RefitSettings()));
}
/// <summary>Registers a strongly typed, keyed, source-generated Refit client without any reflection fallback.</summary>
/// <typeparam name="T">The Refit interface type to register.</typeparam>
/// <param name="services">The service collection to register into.</param>
/// <param name="serviceKey">The key under which the client is registered.</param>
/// <param name="settings">A factory that produces the Refit settings, or null.</param>
/// <param name="httpClientName">A name for the underlying HTTP client, or null.</param>
/// <returns>The HTTP client builder for further configuration.</returns>
[SuppressMessage(
"Design",
"SST2307:Generic method type parameters should be inferable from the parameters",
Justification = "The Refit interface type is intentionally specified explicitly by callers.")]
internal static IHttpClientBuilder AddKeyedRefitGeneratedClientCore<T>(
IServiceCollection services,
object? serviceKey,
Func<IServiceProvider, RefitSettings?>? settings,
string? httpClientName)
where T : class
{
ArgumentExceptionHelper.ThrowIfNull(services);
ArgumentExceptionHelper.ThrowIfNull(serviceKey);
// register settings
_ = services.AddKeyedSingleton(
serviceKey,
(provider, _) => new SettingsFor<T>(settings?.Invoke(provider)));
// create HttpClientBuilder
var builder = services.AddHttpClient(httpClientName ?? UniqueName.ForType<T>(serviceKey));
// configure primary and additional handlers from the keyed settings
ConfigureKeyedHandlers(
builder,
serviceProvider => serviceProvider.GetRequiredKeyedService<SettingsFor<T>>(serviceKey));
// add keyed typed client backed by the source generator only; throws clearly if no generated client exists
_ = builder.Services.AddKeyedTransient(
serviceKey,
(s, _) =>
{
var httpClientFactory = s.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient(builder.Name);
return RestService.ForGenerated<T>(
httpClient,
s.GetRequiredKeyedService<SettingsFor<T>>(serviceKey).Settings ?? new RefitSettings());
});
return builder;
}
#if NET8_0_OR_GREATER
/// <summary>Wraps a settings factory so the settings it produces run on a source-generated JSON context.</summary>
/// <param name="settings">A factory that produces the Refit settings, or null.</param>
/// <param name="context">The generated context to register.</param>
/// <param name="allowReflectionFallback">Whether a type no resolver describes may use reflection-based JSON.</param>
/// <returns>A factory whose settings keep the serializer options they came with and gain the context, or use the context's own options when the factory yields no settings.</returns>
internal static Func<IServiceProvider, RefitSettings?> WithJsonContext(
Func<IServiceProvider, RefitSettings?>? settings,
System.Text.Json.Serialization.JsonSerializerContext context,
bool allowReflectionFallback)
{
ArgumentExceptionHelper.ThrowIfNull(context);
return provider => settings?.Invoke(provider) is { } supplied
? supplied.UseJsonContext(context, allowReflectionFallback)
: RefitSettings.ForJsonContext(context, allowReflectionFallback);
}
#endif
/// <summary>Configures the primary and authorization handlers for a keyed Refit client from its settings.</summary>
/// <param name="builder">The HTTP client builder to configure.</param>
/// <param name="settingsResolver">Resolves the settings holder for the keyed client from a service provider.</param>
internal static void ConfigureKeyedHandlers(
IHttpClientBuilder builder,
Func<IServiceProvider, ISettingsFor> settingsResolver)
{
_ = builder.ConfigurePrimaryHttpMessageHandler(serviceProvider =>
settingsResolver(serviceProvider).Settings?.HttpMessageHandlerFactory?.Invoke() ?? new HttpClientHandler());
_ = builder.ConfigureAdditionalHttpMessageHandlers((handlers, serviceProvider) =>
{
if (settingsResolver(serviceProvider).Settings?.AuthorizationHeaderValueGetter is not { } getToken)
{
return;
}
handlers.Add(new AuthenticatedHttpClientHandler(null, getToken));
});
}
/// <summary>Builds the inner HTTP message handler from the supplied settings, if any.</summary>
/// <param name="settings">The Refit settings that may provide a handler factory or auth getter.</param>
/// <returns>The configured inner handler, or null when none is required.</returns>
internal static HttpMessageHandler? CreateInnerHandlerIfProvided(RefitSettings? settings)
{
HttpMessageHandler? innerHandler = null;
if (settings is not null)
{
if (settings.HttpMessageHandlerFactory is not null)
{
innerHandler = settings.HttpMessageHandlerFactory();
}
if (settings.AuthorizationHeaderValueGetter is not null)
{
innerHandler = new AuthenticatedHttpClientHandler(
settings.AuthorizationHeaderValueGetter,
innerHandler);
}
}
return innerHandler;
}
}