Spotify.NET/SpotifyAPI.Web/Clients/SpotifyClientConfig.cs

167 lines
4.3 KiB
C#
Raw Normal View History

2020-05-12 15:35:59 +01:00
using System.Net.Http;
using System;
using SpotifyAPI.Web.Http;
namespace SpotifyAPI.Web
{
public class SpotifyClientConfig
{
public Uri BaseAddress { get; private set; }
2020-05-25 17:00:38 +01:00
public IAuthenticator? Authenticator { get; private set; }
public IJSONSerializer JSONSerializer { get; private set; }
public IHTTPClient HTTPClient { get; private set; }
2020-05-25 17:00:38 +01:00
public IHTTPLogger? HTTPLogger { get; private set; }
public IRetryHandler? RetryHandler { get; private set; }
public IPaginator DefaultPaginator { get; private set; }
/// <summary>
/// This config spefies the internal parts of the SpotifyClient.
/// </summary>
/// <param name="baseAddress"></param>
/// <param name="authenticator"></param>
/// <param name="jsonSerializer"></param>
/// <param name="httpClient"></param>
/// <param name="retryHandler"></param>
/// <param name="httpLogger"></param>
2020-05-12 19:33:25 +01:00
/// <param name="defaultPaginator"></param>
public SpotifyClientConfig(
Uri baseAddress,
2020-05-25 17:00:38 +01:00
IAuthenticator? authenticator,
IJSONSerializer jsonSerializer,
IHTTPClient httpClient,
2020-05-25 17:00:38 +01:00
IRetryHandler? retryHandler,
IHTTPLogger? httpLogger,
2020-05-12 19:33:25 +01:00
IPaginator defaultPaginator
)
{
BaseAddress = baseAddress;
Authenticator = authenticator;
JSONSerializer = jsonSerializer;
HTTPClient = httpClient;
RetryHandler = retryHandler;
HTTPLogger = httpLogger;
2020-05-12 19:33:25 +01:00
DefaultPaginator = defaultPaginator;
}
2020-05-12 15:35:59 +01:00
public SpotifyClientConfig WithToken(string token, string tokenType = "Bearer")
{
2020-05-12 15:35:59 +01:00
Ensure.ArgumentNotNull(token, nameof(token));
return new SpotifyClientConfig(
BaseAddress,
new TokenAuthenticator(token, tokenType),
JSONSerializer,
HTTPClient,
RetryHandler,
2020-05-12 15:35:59 +01:00
HTTPLogger,
DefaultPaginator
);
}
2020-05-12 15:35:59 +01:00
public SpotifyClientConfig WithRetryHandler(IRetryHandler retryHandler)
{
2020-05-12 15:35:59 +01:00
return new SpotifyClientConfig(
BaseAddress,
Authenticator,
JSONSerializer,
HTTPClient,
retryHandler,
HTTPLogger,
DefaultPaginator
);
}
2020-05-12 15:35:59 +01:00
public SpotifyClientConfig WithAuthenticator(IAuthenticator authenticator)
{
Ensure.ArgumentNotNull(authenticator, nameof(authenticator));
2020-05-12 15:35:59 +01:00
return new SpotifyClientConfig(
BaseAddress,
authenticator,
JSONSerializer,
HTTPClient,
RetryHandler,
HTTPLogger,
DefaultPaginator
);
}
2020-05-12 15:35:59 +01:00
public SpotifyClientConfig WithHTTPLogger(IHTTPLogger httpLogger)
{
2020-05-12 15:35:59 +01:00
return new SpotifyClientConfig(
BaseAddress,
Authenticator,
JSONSerializer,
HTTPClient,
RetryHandler,
httpLogger,
DefaultPaginator
);
}
2020-05-12 15:35:59 +01:00
public SpotifyClientConfig WithHTTPClient(IHTTPClient httpClient)
{
Ensure.ArgumentNotNull(httpClient, nameof(httpClient));
2020-05-12 15:35:59 +01:00
return new SpotifyClientConfig(
BaseAddress,
Authenticator,
JSONSerializer,
httpClient,
RetryHandler,
HTTPLogger,
DefaultPaginator
);
}
2020-05-12 15:35:59 +01:00
public SpotifyClientConfig WithJSONSerializer(IJSONSerializer jsonSerializer)
{
Ensure.ArgumentNotNull(jsonSerializer, nameof(jsonSerializer));
2020-05-12 15:35:59 +01:00
return new SpotifyClientConfig(
BaseAddress,
Authenticator,
jsonSerializer,
HTTPClient,
RetryHandler,
HTTPLogger,
DefaultPaginator
);
}
2020-05-12 19:33:25 +01:00
public SpotifyClientConfig WithDefaultPaginator(IPaginator defaultPaginator)
{
2020-05-12 19:33:25 +01:00
Ensure.ArgumentNotNull(defaultPaginator, nameof(defaultPaginator));
2020-05-12 15:35:59 +01:00
return new SpotifyClientConfig(
BaseAddress,
Authenticator,
JSONSerializer,
HTTPClient,
RetryHandler,
HTTPLogger,
2020-05-12 19:33:25 +01:00
defaultPaginator
2020-05-12 15:35:59 +01:00
);
}
public static SpotifyClientConfig CreateDefault(string token, string tokenType = "Bearer")
{
return CreateDefault().WithAuthenticator(new TokenAuthenticator(token, tokenType));
}
2020-05-12 15:35:59 +01:00
public static SpotifyClientConfig CreateDefault()
{
2020-05-12 15:35:59 +01:00
return new SpotifyClientConfig(
SpotifyUrls.APIV1,
null,
new NewtonsoftJSONSerializer(),
new NetHttpClient(),
null,
null,
new SimplePaginator()
);
}
}
}