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

199 lines
5.1 KiB
C#
Raw Normal View History

using System;
2022-11-27 12:29:35 +00:00
using System.Net.Http;
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; }
2020-12-28 16:23:59 +00:00
public IAPIConnector? APIConnector { 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>
2020-12-28 16:23:59 +00:00
/// <param name="apiConnector"></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-12-28 16:23:59 +00:00
IPaginator defaultPaginator,
IAPIConnector? apiConnector = null
)
{
BaseAddress = baseAddress;
Authenticator = authenticator;
JSONSerializer = jsonSerializer;
HTTPClient = httpClient;
RetryHandler = retryHandler;
HTTPLogger = httpLogger;
2020-05-12 19:33:25 +01:00
DefaultPaginator = defaultPaginator;
2020-12-28 16:23:59 +00:00
APIConnector = apiConnector;
}
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
);
}
2020-12-28 16:23:59 +00:00
public SpotifyClientConfig WithAPIConnector(IAPIConnector apiConnector)
{
Ensure.ArgumentNotNull(apiConnector, nameof(apiConnector));
return new SpotifyClientConfig(
BaseAddress,
Authenticator,
JSONSerializer,
HTTPClient,
RetryHandler,
HTTPLogger,
DefaultPaginator,
apiConnector
);
}
public IAPIConnector BuildAPIConnector()
{
return APIConnector ?? new APIConnector(
BaseAddress,
Authenticator,
JSONSerializer,
HTTPClient,
RetryHandler,
HTTPLogger
);
}
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()
);
}
}
}