Skip to content

Commit

Permalink
feature: Add an authorization header value getter property that suppo…
Browse files Browse the repository at this point in the history
…rts cancellation (#1413)

In addition to `AuthorizationHeaderValueGetter` and `AuthorizationHeaderValueWithParamGetter`, a new `AuthorizationHeaderValueWithCancellationTokenGetter` is added with support for CancellationToken.

Also, the `AuthenticatedHttpClientHandler` and `AuthenticatedParameterizedHttpClientHandler` internal classes have been merged into a single `AuthenticatedHttpClientHandler` class to reduce code duplication.

Co-authored-by: Glenn <5834289+glennawatson@users.noreply.github.com>
  • Loading branch information
0xced and glennawatson committed Apr 12, 2023
1 parent 25c4840 commit 77f084f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 56 deletions.
8 changes: 6 additions & 2 deletions Refit.HttpClientFactory/HttpClientFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,15 @@ public static IHttpClientBuilder AddRefitClient(this IServiceCollection services

if (settings.AuthorizationHeaderValueGetter != null)
{
innerHandler = new AuthenticatedHttpClientHandler(settings.AuthorizationHeaderValueGetter, innerHandler);
innerHandler = new AuthenticatedHttpClientHandler((_, _) => settings.AuthorizationHeaderValueGetter(), innerHandler);
}
else if (settings.AuthorizationHeaderValueWithParamGetter != null)
{
innerHandler = new AuthenticatedParameterizedHttpClientHandler(settings.AuthorizationHeaderValueWithParamGetter, innerHandler);
innerHandler = new AuthenticatedHttpClientHandler((request, _) => settings.AuthorizationHeaderValueWithParamGetter(request), innerHandler);
}
else if (settings.AuthorizationHeaderValueWithCancellationTokenGetter != null)
{
innerHandler = new AuthenticatedHttpClientHandler(settings.AuthorizationHeaderValueWithCancellationTokenGetter, innerHandler);
}
}

Expand Down
16 changes: 1 addition & 15 deletions Refit.Tests/AuthenticatedClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,7 @@ public interface IAuthenticatedServiceWithHeaders
[Fact]
public void DefaultHandlerIsHttpClientHandler()
{
var handler = new AuthenticatedHttpClientHandler((() => Task.FromResult(string.Empty)));

Assert.IsType<HttpClientHandler>(handler.InnerHandler);
}

[Fact]
public void DefaultHandlerIsHttpClientHandlerWithParam()
{
var handler = new AuthenticatedParameterizedHttpClientHandler(((request) => Task.FromResult(string.Empty)));
var handler = new AuthenticatedHttpClientHandler(((_, _) => Task.FromResult(string.Empty)));

Assert.IsType<HttpClientHandler>(handler.InnerHandler);
}
Expand All @@ -70,12 +62,6 @@ public void NullTokenGetterThrows()
Assert.Throws<ArgumentNullException>(() => new AuthenticatedHttpClientHandler(null));
}

[Fact]
public void NullTokenGetterThrowsWithParam()
{
Assert.Throws<ArgumentNullException>(() => new AuthenticatedParameterizedHttpClientHandler(null));
}

[Fact]
public async void AuthenticatedHandlerIgnoresUnAuth()
{
Expand Down
6 changes: 3 additions & 3 deletions Refit/AuthenticatedHttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Refit
{
class AuthenticatedHttpClientHandler : DelegatingHandler
{
readonly Func<Task<string>> getToken;
readonly Func<HttpRequestMessage, CancellationToken, Task<string>> getToken;

public AuthenticatedHttpClientHandler(Func<Task<string>> getToken, HttpMessageHandler? innerHandler = null)
public AuthenticatedHttpClientHandler(Func<HttpRequestMessage, CancellationToken, Task<string>> getToken, HttpMessageHandler? innerHandler = null)
: base(innerHandler ?? new HttpClientHandler())
{
this.getToken = getToken ?? throw new ArgumentNullException(nameof(getToken));
Expand All @@ -22,7 +22,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
var auth = request.Headers.Authorization;
if (auth != null)
{
var token = await getToken().ConfigureAwait(false);
var token = await getToken(request, cancellationToken).ConfigureAwait(false);
request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token);
}

Expand Down
32 changes: 0 additions & 32 deletions Refit/AuthenticatedParameterizedHttpClientHandler.cs

This file was deleted.

5 changes: 5 additions & 0 deletions Refit/RefitSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public RefitSettings()
/// </summary>
public Func<HttpRequestMessage, Task<string>>? AuthorizationHeaderValueWithParamGetter { get; set; }

/// <summary>
/// Supply a function to provide the Authorization header. Does not work if you supply an HttpClient instance.
/// </summary>
public Func<HttpRequestMessage, CancellationToken, Task<string>>? AuthorizationHeaderValueWithCancellationTokenGetter { get; set; }

/// <summary>
/// Supply a custom inner HttpMessageHandler. Does not work if you supply an HttpClient instance.
/// </summary>
Expand Down
12 changes: 8 additions & 4 deletions Refit/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static object For(Type refitInterfaceType, HttpClient client, RefitSettin
public static object For(Type refitInterfaceType, string hostUrl, RefitSettings? settings)
{
var client = CreateHttpClient(hostUrl, settings);

return For(refitInterfaceType, client, settings);
}

Expand All @@ -120,7 +120,7 @@ public static object For(Type refitInterfaceType, string hostUrl, RefitSettings?
/// <param name="refitInterfaceType">Interface to create the implementation for.</param>
/// <param name="hostUrl">Base address the implementation will use.</param>
/// <returns>An instance that implements <paramref name="refitInterfaceType"/>.</returns>
public static object For(Type refitInterfaceType, string hostUrl) => For(refitInterfaceType, hostUrl, null);
public static object For(Type refitInterfaceType, string hostUrl) => For(refitInterfaceType, hostUrl, null);

/// <summary>
/// Create an <see cref="HttpClient"/> with <paramref name="hostUrl"/> as the base address.
Expand Down Expand Up @@ -149,11 +149,15 @@ public static HttpClient CreateHttpClient(string hostUrl, RefitSettings? setting

if (settings.AuthorizationHeaderValueGetter != null)
{
innerHandler = new AuthenticatedHttpClientHandler(settings.AuthorizationHeaderValueGetter, innerHandler);
innerHandler = new AuthenticatedHttpClientHandler((_, _) => settings.AuthorizationHeaderValueGetter(), innerHandler);
}
else if (settings.AuthorizationHeaderValueWithParamGetter != null)
{
innerHandler = new AuthenticatedParameterizedHttpClientHandler(settings.AuthorizationHeaderValueWithParamGetter, innerHandler);
innerHandler = new AuthenticatedHttpClientHandler((request, _) => settings.AuthorizationHeaderValueWithParamGetter(request), innerHandler);
}
else if (settings.AuthorizationHeaderValueWithCancellationTokenGetter != null)
{
innerHandler = new AuthenticatedHttpClientHandler(settings.AuthorizationHeaderValueWithCancellationTokenGetter, innerHandler);
}
}

Expand Down

0 comments on commit 77f084f

Please sign in to comment.