Skip to content

Commit

Permalink
support TImplementation
Browse files Browse the repository at this point in the history
add default request headers extensions
  • Loading branch information
smokedlinq committed Feb 8, 2022
1 parent 12a7a17 commit 591a6f5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<IServiceProvider, string?> configureValue)
=> builder.ConfigureHttpClient((serviceProvider, client) =>
{
var value = configureValue(serviceProvider);
client.DefaultRequestHeaders.Add(name, value);
});

public static IHttpClientBuilder AddDefaultRequestHeader(this IHttpClientBuilder builder, string name, IEnumerable<string?> values)
=> builder.ConfigureHttpClient((serviceProvider, client) => client.DefaultRequestHeaders.Add(name, values));

public static IHttpClientBuilder AddDefaultRequestHeader(this IHttpClientBuilder builder, string name, Func<IServiceProvider, IEnumerable<string?>> configureValues)
=> builder.ConfigureHttpClient((serviceProvider, client) =>
{
var values = configureValues(serviceProvider);
client.DefaultRequestHeaders.Add(name, values);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,41 @@ public static IHttpClientBuilder ConfigureHttpClient<TClient, TOptions>(this ISe
return builder;
}

public static IHttpClientBuilder ConfigureHttpClient<TClient, TImplementation, TOptions>(this IServiceCollection services)
where TClient : class
where TImplementation : class, TClient
where TOptions : HttpClientOptions
{
var clientName = typeof(TClient).Name;

services.ConfigureOptionsFromConfiguration<TOptions>(clientName);

var builder = services.AddHttpClient<TClient, TImplementation>((serviceProvider, client)
=> ConfigureHttpClient<TOptions>(serviceProvider, client, clientName));

return builder;
}

public static IHttpClientBuilder ConfigureHttpClient<TClient, TImplementation, TOptions>(this IServiceCollection services, string name)
where TClient : class
where TImplementation : class, TClient
where TOptions : HttpClientOptions
{
services.ConfigureOptionsFromConfiguration<TOptions>(name);

var builder = services.AddHttpClient<TClient, TImplementation>(name, (serviceProvider, client) =>
ConfigureHttpClient<TOptions>(serviceProvider, client, name));

return builder;
}

public static IHttpClientBuilder ConfigureHttpClient<TClient, TOptions>(this IServiceCollection services, string name)
where TClient : class
where TOptions : HttpClientOptions
{
services.ConfigureOptionsFromConfiguration<TOptions>(name);

var builder = services.AddHttpClient<TClient>(name, (serviceProvider, client) =>
var builder = services.AddHttpClient<TClient>(name, (serviceProvider, client) =>
ConfigureHttpClient<TOptions>(serviceProvider, client, name));

return builder;
Expand Down

0 comments on commit 591a6f5

Please sign in to comment.