-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
106 lines (94 loc) · 4.51 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
106 lines (94 loc) · 4.51 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace RestSharp.Extensions.DependencyInjection;
public static class ServiceCollectionExtensions {
extension(IServiceCollection services) {
/// <summary>
/// Adds a named RestClient to the service collection.
/// </summary>
/// <param name="name">Client name</param>
/// <param name="configureRestClient">Optional: function to configure the client options.</param>
/// <param name="configureSerialization">Optional: function to configure serializers.</param>
[PublicAPI]
public void AddRestClient(
string name,
ConfigureRestClient? configureRestClient = null,
ConfigureSerialization? configureSerialization = null
) {
Ensure.NotEmptyString(name, nameof(name));
Ensure.NotNull(services, nameof(services));
var options = new RestClientOptions();
var configure = configureRestClient ?? (_ => { });
configure(options);
services
.AddHttpClient(name)
.ConfigurePrimaryHttpMessageHandler(() => {
var handler = new HttpClientHandler();
RestClient.ConfigureHttpMessageHandler(handler, options);
return handler;
}
);
services.TryAddSingleton<IRestClientFactory, DefaultRestClientFactory>();
if (name == Constants.DefaultRestClient) {
services.AddTransient<IRestClient>(sp => {
var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient(name);
return new RestClient(client, options);
}
);
}
else {
services.Configure<RestClientConfigOptions>(
Constants.GetConfigName(name),
o => {
o.ConfigureRestClient = configureRestClient;
o.ConfigureSerialization = configureSerialization;
}
);
}
}
/// <summary>
/// Adds a RestClient to the service collection with default options.
/// </summary>
[PublicAPI]
public void AddRestClient() => services.AddRestClient(Constants.DefaultRestClient);
/// <summary>
/// Adds a RestClient to the service collection with a base URL.
/// </summary>
/// <param name="baseUrl">The base URL for the RestClient.</param>
[PublicAPI]
public void AddRestClient(Uri baseUrl) => services.AddRestClient(Constants.DefaultRestClient, o => o.BaseUrl = baseUrl);
/// <summary>
/// Adds a RestClient to the service collection with custom options.
/// </summary>
/// <param name="options">Custom options for the RestClient.</param>
[PublicAPI]
public void AddRestClient(RestClientOptions options) {
Ensure.NotNull(options, nameof(options));
services.AddRestClient(Constants.DefaultRestClient, o => o.CopyFrom(options));
}
/// <summary>
/// Adds a RestClient to the service collection with custom options.
/// </summary>
/// <param name="configureRestClient">Function to configure the RestClient options.</param>
[PublicAPI]
public void AddRestClient(ConfigureRestClient configureRestClient) {
Ensure.NotNull(configureRestClient, nameof(configureRestClient));
services.AddRestClient(Constants.DefaultRestClient, configureRestClient);
}
/// <summary>
/// Adds a named RestClient to the service collection with base URL.
/// </summary>
/// <param name="name">Client name.</param>
/// <param name="baseUrl">The base URL for the RestClient.</param>
public void AddRestClient(string name, Uri baseUrl) => services.AddRestClient(name, o => o.BaseUrl = baseUrl);
/// <summary>
/// Adds a named RestClient to the service collection with custom options.
/// </summary>
/// <param name="name">Client name.</param>
/// <param name="options">Custom options for the RestClient.</param>
public void AddRestClient(string name, RestClientOptions options) {
Ensure.NotNull(options, nameof(options));
services.AddRestClient(name, o => o.CopyFrom(options));
}
}
}