Created
February 12, 2024 02:45
-
-
Save Kittoes0124/f69eecc9119a29b6a0ccb538bd86985c to your computer and use it in GitHub Desktop.
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
using Azure.Core; | |
using System.Text.Json; | |
namespace Sandbox; | |
public sealed class AzureIdentityHttpClient(HttpClient httpClient) | |
{ | |
public async Task<T?> SendAsync<T>( | |
Func<HttpResponseMessage, JsonSerializerOptions, CancellationToken, ValueTask<T>> handler, | |
HttpMethod httpMethod, | |
JsonSerializerOptions jsonSerializerOptions, | |
TokenRequestContext tokenRequestContext, | |
Uri uri, | |
HttpCompletionOption httpCompletionOption = default, | |
CancellationToken cancellationToken = default | |
) { | |
using var httpRequestMessage = new HttpRequestMessage( | |
method: httpMethod, | |
requestUri: uri | |
); | |
httpRequestMessage.Options.Set( | |
key: AzureIdentityAuthenticationHandler.TokenRequestContextKey, | |
value: tokenRequestContext | |
); | |
using var httpResponseMessage = await httpClient | |
.SendAsync( | |
cancellationToken: cancellationToken, | |
completionOption: httpCompletionOption, | |
request: httpRequestMessage | |
) | |
.ConfigureAwait(continueOnCapturedContext: false); | |
return await handler( | |
arg1: httpResponseMessage, | |
arg2: jsonSerializerOptions, | |
arg3: cancellationToken | |
) | |
.ConfigureAwait(continueOnCapturedContext: false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment