mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-24 06:56:27 +00:00
Added Logger interface & Simple Logger implementation
This commit is contained in:
parent
813bba896f
commit
7310c9bc8d
@ -41,7 +41,8 @@ namespace SpotifyAPI.Web.Tests
|
|||||||
authenticator.Object,
|
authenticator.Object,
|
||||||
serializer.Object,
|
serializer.Object,
|
||||||
httpClient.Object,
|
httpClient.Object,
|
||||||
retryHandler.Object
|
retryHandler.Object,
|
||||||
|
null
|
||||||
);
|
);
|
||||||
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get);
|
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get);
|
||||||
|
|
||||||
@ -82,7 +83,8 @@ namespace SpotifyAPI.Web.Tests
|
|||||||
authenticator.Object,
|
authenticator.Object,
|
||||||
serializer.Object,
|
serializer.Object,
|
||||||
httpClient.Object,
|
httpClient.Object,
|
||||||
retryHandler.Object
|
retryHandler.Object,
|
||||||
|
null
|
||||||
);
|
);
|
||||||
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get);
|
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get);
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ namespace SpotifyAPI.Web
|
|||||||
public IAuthenticator Authenticator { get; }
|
public IAuthenticator Authenticator { get; }
|
||||||
public IJSONSerializer JSONSerializer { get; }
|
public IJSONSerializer JSONSerializer { get; }
|
||||||
public IHTTPClient HTTPClient { get; }
|
public IHTTPClient HTTPClient { get; }
|
||||||
|
public IHTTPLogger HTTPLogger { get; }
|
||||||
public IRetryHandler RetryHandler { get; }
|
public IRetryHandler RetryHandler { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -21,12 +22,14 @@ namespace SpotifyAPI.Web
|
|||||||
/// <param name="jsonSerializer"></param>
|
/// <param name="jsonSerializer"></param>
|
||||||
/// <param name="httpClient"></param>
|
/// <param name="httpClient"></param>
|
||||||
/// <param name="retryHandler"></param>
|
/// <param name="retryHandler"></param>
|
||||||
|
/// <param name="httpLogger"></param>
|
||||||
public SpotifyClientConfig(
|
public SpotifyClientConfig(
|
||||||
Uri baseAddress,
|
Uri baseAddress,
|
||||||
IAuthenticator authenticator,
|
IAuthenticator authenticator,
|
||||||
IJSONSerializer jsonSerializer,
|
IJSONSerializer jsonSerializer,
|
||||||
IHTTPClient httpClient,
|
IHTTPClient httpClient,
|
||||||
IRetryHandler retryHandler
|
IRetryHandler retryHandler,
|
||||||
|
IHTTPLogger httpLogger
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
BaseAddress = baseAddress;
|
BaseAddress = baseAddress;
|
||||||
@ -34,6 +37,7 @@ namespace SpotifyAPI.Web
|
|||||||
JSONSerializer = jsonSerializer;
|
JSONSerializer = jsonSerializer;
|
||||||
HTTPClient = httpClient;
|
HTTPClient = httpClient;
|
||||||
RetryHandler = retryHandler;
|
RetryHandler = retryHandler;
|
||||||
|
HTTPLogger = httpLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IAPIConnector CreateAPIConnector()
|
internal IAPIConnector CreateAPIConnector()
|
||||||
@ -44,7 +48,7 @@ namespace SpotifyAPI.Web
|
|||||||
Ensure.ArgumentNotNull(JSONSerializer, nameof(JSONSerializer));
|
Ensure.ArgumentNotNull(JSONSerializer, nameof(JSONSerializer));
|
||||||
Ensure.ArgumentNotNull(HTTPClient, nameof(HTTPClient));
|
Ensure.ArgumentNotNull(HTTPClient, nameof(HTTPClient));
|
||||||
|
|
||||||
return new APIConnector(BaseAddress, Authenticator, JSONSerializer, HTTPClient, RetryHandler);
|
return new APIConnector(BaseAddress, Authenticator, JSONSerializer, HTTPClient, RetryHandler, HTTPLogger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpotifyClientConfig WithToken(string token, string tokenType = "Bearer")
|
public SpotifyClientConfig WithToken(string token, string tokenType = "Bearer")
|
||||||
@ -56,12 +60,17 @@ namespace SpotifyAPI.Web
|
|||||||
|
|
||||||
public SpotifyClientConfig WithRetryHandler(IRetryHandler retryHandler)
|
public SpotifyClientConfig WithRetryHandler(IRetryHandler retryHandler)
|
||||||
{
|
{
|
||||||
return new SpotifyClientConfig(BaseAddress, Authenticator, JSONSerializer, HTTPClient, retryHandler);
|
return new SpotifyClientConfig(BaseAddress, Authenticator, JSONSerializer, HTTPClient, retryHandler, HTTPLogger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpotifyClientConfig WithAuthenticator(IAuthenticator authenticator)
|
public SpotifyClientConfig WithAuthenticator(IAuthenticator authenticator)
|
||||||
{
|
{
|
||||||
return new SpotifyClientConfig(BaseAddress, authenticator, JSONSerializer, HTTPClient, RetryHandler);
|
return new SpotifyClientConfig(BaseAddress, authenticator, JSONSerializer, HTTPClient, RetryHandler, HTTPLogger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpotifyClientConfig WithHTTPLogger(IHTTPLogger httpLogger)
|
||||||
|
{
|
||||||
|
return new SpotifyClientConfig(BaseAddress, Authenticator, JSONSerializer, HTTPClient, RetryHandler, httpLogger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SpotifyClientConfig CreateDefault(string token, string tokenType = "Bearer")
|
public static SpotifyClientConfig CreateDefault(string token, string tokenType = "Bearer")
|
||||||
@ -87,6 +96,7 @@ namespace SpotifyAPI.Web
|
|||||||
authenticator,
|
authenticator,
|
||||||
new NewtonsoftJSONSerializer(),
|
new NewtonsoftJSONSerializer(),
|
||||||
new NetHttpClient(),
|
new NetHttpClient(),
|
||||||
|
null,
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -13,22 +13,25 @@ namespace SpotifyAPI.Web.Http
|
|||||||
private readonly IJSONSerializer _jsonSerializer;
|
private readonly IJSONSerializer _jsonSerializer;
|
||||||
private readonly IHTTPClient _httpClient;
|
private readonly IHTTPClient _httpClient;
|
||||||
private readonly IRetryHandler _retryHandler;
|
private readonly IRetryHandler _retryHandler;
|
||||||
|
private readonly IHTTPLogger _httpLogger;
|
||||||
|
|
||||||
public APIConnector(Uri baseAddress, IAuthenticator authenticator) :
|
public APIConnector(Uri baseAddress, IAuthenticator authenticator) :
|
||||||
this(baseAddress, authenticator, new NewtonsoftJSONSerializer(), new NetHttpClient(), null)
|
this(baseAddress, authenticator, new NewtonsoftJSONSerializer(), new NetHttpClient(), null, null)
|
||||||
{ }
|
{ }
|
||||||
public APIConnector(
|
public APIConnector(
|
||||||
Uri baseAddress,
|
Uri baseAddress,
|
||||||
IAuthenticator authenticator,
|
IAuthenticator authenticator,
|
||||||
IJSONSerializer jsonSerializer,
|
IJSONSerializer jsonSerializer,
|
||||||
IHTTPClient httpClient,
|
IHTTPClient httpClient,
|
||||||
IRetryHandler retryHandler)
|
IRetryHandler retryHandler,
|
||||||
|
IHTTPLogger httpLogger)
|
||||||
{
|
{
|
||||||
_baseAddress = baseAddress;
|
_baseAddress = baseAddress;
|
||||||
_authenticator = authenticator;
|
_authenticator = authenticator;
|
||||||
_jsonSerializer = jsonSerializer;
|
_jsonSerializer = jsonSerializer;
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
_retryHandler = retryHandler;
|
_retryHandler = retryHandler;
|
||||||
|
_httpLogger = httpLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<T> Delete<T>(Uri uri)
|
public Task<T> Delete<T>(Uri uri)
|
||||||
@ -159,13 +162,17 @@ namespace SpotifyAPI.Web.Http
|
|||||||
private async Task<IResponse> DoRequest(IRequest request)
|
private async Task<IResponse> DoRequest(IRequest request)
|
||||||
{
|
{
|
||||||
await _authenticator.Apply(request).ConfigureAwait(false);
|
await _authenticator.Apply(request).ConfigureAwait(false);
|
||||||
|
_httpLogger?.OnRequest(request);
|
||||||
IResponse response = await _httpClient.DoRequest(request).ConfigureAwait(false);
|
IResponse response = await _httpClient.DoRequest(request).ConfigureAwait(false);
|
||||||
|
_httpLogger?.OnResponse(response);
|
||||||
if (_retryHandler != null)
|
if (_retryHandler != null)
|
||||||
{
|
{
|
||||||
response = await _retryHandler?.HandleRetry(request, response, async (newRequest) =>
|
response = await _retryHandler?.HandleRetry(request, response, async (newRequest) =>
|
||||||
{
|
{
|
||||||
await _authenticator.Apply(newRequest).ConfigureAwait(false);
|
await _authenticator.Apply(newRequest).ConfigureAwait(false);
|
||||||
return await _httpClient.DoRequest(request).ConfigureAwait(false);
|
var newResponse = await _httpClient.DoRequest(request).ConfigureAwait(false);
|
||||||
|
_httpLogger?.OnResponse(newResponse);
|
||||||
|
return newResponse;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ProcessErrors(response);
|
ProcessErrors(response);
|
||||||
|
8
SpotifyAPI.Web/Http/Interfaces/IHTTPLogger.cs
Normal file
8
SpotifyAPI.Web/Http/Interfaces/IHTTPLogger.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace SpotifyAPI.Web.Http
|
||||||
|
{
|
||||||
|
public interface IHTTPLogger
|
||||||
|
{
|
||||||
|
void OnRequest(IRequest request);
|
||||||
|
void OnResponse(IResponse response);
|
||||||
|
}
|
||||||
|
}
|
24
SpotifyAPI.Web/Http/SimpleHTTPLogger.cs
Normal file
24
SpotifyAPI.Web/Http/SimpleHTTPLogger.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SpotifyAPI.Web.Http
|
||||||
|
{
|
||||||
|
public class SimpleHTTPLogger : IHTTPLogger
|
||||||
|
{
|
||||||
|
public void OnRequest(IRequest request)
|
||||||
|
{
|
||||||
|
string parameters = null;
|
||||||
|
if (request.Parameters != null)
|
||||||
|
{
|
||||||
|
parameters = string.Join(",", request.Parameters?.Select(kv => kv.Key + "=" + kv.Value).ToArray());
|
||||||
|
}
|
||||||
|
Console.WriteLine("\n{0} {1} [{2}] {3}", request.Method, request.Endpoint, parameters, request.Body);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnResponse(IResponse response)
|
||||||
|
{
|
||||||
|
string body = response.Body?.ToString().Substring(0, 50).Replace("\n", "");
|
||||||
|
Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user