-
Notifications
You must be signed in to change notification settings - Fork 4
/
ServiceCollectionExtensions.cs
69 lines (63 loc) · 4 KB
/
ServiceCollectionExtensions.cs
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
using Microsoft.Extensions.DependencyInjection;
using TypedRest.Endpoints;
namespace TypedRest;
/// <summary>
/// Provides extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers <see cref="EntryEndpoint"/> for dependency injection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="uri">The base URI of the REST API. Missing trailing slash will be appended automatically.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the underlying <see cref="HttpClient"/>.</returns>
public static IHttpClientBuilder AddTypedRest(this IServiceCollection services, Uri uri)
=> services.AddTypedRest<EntryEndpoint>(uri);
/// <summary>
/// Registers a type derived from <see cref="EntryEndpoint"/> for dependency injection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="uri">The base URI of the REST API. Missing trailing slash will be appended automatically.</param>
/// <typeparam name="TEndpoint">The type of the <see cref="EntryEndpoint"/>. Must provide a constructor that accepts an <see cref="HttpClient"/>.</typeparam>
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the underlying <see cref="HttpClient"/>.</returns>
public static IHttpClientBuilder AddTypedRest<TEndpoint>(this IServiceCollection services, Uri uri)
where TEndpoint : EntryEndpoint
=> services.AddHttpClient<TEndpoint>(client => client.BaseAddress = uri);
/// <summary>
/// Registers a type derived from <see cref="EntryEndpoint"/> for dependency injection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="uri">The base URI of the REST API. Missing trailing slash will be appended automatically.</param>
/// <typeparam name="TInterface">The interface to register.</typeparam>
/// <typeparam name="TEndpoint">The type of the <see cref="EntryEndpoint"/>. Must provide a constructor that accepts an <see cref="HttpClient"/>.</typeparam>
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the underlying <see cref="HttpClient"/>.</returns>
public static IHttpClientBuilder AddTypedRest<TInterface, TEndpoint>(this IServiceCollection services, Uri uri)
where TInterface : class
where TEndpoint : EntryEndpoint, TInterface
=> services.AddHttpClient<TInterface, TEndpoint>(client => client.BaseAddress = uri);
/// <summary>
/// Adds HTTP Basic authentication.
/// </summary>
/// <param name="builder">The builder to apply the configuration to.</param>
/// <param name="configureCredentials">A delegate that is used to configure <see cref="NetworkCredential"/>.</param>
public static IHttpClientBuilder AddBasicAuth(this IHttpClientBuilder builder, Action<NetworkCredential> configureCredentials)
=> builder.ConfigureHttpClient(httpClient =>
{
var credentials = new NetworkCredential();
configureCredentials(credentials);
httpClient.AddBasicAuth(credentials);
});
/// <summary>
/// Adds HTTP Basic authentication.
/// </summary>
/// <param name="builder">The builder to apply the configuration to.</param>
/// <param name="credentials">A credential provider. Will be queried using <see cref="HttpClient.BaseAddress"/> as the uri and "Basic" as the authType.</param>
public static IHttpClientBuilder AddBasicAuth(this IHttpClientBuilder builder, ICredentials credentials)
=> builder.ConfigureHttpClient(httpClient =>
{
var networkCredential = credentials.GetCredential(httpClient.BaseAddress ?? throw new InvalidOperationException("HttpClient.BaseAddress must be set."), "Basic");
if (networkCredential != null)
httpClient.AddBasicAuth(networkCredential);
});
}