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

Add an authorization header value getter property that supports cancellation #1413

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
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