diff --git a/Extensions.Microsoft.Http/src/AddDefaultRequestHeaderHttpClientBuilderExtensions.cs b/Extensions.Microsoft.Http/src/AddDefaultRequestHeaderHttpClientBuilderExtensions.cs new file mode 100644 index 0000000..34a4dfd --- /dev/null +++ b/Extensions.Microsoft.Http/src/AddDefaultRequestHeaderHttpClientBuilderExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; + +namespace Extensions.Microsoft.Http; + +public static class AddDefaultRequestHeaderHttpClientBuilderExtensions +{ + public static IHttpClientBuilder AddDefaultRequestHeader(this IHttpClientBuilder builder, string name, string? value) + => builder.ConfigureHttpClient((serviceProvider, client) => client.DefaultRequestHeaders.Add(name, value)); + + public static IHttpClientBuilder AddDefaultRequestHeader(this IHttpClientBuilder builder, string name, Func configureValue) + => builder.ConfigureHttpClient((serviceProvider, client) => + { + var value = configureValue(serviceProvider); + client.DefaultRequestHeaders.Add(name, value); + }); + + public static IHttpClientBuilder AddDefaultRequestHeader(this IHttpClientBuilder builder, string name, IEnumerable values) + => builder.ConfigureHttpClient((serviceProvider, client) => client.DefaultRequestHeaders.Add(name, values)); + + public static IHttpClientBuilder AddDefaultRequestHeader(this IHttpClientBuilder builder, string name, Func> configureValues) + => builder.ConfigureHttpClient((serviceProvider, client) => + { + var values = configureValues(serviceProvider); + client.DefaultRequestHeaders.Add(name, values); + }); +} diff --git a/Extensions.Microsoft.Http/src/ConfigureHttpClientServiceCollectionExtensions.cs b/Extensions.Microsoft.Http/src/ConfigureHttpClientServiceCollectionExtensions.cs index 9f8cab9..5921b2d 100644 --- a/Extensions.Microsoft.Http/src/ConfigureHttpClientServiceCollectionExtensions.cs +++ b/Extensions.Microsoft.Http/src/ConfigureHttpClientServiceCollectionExtensions.cs @@ -19,13 +19,41 @@ public static IHttpClientBuilder ConfigureHttpClient(this ISe return builder; } + public static IHttpClientBuilder ConfigureHttpClient(this IServiceCollection services) + where TClient : class + where TImplementation : class, TClient + where TOptions : HttpClientOptions + { + var clientName = typeof(TClient).Name; + + services.ConfigureOptionsFromConfiguration(clientName); + + var builder = services.AddHttpClient((serviceProvider, client) + => ConfigureHttpClient(serviceProvider, client, clientName)); + + return builder; + } + + public static IHttpClientBuilder ConfigureHttpClient(this IServiceCollection services, string name) + where TClient : class + where TImplementation : class, TClient + where TOptions : HttpClientOptions + { + services.ConfigureOptionsFromConfiguration(name); + + var builder = services.AddHttpClient(name, (serviceProvider, client) => + ConfigureHttpClient(serviceProvider, client, name)); + + return builder; + } + public static IHttpClientBuilder ConfigureHttpClient(this IServiceCollection services, string name) where TClient : class where TOptions : HttpClientOptions { services.ConfigureOptionsFromConfiguration(name); - var builder = services.AddHttpClient(name, (serviceProvider, client) => + var builder = services.AddHttpClient(name, (serviceProvider, client) => ConfigureHttpClient(serviceProvider, client, name)); return builder;