forked from reactiveui/refit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientFactoryExtensions.cs
More file actions
218 lines (203 loc) · 8.53 KB
/
HttpClientFactoryExtensions.cs
File metadata and controls
218 lines (203 loc) · 8.53 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
namespace Refit
{
/// <summary>
/// HttpClientFactoryExtensions.
/// </summary>
public static class HttpClientFactoryExtensions
{
/// <summary>
/// Adds a Refit client to the DI container
/// </summary>
/// <typeparam name="T">Type of the Refit interface</typeparam>
/// <param name="services">container</param>
/// <param name="settings">Optional. Settings to configure the instance with</param>
/// <returns></returns>
public static IHttpClientBuilder AddRefitClient<T>(
this IServiceCollection services,
RefitSettings? settings = null
)
where T : class
{
return AddRefitClient<T>(services, _ => settings);
}
/// <summary>
/// Adds a Refit client to the DI container
/// </summary>
/// <param name="services">container</param>
/// <param name="refitInterfaceType">Type of the Refit interface</param>
/// <param name="settings">Optional. Settings to configure the instance with</param>
/// <returns></returns>
public static IHttpClientBuilder AddRefitClient(
this IServiceCollection services,
Type refitInterfaceType,
RefitSettings? settings = null
)
{
return AddRefitClient(services, refitInterfaceType, _ => settings);
}
/// <summary>
/// Adds a Refit client to the DI container
/// </summary>
/// <typeparam name="T">Type of the Refit interface</typeparam>
/// <param name="services">container</param>
/// <param name="settingsAction">Optional. Action to configure refit settings. This method is called once and only once, avoid using any scoped dependencies that maybe be disposed automatically.</param>
/// <param name="httpClientName">Optional. Allows users to change the HttpClient name as provided to IServiceCollection.AddHttpClient. Useful for logging scenarios.</param>
/// <returns></returns>
public static IHttpClientBuilder AddRefitClient<T>(
this IServiceCollection services,
Func<IServiceProvider, RefitSettings?>? settingsAction,
string? httpClientName = null
)
where T : class
{
services.AddSingleton(provider => new SettingsFor<T>(settingsAction?.Invoke(provider)));
services.AddSingleton(
provider =>
RequestBuilder.ForType<T>(
provider.GetRequiredService<SettingsFor<T>>().Settings
)
);
return services
.AddHttpClient(httpClientName ?? UniqueName.ForType<T>())
.ConfigureHttpMessageHandlerBuilder(builder =>
{
// check to see if user provided custom auth token
if (
CreateInnerHandlerIfProvided(
builder.Services.GetRequiredService<SettingsFor<T>>().Settings
) is
{ } innerHandler
)
{
builder.PrimaryHandler = innerHandler;
}
})
.AddTypedClient(
(client, serviceProvider) =>
RestService.For<T>(
client,
serviceProvider.GetService<IRequestBuilder<T>>()!
)
);
}
/// <summary>
/// Adds a Refit client to the DI container
/// </summary>
/// <param name="services">container</param>
/// <param name="refitInterfaceType">Type of the Refit interface</param>
/// <param name="settingsAction">Optional. Action to configure refit settings. This method is called once and only once, avoid using any scoped dependencies that maybe be disposed automatically.</param>
/// <param name="httpClientName">Optional. Allows users to change the HttpClient name as provided to IServiceCollection.AddHttpClient. Useful for logging scenarios.</param>
/// <returns></returns>
public static IHttpClientBuilder AddRefitClient(
this IServiceCollection services,
Type refitInterfaceType,
Func<IServiceProvider, RefitSettings?>? settingsAction,
string? httpClientName = null
)
{
var settingsType = typeof(SettingsFor<>).MakeGenericType(refitInterfaceType);
var requestBuilderType = typeof(IRequestBuilder<>).MakeGenericType(refitInterfaceType);
services.AddSingleton(
settingsType,
provider =>
Activator.CreateInstance(
typeof(SettingsFor<>).MakeGenericType(refitInterfaceType)!,
settingsAction?.Invoke(provider)
)!
);
services.AddSingleton(
requestBuilderType,
provider =>
RequestBuilderGenericForTypeMethod
.MakeGenericMethod(refitInterfaceType)
.Invoke(
null,
new object?[]
{
((ISettingsFor)provider.GetRequiredService(settingsType)).Settings
}
)!
);
return services
.AddHttpClient(httpClientName ?? UniqueName.ForType(refitInterfaceType))
.ConfigureHttpMessageHandlerBuilder(builder =>
{
// check to see if user provided custom auth token
if (
CreateInnerHandlerIfProvided(
(
(ISettingsFor)builder.Services.GetRequiredService(settingsType)
).Settings
) is
{ } innerHandler
)
{
builder.PrimaryHandler = innerHandler;
}
})
.AddTypedClient(
refitInterfaceType,
(client, serviceProvider) =>
RestService.For(
refitInterfaceType,
client,
(IRequestBuilder)serviceProvider.GetRequiredService(requestBuilderType)
)
);
}
private static readonly MethodInfo RequestBuilderGenericForTypeMethod =
typeof(RequestBuilder)
.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Single(z => z.IsGenericMethodDefinition && z.GetParameters().Length == 1);
static HttpMessageHandler? CreateInnerHandlerIfProvided(RefitSettings? settings)
{
HttpMessageHandler? innerHandler = null;
if (settings != null)
{
if (settings.HttpMessageHandlerFactory != null)
{
innerHandler = settings.HttpMessageHandlerFactory();
}
if (settings.AuthorizationHeaderValueGetter != null)
{
innerHandler = new AuthenticatedHttpClientHandler(
settings.AuthorizationHeaderValueGetter,
innerHandler
);
}
}
return innerHandler;
}
static IHttpClientBuilder AddTypedClient(
this IHttpClientBuilder builder,
Type type,
Func<HttpClient, IServiceProvider, object> factory
)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}
builder.Services.AddTransient(
type,
s =>
{
var httpClientFactory = s.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient(builder.Name);
return factory(httpClient, s);
}
);
return builder;
}
}
}