Skip to content

Commit 2989090

Browse files
committed
Example: Use http handler for writing to the console
1 parent 1200e59 commit 2989090

File tree

3 files changed

+66
-47
lines changed

3 files changed

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

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)