2020-05-03 12:00:50 +01:00
|
|
|
using System;
|
2022-11-27 12:29:35 +00:00
|
|
|
using System.Linq;
|
2020-05-03 12:00:50 +01:00
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Http
|
|
|
|
{
|
2020-05-30 23:11:05 +01:00
|
|
|
public class SimpleConsoleHTTPLogger : IHTTPLogger
|
2020-05-03 12:00:50 +01:00
|
|
|
{
|
2020-05-05 14:30:00 +01:00
|
|
|
private const string OnRequestFormat = "\n{0} {1} [{2}] {3}";
|
|
|
|
|
2020-05-03 12:00:50 +01:00
|
|
|
public void OnRequest(IRequest request)
|
|
|
|
{
|
2020-05-05 14:30:00 +01:00
|
|
|
Ensure.ArgumentNotNull(request, nameof(request));
|
|
|
|
|
2020-05-25 17:00:38 +01:00
|
|
|
string? parameters = null;
|
2020-05-03 12:00:50 +01:00
|
|
|
if (request.Parameters != null)
|
|
|
|
{
|
2020-11-13 13:43:00 +00:00
|
|
|
parameters = string.Join(",",
|
2020-12-26 17:28:44 +00:00
|
|
|
request.Parameters.Select(kv => kv.Key + "=" + kv.Value)?.ToArray() ?? Array.Empty<string>()
|
2020-11-13 13:43:00 +00:00
|
|
|
);
|
2020-05-03 12:00:50 +01:00
|
|
|
}
|
2020-05-05 14:30:00 +01:00
|
|
|
|
|
|
|
Console.WriteLine(OnRequestFormat, request.Method, request.Endpoint, parameters, request.Body);
|
2020-05-03 12:00:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void OnResponse(IResponse response)
|
|
|
|
{
|
2020-05-05 14:30:00 +01:00
|
|
|
Ensure.ArgumentNotNull(response, nameof(response));
|
2023-11-03 22:23:54 +00:00
|
|
|
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
|
2020-11-13 13:43:00 +00:00
|
|
|
string? body = response.Body?.ToString()?.Replace("\n", "", StringComparison.InvariantCulture);
|
2023-11-03 22:23:54 +00:00
|
|
|
#else
|
|
|
|
string? body = response.Body?.ToString()?.Replace("\n", "");
|
|
|
|
#endif
|
2020-12-26 17:28:44 +00:00
|
|
|
body = body?.Substring(0, Math.Min(50, body.Length));
|
2020-05-03 12:00:50 +01:00
|
|
|
Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|