Skip to content

Instantly share code, notes, and snippets.

@Kittoes0124
Created February 12, 2024 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kittoes0124/f69eecc9119a29b6a0ccb538bd86985c to your computer and use it in GitHub Desktop.
Save Kittoes0124/f69eecc9119a29b6a0ccb538bd86985c to your computer and use it in GitHub Desktop.
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