diff --git a/src/Leanetec.EConnect.Infrastructure/EConnectClient.cs b/src/Leanetec.EConnect.Infrastructure/EConnectClient.cs index 56c2e5a..414eec8 100644 --- a/src/Leanetec.EConnect.Infrastructure/EConnectClient.cs +++ b/src/Leanetec.EConnect.Infrastructure/EConnectClient.cs @@ -1,5 +1,6 @@ using Leanetec.EConnect.Client.Interface; using Leanetec.EConnect.Domain.Entities; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System.Net.Http.Json; @@ -13,10 +14,13 @@ public class EConnectClient : IEConnectClient where TError : cl private HttpClient Http => LazyHttp.Value; - public EConnectClient(IOptions options, IHttpClientFactory httpFactory) + private readonly ILogger>? _logger; + + public EConnectClient(IOptions options, IHttpClientFactory httpFactory, ILogger>? logger = null) { _options = options.Value; LazyHttp = new Lazy(httpFactory.CreateEConnectClient); + _logger = logger; } private string? AddQueryString(string? route = null, object? queryParams = null) @@ -127,6 +131,8 @@ public class EConnectClient : IEConnectClient where TError : cl var res = await Http.PostAsync(route, content, cancel); + _logger?.LogCurlAsync(Http, HttpMethod.Post, route, content); + if (res.IsSuccessStatusCode) { return new() diff --git a/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs b/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs index 2612bc5..32ef009 100644 --- a/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs +++ b/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs @@ -5,7 +5,7 @@ namespace Leanetec.EConnect.Infrastructure; public static class LogExtensions { - public static async Task LogCurlAsync(this ILogger logger, HttpClient client, HttpMethod method, string url, HttpContent? content, LogLevel logLevel = LogLevel.Information) + public static async Task LogCurlAsync(this ILogger logger, HttpClient client, HttpMethod method, string? route = null, HttpContent? content = null, LogLevel logLevel = LogLevel.Information) { var sb = new StringBuilder(); sb.Append("curl"); @@ -13,29 +13,47 @@ public static class LogExtensions // Method sb.Append($" -X {method.Method}"); + // URL + var fullUrl = string.Empty; + if (client.BaseAddress is not null && route is not null) + fullUrl = new Uri(client.BaseAddress, route).ToString(); + else if (route is not null) + fullUrl = route; + else if (client.BaseAddress is not null) + fullUrl = client.BaseAddress.ToString(); + + if (!string.IsNullOrWhiteSpace(fullUrl)) + sb.AppendLine($" \"{fullUrl}\""); + // Headers foreach (var header in client.DefaultRequestHeaders) { - sb.Append($" -H \"{header.Key}: {string.Join(", ", header.Value)}\""); + sb.AppendLine($"\t-H \"{header.Key}: {string.Join(", ", header.Value)}\""); } if (content != null) { foreach (var header in content.Headers) { - sb.Append($" -H \"{header.Key}: {string.Join(", ", header.Value)}\""); + sb.AppendLine($"\t-H \"{header.Key}: {string.Join(", ", header.Value)}\""); } - var body = await content.ReadAsStringAsync(); - if (!string.IsNullOrWhiteSpace(body)) + var mediaType = content.Headers.ContentType?.MediaType; + if (mediaType is not null && mediaType.StartsWith("text") || mediaType!.Contains("json")) { - // Escape double quotes - body = body.Replace("\"", "\\\""); - sb.Append($" -d \"{body}\""); + var body = await content.ReadAsStringAsync(); + if (!string.IsNullOrWhiteSpace(body)) + { + // Escape double quotes + body = body.Replace("\"", "\\\""); + sb.AppendLine($"\t-d \"{body}\""); + } } + else + // Binary content, only placeholder in log + sb.AppendLine("\t-d \"\""); } - - sb.Append($" \"{url}\""); + logger.Log(logLevel, "{message}", sb.ToString()); } } \ No newline at end of file diff --git a/src/Leanetec.EConnect.Proxy/Controllers/OrderController.cs b/src/Leanetec.EConnect.Proxy/Controllers/OrderController.cs index e04b410..c337e94 100644 --- a/src/Leanetec.EConnect.Proxy/Controllers/OrderController.cs +++ b/src/Leanetec.EConnect.Proxy/Controllers/OrderController.cs @@ -20,13 +20,11 @@ public class OrderController : ControllerBase { var res = await _mediator.Send(request, cancel); if(res.Ok) - { - return res.Data is null || !res.Data.Any() ? NotFound() : Ok(res.Data); - } + return res.Data is null || !res.Data.Any() + ? NotFound() + : Ok(res.Data); else - { return StatusCode(res.StatusCodeInt, res?.Error); - } } [HttpPost("document")] @@ -40,13 +38,8 @@ public class OrderController : ControllerBase var res = await _mediator.Send(request.ToUploadDocument(streamContent), cancel); - if (res.Ok) - { - return StatusCode(res.StatusCodeInt); - } - else - { - return StatusCode(res.StatusCodeInt, res?.Error); - } + return res.Ok + ? StatusCode(res.StatusCodeInt) + : StatusCode(res.StatusCodeInt, res?.Error); } } \ No newline at end of file