Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds named httpclient support #1418

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<T>(this IServiceCollection servi
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>>());
}
}