Example #1
0
        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));
        }
Example #2
0
        // 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));
        }
Example #3
0
        public virtual Task <Outcome <HttpClient> > GetHttpClientAsync(
            HttpClientOptions options           = null,
            CancellationToken?cancellationToken = null,
            ILogger logger = null)
        {
            var transient = options?.IsClientTransient ?? true;

            if (options?.MessageHandler is { })
Example #4
0
        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 {})