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,
|
||||
serializer.Object,
|
||||
httpClient.Object,
|
||||
retryHandler.Object
|
||||
retryHandler.Object,
|
||||
null
|
||||
);
|
||||
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get);
|
||||
|
||||
@ -82,7 +83,8 @@ namespace SpotifyAPI.Web.Tests
|
||||
authenticator.Object,
|
||||
serializer.Object,
|
||||
httpClient.Object,
|
||||
retryHandler.Object
|
||||
retryHandler.Object,
|
||||
null
|
||||
);
|
||||
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get);
|
||||
|
||||
|
@ -9,6 +9,7 @@ namespace SpotifyAPI.Web
|
||||
public IAuthenticator Authenticator { get; }
|
||||
public IJSONSerializer JSONSerializer { get; }
|
||||
public IHTTPClient HTTPClient { get; }
|
||||
public IHTTPLogger HTTPLogger { get; }
|
||||
public IRetryHandler RetryHandler { get; }
|
||||
|
||||
/// <summary>
|
||||
@ -21,12 +22,14 @@ namespace SpotifyAPI.Web
|
||||
/// <param name="jsonSerializer"></param>
|
||||
/// <param name="httpClient"></param>
|
||||
/// <param name="retryHandler"></param>
|
||||
/// <param name="httpLogger"></param>
|
||||
public SpotifyClientConfig(
|
||||
Uri baseAddress,
|
||||
IAuthenticator authenticator,
|
||||
IJSONSerializer jsonSerializer,
|
||||
IHTTPClient httpClient,
|
||||
IRetryHandler retryHandler
|
||||
IRetryHandler retryHandler,
|
||||
IHTTPLogger httpLogger
|
||||
)
|
||||
{
|
||||
BaseAddress = baseAddress;
|
||||
@ -34,6 +37,7 @@ namespace SpotifyAPI.Web
|
||||
JSONSerializer = jsonSerializer;
|
||||
HTTPClient = httpClient;
|
||||
RetryHandler = retryHandler;
|
||||
HTTPLogger = httpLogger;
|
||||
}
|
||||
|
||||
internal IAPIConnector CreateAPIConnector()
|
||||
@ -44,7 +48,7 @@ namespace SpotifyAPI.Web
|
||||
Ensure.ArgumentNotNull(JSONSerializer, nameof(JSONSerializer));
|
||||
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")
|
||||
@ -56,12 +60,17 @@ namespace SpotifyAPI.Web
|
||||
|
||||
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)
|
||||
{
|
||||
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")
|
||||
@ -87,6 +96,7 @@ namespace SpotifyAPI.Web
|
||||
authenticator,
|
||||
new NewtonsoftJSONSerializer(),
|
||||
new NetHttpClient(),
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
@ -13,22 +13,25 @@ namespace SpotifyAPI.Web.Http
|
||||
private readonly IJSONSerializer _jsonSerializer;
|
||||
private readonly IHTTPClient _httpClient;
|
||||
private readonly IRetryHandler _retryHandler;
|
||||
private readonly IHTTPLogger _httpLogger;
|
||||
|
||||
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(
|
||||
Uri baseAddress,
|
||||
IAuthenticator authenticator,
|
||||
IJSONSerializer jsonSerializer,
|
||||
IHTTPClient httpClient,
|
||||
IRetryHandler retryHandler)
|
||||
IRetryHandler retryHandler,
|
||||
IHTTPLogger httpLogger)
|
||||
{
|
||||
_baseAddress = baseAddress;
|
||||
_authenticator = authenticator;
|
||||
_jsonSerializer = jsonSerializer;
|
||||
_httpClient = httpClient;
|
||||
_retryHandler = retryHandler;
|
||||
_httpLogger = httpLogger;
|
||||
}
|
||||
|
||||
public Task<T> Delete<T>(Uri uri)
|
||||
@ -159,13 +162,17 @@ namespace SpotifyAPI.Web.Http
|
||||
private async Task<IResponse> DoRequest(IRequest request)
|
||||
{
|
||||
await _authenticator.Apply(request).ConfigureAwait(false);
|
||||
_httpLogger?.OnRequest(request);
|
||||
IResponse response = await _httpClient.DoRequest(request).ConfigureAwait(false);
|
||||
_httpLogger?.OnResponse(response);
|
||||
if (_retryHandler != null)
|
||||
{
|
||||
response = await _retryHandler?.HandleRetry(request, response, async (newRequest) =>
|
||||
{
|
||||
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);
|
||||
|
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