Skip to content

Commit

Permalink
feature: Add named httpclient support (#1418)
Browse files Browse the repository at this point in the history
* Adds named httpclient support

* Adds IRefitHttpClientFactory

Used to resolve Refit http clients
  • Loading branch information
redbaty committed Apr 12, 2023
1 parent 3c87611 commit b78bbc7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Refit.HttpClientFactory/HttpClientFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;

using Refit.HttpClientFactory;

namespace Refit
{
public static class HttpClientFactoryExtensions
Expand Down Expand Up @@ -41,13 +43,24 @@ public static IHttpClientBuilder AddRefitClient(this IServiceCollection services
/// <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>
/// <returns></returns>
public static IHttpClientBuilder AddRefitClient<T>(this IServiceCollection services, Func<IServiceProvider, RefitSettings?>? settingsAction) where T : class
public static IHttpClientBuilder AddRefitClient<T>(this IServiceCollection services, Func<IServiceProvider, RefitSettings?>? settingsAction) where T : class => AddRefitClient<T>(services, null, settingsAction);

/// <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="name">Named http client</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>
/// <returns></returns>
public static IHttpClientBuilder AddRefitClient<T>(this IServiceCollection services, string? name, Func<IServiceProvider, RefitSettings?>? settingsAction) where T : class
{
services.AddSingleton(provider => new SettingsFor<T>(settingsAction?.Invoke(provider)));
services.AddSingleton(provider => RequestBuilder.ForType<T>(provider.GetRequiredService<SettingsFor<T>>().Settings));
services.AddScoped<IRefitHttpClientFactory, RefitHttpClientFactory>();

return services
.AddHttpClient(UniqueName.ForType<T>())
.AddHttpClient(name ?? UniqueName.ForType<T>())
.ConfigureHttpMessageHandlerBuilder(builder =>
{
// check to see if user provided custom auth token
Expand All @@ -56,7 +69,7 @@ public static IHttpClientBuilder AddRefitClient(this IServiceCollection services
builder.PrimaryHandler = innerHandler;
}
})
.AddTypedClient((client, serviceProvider) => RestService.For<T>(client, serviceProvider.GetService<IRequestBuilder<T>>()!));
.AddTypedClient((client, serviceProvider) => RestService.For(client, serviceProvider.GetService<IRequestBuilder<T>>()!));
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions Refit.HttpClientFactory/IRefitHttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Refit.HttpClientFactory;

public interface IRefitHttpClientFactory
{
T CreateClient<T>(string? name);
}
25 changes: 25 additions & 0 deletions Refit.HttpClientFactory/RefitHttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Net.Http;

using Microsoft.Extensions.DependencyInjection;

namespace Refit.HttpClientFactory;

internal class RefitHttpClientFactory : IRefitHttpClientFactory
{
public RefitHttpClientFactory(IHttpClientFactory httpClientFactory, IServiceProvider serviceProvider)
{
HttpClientFactory = httpClientFactory;
ServiceProvider = serviceProvider;
}

private IHttpClientFactory HttpClientFactory { get; }

private IServiceProvider ServiceProvider { get; }

public T CreateClient<T>(string? name)
{
var client = HttpClientFactory.CreateClient(name ?? UniqueName.ForType<T>());
return RestService.For(client, ServiceProvider.GetRequiredService<IRequestBuilder<T>>());
}
}

0 comments on commit b78bbc7

Please sign in to comment.