public static void AppendHeaders(this HttpRequestMessage src, CustomHttpHeaders headersEx)
 {
     foreach (var h in headersEx)
     {
         if (string.IsNullOrEmpty(h.Key) || h.Value == null || !h.Value.Any())
         {
             continue;
         }
         src.Headers.Add(h.Key, h.Value);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestType"></param>
        /// <param name="apiMethod"></param>
        /// <param name="body"></param>
        /// <param name="cancel"></param>
        /// <param name="cfgAction"></param>
        /// <typeparam name="TReq"></typeparam>
        /// <typeparam name="TResp"></typeparam>
        /// <returns></returns>
        public async Task <IResponse <TResp> > SendHttpRequest <TReq, TResp>(HttpMethod requestType, string apiMethod, TReq body, CancellationToken cancel = default,
                                                                             Action <HttpSenderOptions, CustomHttpHeaders> cfgAction = default)
            where TResp : new()
        {
            HttpSenderOptions options;
            var headers = new CustomHttpHeaders();

            if (cfgAction == default)
            {
                options = _options;
            }
            else
            {
                options = new HttpSenderOptions();
                cfgAction.Invoke(options, headers);
            }

            var content = DoCreateContent(body, options);

            return(await SendHttpRequest <TResp>(requestType, apiMethod, content, cancel, options, headers));
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestType"></param>
        /// <param name="apiMethod"></param>
        /// <param name="content"></param>
        /// <param name="cancel"></param>
        /// <param name="options"></param>
        /// <param name="headers"></param>
        /// <typeparam name="TResp"></typeparam>
        /// <returns></returns>
        public async Task <IResponse <TResp> > SendHttpRequest <TResp>(HttpMethod requestType, string apiMethod, HttpContent content,
                                                                       CancellationToken cancel = default, HttpSenderOptions options = default, CustomHttpHeaders headers = default)
            where TResp : new()
        {
            var uri           = new Uri(_client.BaseAddress, apiMethod);
            var senderOptions = options ?? _options ?? new HttpSenderOptions();
            var senderHeaders = headers ?? new CustomHttpHeaders();

            using (var request = new HttpRequestMessage(requestType, uri))
            {
                request.Content = content;
                request.AppendHeaders(senderHeaders);

                using (var cts = HttpSenderHelper.CreateCancellationTokenSource(senderOptions.RequestTimeout, cancel))
                {
                    try
                    {
                        _logger.LogDebug($"Request: [{requestType.ToString().ToUpper()}] {uri.AbsoluteUri}");
                        _logger.LogDebug(await content.ReadAsStringAsync());
                        var response = await _client.SendAsync(request, cts?.Token ?? cancel);

                        var bodyAsStr = await HttpSenderHelper.ExtractBodyAsync(response.Content);

                        _logger.LogDebug("Response: " + bodyAsStr);

                        return(HasNotOkStatusCode(response)
                            ? ResponseFactory.CreateFault <TResp>(new HttpFail(response.StatusCode, response.ReasonPhrase))
                            : DoDeserialize <TResp>(bodyAsStr, senderOptions));
                    }
                    catch (OperationCanceledException oex)
                    {
                        var errMsg = cancel.IsCancellationRequested
                            ? $"Client cancel task: {oex.Message}"
                            : $"Connection timeout: {oex.Message}";

                        return(ResponseFactory.CreateFault <TResp>(new HttpFail(errMsg)));
                    }
                    catch (Exception ex)
                    {
                        return(ResponseFactory.CreateFault <TResp>(new HttpFail(ex.Message, ex.InnerException)));
                    }
                }
            }
        }