mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add initial project structure and configuration files for Marechai.App
This commit is contained in:
50
Marechai.App/Services/Endpoints/DebugHandler.cs
Normal file
50
Marechai.App/Services/Endpoints/DebugHandler.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Uno.Extensions.Logging;
|
||||
|
||||
namespace Marechai.App.Services.Endpoints;
|
||||
|
||||
internal class DebugHttpHandler : DelegatingHandler
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public DebugHttpHandler(ILogger<DebugHttpHandler> logger, HttpMessageHandler? innerHandler = null)
|
||||
: base(innerHandler ?? new HttpClientHandler())
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected async override Task<HttpResponseMessage> SendAsync(
|
||||
HttpRequestMessage request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await base.SendAsync(request, cancellationToken);
|
||||
#if DEBUG
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogDebugMessage("Unsuccessful API Call");
|
||||
if (request.RequestUri is not null)
|
||||
{
|
||||
_logger.LogDebugMessage($"{request.RequestUri} ({request.Method})");
|
||||
}
|
||||
|
||||
foreach ((var key, var values) in request.Headers.ToDictionary(x => x.Key, x => string.Join(", ", x.Value)))
|
||||
{
|
||||
_logger.LogDebugMessage($"{key}: {values}");
|
||||
}
|
||||
|
||||
var content = request.Content is not null ? await request.Content.ReadAsStringAsync() : null;
|
||||
if (!string.IsNullOrEmpty(content))
|
||||
{
|
||||
_logger.LogDebugMessage(content);
|
||||
}
|
||||
|
||||
// Uncomment to automatically break when an API call fails while debugging
|
||||
// System.Diagnostics.Debugger.Break();
|
||||
}
|
||||
#endif
|
||||
return response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user