Spotify.NET/SpotifyAPI.Web/Http/APIConnector.cs

286 lines
8.9 KiB
C#
Raw Normal View History

2020-05-01 19:05:28 +01:00
using System.Net.Http;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net;
namespace SpotifyAPI.Web.Http
{
public class APIConnector : IAPIConnector
{
private readonly Uri _baseAddress;
2020-05-25 17:00:38 +01:00
private readonly IAuthenticator? _authenticator;
private readonly IJSONSerializer _jsonSerializer;
private readonly IHTTPClient _httpClient;
2020-05-25 17:00:38 +01:00
private readonly IRetryHandler? _retryHandler;
private readonly IHTTPLogger? _httpLogger;
2020-05-01 19:05:28 +01:00
public event EventHandler<IResponse>? ResponseReceived;
2020-05-01 19:05:28 +01:00
public APIConnector(Uri baseAddress, IAuthenticator authenticator) :
this(baseAddress, authenticator, new NewtonsoftJSONSerializer(), new NetHttpClient(), null, null)
2020-05-01 19:05:28 +01:00
{ }
public APIConnector(
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-01 19:05:28 +01:00
{
_baseAddress = baseAddress;
_authenticator = authenticator;
_jsonSerializer = jsonSerializer;
_httpClient = httpClient;
_retryHandler = retryHandler;
_httpLogger = httpLogger;
2020-05-01 19:05:28 +01:00
}
public Task<T> Delete<T>(Uri uri)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Delete);
}
2020-05-25 17:00:38 +01:00
public Task<T> Delete<T>(Uri uri, IDictionary<string, string>? parameters)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Delete, parameters);
}
2020-05-25 17:00:38 +01:00
public Task<T> Delete<T>(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Delete, parameters, body);
}
2020-05-25 17:00:38 +01:00
public async Task<HttpStatusCode> Delete(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-04 22:02:53 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendAPIRequestDetailed(uri, HttpMethod.Delete, parameters, body).ConfigureAwait(false);
2020-05-04 22:02:53 +01:00
return response.StatusCode;
}
2020-05-01 19:05:28 +01:00
public Task<T> Get<T>(Uri uri)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Get);
}
2020-05-25 17:00:38 +01:00
public Task<T> Get<T>(Uri uri, IDictionary<string, string>? parameters)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Get, parameters);
}
2020-05-25 17:00:38 +01:00
public async Task<HttpStatusCode> Get(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-07 17:03:20 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendAPIRequestDetailed(uri, HttpMethod.Get, parameters, body).ConfigureAwait(false);
return response.StatusCode;
}
2020-05-01 19:05:28 +01:00
public Task<T> Post<T>(Uri uri)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Post);
}
2020-05-25 17:00:38 +01:00
public Task<T> Post<T>(Uri uri, IDictionary<string, string>? parameters)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Post, parameters);
}
2020-05-25 17:00:38 +01:00
public Task<T> Post<T>(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
2020-05-03 00:00:35 +01:00
return SendAPIRequest<T>(uri, HttpMethod.Post, parameters, body);
2020-05-01 19:05:28 +01:00
}
2020-05-25 17:00:38 +01:00
public Task<T> Post<T>(Uri uri, IDictionary<string, string>? parameters, object? body, Dictionary<string, string>? headers)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Post, parameters, body, headers);
}
2020-05-25 17:00:38 +01:00
public async Task<HttpStatusCode> Post(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-04 22:02:53 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendAPIRequestDetailed(uri, HttpMethod.Post, parameters, body).ConfigureAwait(false);
2020-05-04 22:02:53 +01:00
return response.StatusCode;
}
2020-05-01 19:05:28 +01:00
public Task<T> Put<T>(Uri uri)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Put);
}
2020-05-25 17:00:38 +01:00
public Task<T> Put<T>(Uri uri, IDictionary<string, string>? parameters)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Put, parameters);
}
2020-05-25 17:00:38 +01:00
public Task<T> Put<T>(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Put, parameters, body);
}
2020-05-25 17:00:38 +01:00
public async Task<HttpStatusCode> Put(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-03 08:06:28 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendAPIRequestDetailed(uri, HttpMethod.Put, parameters, body).ConfigureAwait(false);
2020-05-03 08:06:28 +01:00
return response.StatusCode;
}
2020-05-25 17:00:38 +01:00
public async Task<HttpStatusCode> PutRaw(Uri uri, IDictionary<string, string>? parameters, object? body)
2020-05-03 07:10:41 +01:00
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendRawRequest(uri, HttpMethod.Put, parameters, body).ConfigureAwait(false);
2020-05-03 07:10:41 +01:00
return response.StatusCode;
}
2020-05-01 19:05:28 +01:00
public void SetRequestTimeout(TimeSpan timeout)
{
_httpClient.SetRequestTimeout(timeout);
}
2020-05-03 07:10:41 +01:00
private IRequest CreateRequest(
2020-05-01 19:05:28 +01:00
Uri uri,
HttpMethod method,
2020-05-25 17:00:38 +01:00
IDictionary<string, string>? parameters,
object? body,
IDictionary<string, string>? headers
2020-05-01 19:05:28 +01:00
)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
Ensure.ArgumentNotNull(method, nameof(method));
2020-05-25 17:00:38 +01:00
return new Request(
_baseAddress,
uri,
method,
headers ?? new Dictionary<string, string>(),
parameters ?? new Dictionary<string, string>())
2020-05-01 19:05:28 +01:00
{
Body = body
2020-05-01 19:05:28 +01:00
};
2020-05-03 07:10:41 +01:00
}
2020-05-01 19:05:28 +01:00
2020-05-03 08:06:28 +01:00
private async Task<IAPIResponse<T>> DoSerializedRequest<T>(IRequest request)
2020-05-03 07:10:41 +01:00
{
2020-05-01 19:05:28 +01:00
_jsonSerializer.SerializeRequest(request);
var response = await DoRequest(request).ConfigureAwait(false);
2020-05-03 07:10:41 +01:00
return _jsonSerializer.DeserializeResponse<T>(response);
}
2020-05-03 08:06:28 +01:00
private async Task<IResponse> DoRequest(IRequest request)
2020-05-03 07:10:41 +01:00
{
2020-05-14 22:26:40 +01:00
await ApplyAuthenticator(request).ConfigureAwait(false);
_httpLogger?.OnRequest(request);
2020-05-01 19:05:28 +01:00
IResponse response = await _httpClient.DoRequest(request).ConfigureAwait(false);
_httpLogger?.OnResponse(response);
ResponseReceived?.Invoke(this, response);
2020-05-02 21:48:21 +01:00
if (_retryHandler != null)
{
response = await _retryHandler.HandleRetry(request, response, async (newRequest) =>
2020-05-02 21:48:21 +01:00
{
2020-05-14 22:26:40 +01:00
await ApplyAuthenticator(request).ConfigureAwait(false);
var newResponse = await _httpClient.DoRequest(request).ConfigureAwait(false);
_httpLogger?.OnResponse(newResponse);
ResponseReceived?.Invoke(this, response);
return newResponse;
}).ConfigureAwait(false);
2020-05-02 21:48:21 +01:00
}
2020-05-01 19:05:28 +01:00
ProcessErrors(response);
2020-05-03 07:10:41 +01:00
return response;
}
2020-05-01 19:05:28 +01:00
2020-05-14 22:26:40 +01:00
private async Task ApplyAuthenticator(IRequest request)
{
2020-05-15 19:06:24 +01:00
if (_authenticator != null
&& !request.Endpoint.IsAbsoluteUri
|| request.Endpoint.AbsoluteUri.Contains("https://api.spotify.com", StringComparison.InvariantCulture))
2020-05-14 22:26:40 +01:00
{
2020-05-25 17:00:38 +01:00
await _authenticator!.Apply(request, this).ConfigureAwait(false);
2020-05-14 22:26:40 +01:00
}
}
2020-05-03 07:10:41 +01:00
public Task<IResponse> SendRawRequest(
Uri uri,
HttpMethod method,
2020-05-25 17:00:38 +01:00
IDictionary<string, string>? parameters = null,
object? body = null,
IDictionary<string, string>? headers = null
2020-05-03 07:10:41 +01:00
)
{
var request = CreateRequest(uri, method, parameters, body, headers);
2020-05-03 08:06:28 +01:00
return DoRequest(request);
2020-05-03 07:10:41 +01:00
}
public async Task<T> SendAPIRequest<T>(
Uri uri,
HttpMethod method,
2020-05-25 17:00:38 +01:00
IDictionary<string, string>? parameters = null,
object? body = null,
IDictionary<string, string>? headers = null
2020-05-03 07:10:41 +01:00
)
{
var request = CreateRequest(uri, method, parameters, body, headers);
IAPIResponse<T> apiResponse = await DoSerializedRequest<T>(request).ConfigureAwait(false);
2020-11-13 13:43:00 +00:00
return apiResponse.Body!;
2020-05-01 19:05:28 +01:00
}
2020-05-03 08:06:28 +01:00
public async Task<IResponse> SendAPIRequestDetailed(
Uri uri,
HttpMethod method,
2020-05-25 17:00:38 +01:00
IDictionary<string, string>? parameters = null,
object? body = null,
IDictionary<string, string>? headers = null
2020-05-03 08:06:28 +01:00
)
{
var request = CreateRequest(uri, method, parameters, body, headers);
var response = await DoSerializedRequest<object>(request).ConfigureAwait(false);
2020-05-03 08:06:28 +01:00
return response.Response;
}
private static void ProcessErrors(IResponse response)
2020-05-01 19:05:28 +01:00
{
Ensure.ArgumentNotNull(response, nameof(response));
if ((int)response.StatusCode >= 200 && (int)response.StatusCode < 400)
{
return;
}
throw response.StatusCode switch
2020-05-01 19:05:28 +01:00
{
HttpStatusCode.Unauthorized => new APIUnauthorizedException(response),
// TODO: Remove hack once .netstandard 2.0 is not supported
(HttpStatusCode)429 => new APITooManyRequestsException(response),
_ => new APIException(response),
};
2020-05-01 19:05:28 +01:00
}
}
}