diff --git a/SpotifyAPI.Web.Tests/Http/APIConnectorTest.cs b/SpotifyAPI.Web.Tests/Http/APIConnectorTest.cs index 44b83f97..fee42f62 100644 --- a/SpotifyAPI.Web.Tests/Http/APIConnectorTest.cs +++ b/SpotifyAPI.Web.Tests/Http/APIConnectorTest.cs @@ -41,7 +41,8 @@ namespace SpotifyAPI.Web.Tests authenticator.Object, serializer.Object, httpClient.Object, - retryHandler.Object + retryHandler.Object, + null ); await apiConnector.SendAPIRequest(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(new Uri("/me", UriKind.Relative), HttpMethod.Get); diff --git a/SpotifyAPI.Web/Clients/SpotifyClientConfig.cs b/SpotifyAPI.Web/Clients/SpotifyClientConfig.cs index 2ad56509..d1969215 100644 --- a/SpotifyAPI.Web/Clients/SpotifyClientConfig.cs +++ b/SpotifyAPI.Web/Clients/SpotifyClientConfig.cs @@ -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; } /// @@ -21,12 +22,14 @@ namespace SpotifyAPI.Web /// /// /// + /// 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 ); } diff --git a/SpotifyAPI.Web/Http/APIConnector.cs b/SpotifyAPI.Web/Http/APIConnector.cs index 8d146c19..c349731c 100644 --- a/SpotifyAPI.Web/Http/APIConnector.cs +++ b/SpotifyAPI.Web/Http/APIConnector.cs @@ -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 Delete(Uri uri) @@ -159,13 +162,17 @@ namespace SpotifyAPI.Web.Http private async Task 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); diff --git a/SpotifyAPI.Web/Http/Interfaces/IHTTPLogger.cs b/SpotifyAPI.Web/Http/Interfaces/IHTTPLogger.cs new file mode 100644 index 00000000..48525cc6 --- /dev/null +++ b/SpotifyAPI.Web/Http/Interfaces/IHTTPLogger.cs @@ -0,0 +1,8 @@ +namespace SpotifyAPI.Web.Http +{ + public interface IHTTPLogger + { + void OnRequest(IRequest request); + void OnResponse(IResponse response); + } +} diff --git a/SpotifyAPI.Web/Http/SimpleHTTPLogger.cs b/SpotifyAPI.Web/Http/SimpleHTTPLogger.cs new file mode 100644 index 00000000..bf3a79be --- /dev/null +++ b/SpotifyAPI.Web/Http/SimpleHTTPLogger.cs @@ -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); + } + } +}