Skip to content

Commit 7d889dd

Browse files
committed
Example: Use http handler for writing to the console
1 parent e27c98f commit 7d889dd

File tree

3 files changed

+69
-47
lines changed

3 files changed

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

src/Examples/JsonApiDotNetCoreExampleClient/ExampleApiClient.cs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using JsonApiDotNetCore.OpenApi.Client;
33
using Newtonsoft.Json;
44

5-
// ReSharper disable UnusedParameterInPartialMethod
6-
75
namespace JsonApiDotNetCoreExampleClient;
86

97
[UsedImplicitly(ImplicitUseTargetFlags.Itself)]
@@ -13,50 +11,8 @@ partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings)
1311
{
1412
SetSerializerSettingsForJsonApi(settings);
1513

16-
// Optional: Makes the JSON easier to read when logged.
14+
#if DEBUG
1715
settings.Formatting = Formatting.Indented;
18-
}
19-
20-
// Optional: Log outgoing request to the console.
21-
partial void PrepareRequest(HttpClient client, HttpRequestMessage request, string url)
22-
{
23-
using var _ = new UsingConsoleColor(ConsoleColor.Green);
24-
25-
Console.WriteLine($"--> {request}");
26-
string? requestBody = request.Content?.ReadAsStringAsync().GetAwaiter().GetResult();
27-
28-
if (!string.IsNullOrEmpty(requestBody))
29-
{
30-
Console.WriteLine();
31-
Console.WriteLine(requestBody);
32-
}
33-
}
34-
35-
// Optional: Log incoming response to the console.
36-
partial void ProcessResponse(HttpClient client, HttpResponseMessage response)
37-
{
38-
using var _ = new UsingConsoleColor(ConsoleColor.Cyan);
39-
40-
Console.WriteLine($"<-- {response}");
41-
string responseBody = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
42-
43-
if (!string.IsNullOrEmpty(responseBody))
44-
{
45-
Console.WriteLine();
46-
Console.WriteLine(responseBody);
47-
}
48-
}
49-
50-
private sealed class UsingConsoleColor : IDisposable
51-
{
52-
public UsingConsoleColor(ConsoleColor foregroundColor)
53-
{
54-
Console.ForegroundColor = foregroundColor;
55-
}
56-
57-
public void Dispose()
58-
{
59-
Console.ResetColor();
60-
}
16+
#endif
6117
}
6218
}

src/Examples/JsonApiDotNetCoreExampleClient/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using JsonApiDotNetCoreExampleClient;
22

3-
using var httpClient = new HttpClient();
3+
using var httpClient = new HttpClient(new ColoredConsoleLogDelegatingHandler
4+
{
5+
InnerHandler = new HttpClientHandler()
6+
});
7+
48
var apiClient = new ExampleApiClient(httpClient);
59

610
PersonCollectionResponseDocument getResponse = await apiClient.GetPersonCollectionAsync(new Dictionary<string, string?>

0 commit comments

Comments
 (0)