2020-05-02 12:04:26 +01:00
|
|
|
using System;
|
|
|
|
using SpotifyAPI.Web.Http;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web
|
|
|
|
{
|
|
|
|
public class SpotifyClientConfig
|
|
|
|
{
|
|
|
|
public Uri BaseAddress { get; }
|
|
|
|
public IAuthenticator Authenticator { get; }
|
|
|
|
public IJSONSerializer JSONSerializer { get; }
|
|
|
|
public IHTTPClient HTTPClient { get; }
|
2020-05-03 12:00:50 +01:00
|
|
|
public IHTTPLogger HTTPLogger { get; }
|
2020-05-02 18:57:31 +01:00
|
|
|
public IRetryHandler RetryHandler { get; }
|
2020-05-02 12:04:26 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This config spefies the internal parts of the SpotifyClient.
|
|
|
|
/// In apps where multiple different access tokens are used, one should create a default config and then use
|
|
|
|
/// <see cref="WithToken" /> or <see cref="WithAuthenticator" /> to specify the auth details.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="baseAddress"></param>
|
|
|
|
/// <param name="authenticator"></param>
|
|
|
|
/// <param name="jsonSerializer"></param>
|
|
|
|
/// <param name="httpClient"></param>
|
2020-05-02 18:57:31 +01:00
|
|
|
/// <param name="retryHandler"></param>
|
2020-05-03 12:00:50 +01:00
|
|
|
/// <param name="httpLogger"></param>
|
2020-05-02 12:04:26 +01:00
|
|
|
public SpotifyClientConfig(
|
|
|
|
Uri baseAddress,
|
|
|
|
IAuthenticator authenticator,
|
|
|
|
IJSONSerializer jsonSerializer,
|
2020-05-02 18:57:31 +01:00
|
|
|
IHTTPClient httpClient,
|
2020-05-03 12:00:50 +01:00
|
|
|
IRetryHandler retryHandler,
|
|
|
|
IHTTPLogger httpLogger
|
2020-05-02 12:04:26 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
BaseAddress = baseAddress;
|
|
|
|
Authenticator = authenticator;
|
|
|
|
JSONSerializer = jsonSerializer;
|
|
|
|
HTTPClient = httpClient;
|
2020-05-02 18:57:31 +01:00
|
|
|
RetryHandler = retryHandler;
|
2020-05-03 12:00:50 +01:00
|
|
|
HTTPLogger = httpLogger;
|
2020-05-02 12:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
internal IAPIConnector CreateAPIConnector()
|
|
|
|
{
|
2020-05-02 21:48:21 +01:00
|
|
|
Ensure.ArgumentNotNull(BaseAddress, nameof(BaseAddress));
|
2020-05-03 21:34:03 +01:00
|
|
|
Ensure.ArgumentNotNull(Authenticator, nameof(Authenticator));
|
2020-05-02 21:48:21 +01:00
|
|
|
Ensure.ArgumentNotNull(JSONSerializer, nameof(JSONSerializer));
|
|
|
|
Ensure.ArgumentNotNull(HTTPClient, nameof(HTTPClient));
|
2020-05-02 12:04:26 +01:00
|
|
|
|
2020-05-03 12:00:50 +01:00
|
|
|
return new APIConnector(BaseAddress, Authenticator, JSONSerializer, HTTPClient, RetryHandler, HTTPLogger);
|
2020-05-02 12:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public SpotifyClientConfig WithToken(string token, string tokenType = "Bearer")
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(token, nameof(token));
|
|
|
|
|
|
|
|
return WithAuthenticator(new TokenHeaderAuthenticator(token, tokenType));
|
|
|
|
}
|
|
|
|
|
2020-05-02 18:57:31 +01:00
|
|
|
public SpotifyClientConfig WithRetryHandler(IRetryHandler retryHandler)
|
2020-05-02 12:04:26 +01:00
|
|
|
{
|
2020-05-03 12:00:50 +01:00
|
|
|
return new SpotifyClientConfig(BaseAddress, Authenticator, JSONSerializer, HTTPClient, retryHandler, HTTPLogger);
|
2020-05-02 18:57:31 +01:00
|
|
|
}
|
2020-05-02 12:04:26 +01:00
|
|
|
|
2020-05-02 18:57:31 +01:00
|
|
|
public SpotifyClientConfig WithAuthenticator(IAuthenticator authenticator)
|
|
|
|
{
|
2020-05-03 12:00:50 +01:00
|
|
|
return new SpotifyClientConfig(BaseAddress, authenticator, JSONSerializer, HTTPClient, RetryHandler, HTTPLogger);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SpotifyClientConfig WithHTTPLogger(IHTTPLogger httpLogger)
|
|
|
|
{
|
|
|
|
return new SpotifyClientConfig(BaseAddress, Authenticator, JSONSerializer, HTTPClient, RetryHandler, httpLogger);
|
2020-05-02 12:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static SpotifyClientConfig CreateDefault(string token, string tokenType = "Bearer")
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(token, nameof(token));
|
|
|
|
|
|
|
|
return CreateDefault(new TokenHeaderAuthenticator(token, tokenType));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a default configuration, which is not useable without calling <see cref="WithToken" /> or
|
|
|
|
/// <see cref="WithAuthenticator" />
|
|
|
|
/// </summary>
|
|
|
|
public static SpotifyClientConfig CreateDefault()
|
|
|
|
{
|
|
|
|
return CreateDefault(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SpotifyClientConfig CreateDefault(IAuthenticator authenticator)
|
|
|
|
{
|
|
|
|
return new SpotifyClientConfig(
|
|
|
|
SpotifyUrls.API_V1,
|
|
|
|
authenticator,
|
|
|
|
new NewtonsoftJSONSerializer(),
|
2020-05-02 18:57:31 +01:00
|
|
|
new NetHttpClient(),
|
2020-05-03 12:00:50 +01:00
|
|
|
null,
|
2020-05-02 18:57:31 +01:00
|
|
|
null
|
2020-05-02 12:04:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|