Skip to content

Commit 777b7f9

Browse files
committed
HACK: Adjust generated code to log HTTP requests/responses
1 parent 194dd6d commit 777b7f9

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using JetBrains.Annotations;
2+
3+
namespace JsonApiDotNetCoreClientExample;
4+
5+
/// <summary>
6+
/// Writes incoming and outgoing HTTP messages to the console.
7+
/// </summary>
8+
internal sealed class ColoredConsoleLogHttpMessageHandler : DelegatingHandler
9+
{
10+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
11+
{
12+
#if DEBUG
13+
await LogRequestAsync(request, cancellationToken);
14+
#endif
15+
16+
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
17+
18+
#if DEBUG
19+
await LogResponseAsync(response, cancellationToken);
20+
#endif
21+
22+
return response;
23+
}
24+
25+
[UsedImplicitly]
26+
private static async Task LogRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
27+
{
28+
using var _ = new ConsoleColorScope(ConsoleColor.Green);
29+
30+
Console.WriteLine($"--> {request}");
31+
string? requestBody = request.Content != null ? await request.Content.ReadAsStringAsync(cancellationToken) : null;
32+
33+
if (!string.IsNullOrEmpty(requestBody))
34+
{
35+
Console.WriteLine();
36+
Console.WriteLine(requestBody);
37+
}
38+
}
39+
40+
[UsedImplicitly]
41+
private static async Task LogResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken)
42+
{
43+
using var _ = new ConsoleColorScope(ConsoleColor.Cyan);
44+
45+
Console.WriteLine($"<-- {response}");
46+
string responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
47+
48+
if (!string.IsNullOrEmpty(responseBody))
49+
{
50+
Console.WriteLine();
51+
Console.WriteLine(responseBody);
52+
}
53+
}
54+
55+
private sealed class ConsoleColorScope : IDisposable
56+
{
57+
public ConsoleColorScope(ConsoleColor foregroundColor)
58+
{
59+
Console.ForegroundColor = foregroundColor;
60+
}
61+
62+
public void Dispose()
63+
{
64+
Console.ResetColor();
65+
}
66+
}
67+
}

src/Examples/OpenApiLiblabClientExample/output/csharp/JsonApiDotNetCoreClientExample/JsonApiDotNetCoreClientExampleClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public class JsonApiDotNetCoreClientExampleClient : IDisposable
1818

1919
public JsonApiDotNetCoreClientExampleClient(JsonApiDotNetCoreClientExampleConfig? config = null)
2020
{
21-
var retryHandler = new RetryHandler();
21+
// HACK: Enable logging of HTTP requests and responses.
22+
//var retryHandler = new RetryHandler();
23+
var retryHandler = new RetryHandler(new ColoredConsoleLogHttpMessageHandler { InnerHandler = new SocketsHttpHandler() });
24+
2225
_accessTokenHandler = new TokenHandler(retryHandler)
2326
{
2427
Header = "Authorization",

0 commit comments

Comments
 (0)