public async Task <Outcome <HttpResponseMessage> > PostAsync( string path, HttpContent content, HttpClientOptions clientOptions = null, CancellationToken?cancellationToken = null) { var useCancellationToken = cancellationToken ?? CancellationToken.None; var clientOutcome = await OnGetHttpClientAsync(clientOptions, cancellationToken); if (!clientOutcome) { return(Outcome <HttpResponseMessage> .Fail( new Exception("Could not initialize a HTTP client (see inner exception)", clientOutcome.Exception))); } var client = clientOutcome.Value; #if DEBUG || LOG_DEBUG Logger.Debug($"Sending request URI: {path}"); Logger.Debug($"Sending request HEADERS: {client.DefaultRequestHeaders.Concat()}"); #if NET5_0_OR_GREATER var contentString = await content.ReadAsStringAsync(useCancellationToken); #else var contentString = await content.ReadAsStringAsync(); #endif Logger.Debug($"Sending request CONTENT: {contentString}"); #endif var response = await client.PostAsync(path.TrimStart('/'), content, useCancellationToken); return(response.IsSuccessStatusCode ? Outcome <HttpResponseMessage> .Success(response) : Outcome <HttpResponseMessage> .Fail(response)); }
// ReSharper restore MemberCanBePrivate.Global public async Task <Outcome <HttpResponseMessage> > SendAsync( HttpRequestMessage request, CancellationToken?cancellationToken = null) { var useCancellationToken = cancellationToken ?? CancellationToken.None; var clientOptions = new HttpClientOptions { Authentication = request.Headers.Authorization }; var clientOutcome = await OnGetHttpClientAsync(clientOptions, cancellationToken); if (clientOutcome) { return(Outcome <HttpResponseMessage> .Fail( new Exception("Could not initialize a HTTP client (see inner exception)", clientOutcome.Exception))); } var client = clientOutcome.Value; var response = await client.SendAsync(request, useCancellationToken); return(response.IsSuccessStatusCode ? Outcome <HttpResponseMessage> .Success(response) : Outcome <HttpResponseMessage> .Fail(response)); }
public virtual Task <Outcome <HttpClient> > GetHttpClientAsync( HttpClientOptions options = null, CancellationToken?cancellationToken = null, ILogger logger = null) { var transient = options?.IsClientTransient ?? true; if (options?.MessageHandler is { })
protected virtual async Task <Outcome <HttpClient> > OnGetHttpClientAsync(HttpClientOptions clientOptions, CancellationToken?cancellationToken) { var clientOutcome = await HttpClientProvider.GetHttpClientAsync(clientOptions, cancellationToken, Logger); if (!clientOutcome) { return(Outcome <HttpClient> .Fail(new Exception("Could not initialize a HTTP client (see inner exception)", clientOutcome.Exception))); } var client = clientOutcome.Value; if (client.BaseAddress is {})