-
-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add named httpclient support (#1418)
* Adds named httpclient support * Adds IRefitHttpClientFactory Used to resolve Refit http clients
- Loading branch information
Showing
3 changed files
with
47 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>()); | ||
} | ||
} |