Skip to content

Commit

Permalink
log response body
Browse files Browse the repository at this point in the history
  • Loading branch information
zivillian committed Aug 10, 2024
1 parent 45b0aae commit 93fdc60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 16 additions & 3 deletions libgwmapi/GwmApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;

namespace libgwmapi;

Expand All @@ -10,14 +11,16 @@ public partial class GwmApiClient
public static readonly string AppHttpClientName = "eu-app-gateway";
private readonly HttpClient _h5Client;
private readonly HttpClient _appClient;
private readonly ILogger<GwmApiClient> _logger;

public GwmApiClient(IHttpClientFactory factory)
: this(factory.CreateClient(H5HttpClientName), factory.CreateClient(AppHttpClientName))
public GwmApiClient(IHttpClientFactory factory, ILoggerFactory loggerFactory)
: this(factory.CreateClient(H5HttpClientName), factory.CreateClient(AppHttpClientName), loggerFactory)
{
}

public GwmApiClient(HttpClient h5Client, HttpClient appClient)
public GwmApiClient(HttpClient h5Client, HttpClient appClient, ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<GwmApiClient>();
_h5Client = h5Client;
_h5Client.DefaultRequestHeaders.Add("rs", "2");
_h5Client.DefaultRequestHeaders.Add("terminal", "GW_APP_ORA");
Expand Down Expand Up @@ -107,13 +110,23 @@ private async Task<T> GetAppAsync<T>(string url, CancellationToken cancellationT

private async Task CheckResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken)
{
if (_logger.IsEnabled(LogLevel.Trace))
{
await response.Content.LoadIntoBufferAsync();
_logger.LogTrace(await response.Content.ReadAsStringAsync(cancellationToken));
}
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<GwmResponse>(cancellationToken: cancellationToken);
CheckResponse(result);
}

private async Task<T> GetResponseAsync<T>(HttpResponseMessage response, CancellationToken cancellationToken)
{
if (_logger.IsEnabled(LogLevel.Trace))
{
await response.Content.LoadIntoBufferAsync();
_logger.LogTrace(await response.Content.ReadAsStringAsync(cancellationToken));
}
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<GwmResponse<T>>(cancellationToken: cancellationToken);
CheckResponse(result);
Expand Down
9 changes: 7 additions & 2 deletions ora2mqtt/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using libgwmapi;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Http.Logging;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
Expand Down Expand Up @@ -52,15 +53,19 @@ protected GwmApiClient ConfigureApiClient(Ora2MqttOptions options)
}

var httpLogger = LoggerFactory.CreateLogger<HttpClient>();
var h5Client = new HttpClient(new LoggingHttpMessageHandler(httpLogger)
var httpOptions = new HttpClientFactoryOptions
{
ShouldRedactHeaderValue = x => "accessToken".Equals(x, StringComparison.InvariantCultureIgnoreCase)
};
var h5Client = new HttpClient(new LoggingHttpMessageHandler(httpLogger, httpOptions)
{
InnerHandler = new HttpClientHandler()
});
var appClient = new HttpClient(new LoggingHttpMessageHandler(httpLogger)
{
InnerHandler = httpHandler
});
return new GwmApiClient(h5Client, appClient)
return new GwmApiClient(h5Client, appClient, LoggerFactory)
{
Country = options.Country
};
Expand Down

0 comments on commit 93fdc60

Please sign in to comment.