/// <inheritdoc/> protected override HttpContent CreateContent <T>(T body, HttpSenderOptions options) { if (body is byte[] bytes) { return(HttpSenderHelper.CreateByteArrayContent(bytes)); } return(HttpSenderHelper.CreateStringContent(Serialize(body, options), true)); }
/// <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))); } } } }