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
|
|
|
|
{
|
2020-05-02 12:04:26 +01:00
|
|
|
private readonly Uri _baseAddress;
|
|
|
|
private readonly IAuthenticator _authenticator;
|
|
|
|
private readonly IJSONSerializer _jsonSerializer;
|
|
|
|
private readonly IHTTPClient _httpClient;
|
2020-05-02 18:57:31 +01:00
|
|
|
private readonly IRetryHandler _retryHandler;
|
2020-05-03 12:00:50 +01:00
|
|
|
private readonly IHTTPLogger _httpLogger;
|
2020-05-01 19:05:28 +01:00
|
|
|
|
|
|
|
public APIConnector(Uri baseAddress, IAuthenticator authenticator) :
|
2020-05-03 12:00:50 +01:00
|
|
|
this(baseAddress, authenticator, new NewtonsoftJSONSerializer(), new NetHttpClient(), null, null)
|
2020-05-01 19:05:28 +01:00
|
|
|
{ }
|
2020-05-02 18:57:31 +01:00
|
|
|
public APIConnector(
|
|
|
|
Uri baseAddress,
|
|
|
|
IAuthenticator authenticator,
|
|
|
|
IJSONSerializer jsonSerializer,
|
|
|
|
IHTTPClient httpClient,
|
2020-05-03 12:00:50 +01:00
|
|
|
IRetryHandler retryHandler,
|
|
|
|
IHTTPLogger httpLogger)
|
2020-05-01 19:05:28 +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-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Delete<T>(Uri uri)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
return SendAPIRequest<T>(uri, HttpMethod.Delete);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Delete<T>(Uri uri, IDictionary<string, string> parameters)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
return SendAPIRequest<T>(uri, HttpMethod.Delete, parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Delete<T>(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
return SendAPIRequest<T>(uri, HttpMethod.Delete, parameters, body);
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:02:53 +01:00
|
|
|
public async Task<HttpStatusCode> Delete(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Get<T>(Uri uri, IDictionary<string, string> parameters)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
return SendAPIRequest<T>(uri, HttpMethod.Get, parameters);
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:03:20 +01:00
|
|
|
public async Task<HttpStatusCode> Get(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Post<T>(Uri uri, IDictionary<string, string> parameters)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
return SendAPIRequest<T>(uri, HttpMethod.Post, parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Post<T>(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
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-13 22:49:54 +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-04 22:02:53 +01:00
|
|
|
public async Task<HttpStatusCode> Post(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Put<T>(Uri uri, IDictionary<string, string> parameters)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
return SendAPIRequest<T>(uri, HttpMethod.Put, parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> Put<T>(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
return SendAPIRequest<T>(uri, HttpMethod.Put, parameters, body);
|
|
|
|
}
|
|
|
|
|
2020-05-03 08:06:28 +01:00
|
|
|
public async Task<HttpStatusCode> Put(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
var response = await SendAPIRequestDetailed(uri, HttpMethod.Put, parameters, body).ConfigureAwait(false);
|
2020-05-03 08:06:28 +01:00
|
|
|
return response.StatusCode;
|
|
|
|
}
|
|
|
|
|
2020-05-03 07:10:41 +01:00
|
|
|
public async Task<HttpStatusCode> PutRaw(Uri uri, IDictionary<string, string> parameters, object body)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
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-03 07:10:41 +01:00
|
|
|
IDictionary<string, string> parameters,
|
2020-05-13 22:49:54 +01:00
|
|
|
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-13 22:49:54 +01:00
|
|
|
return new Request(headers ?? new Dictionary<string, string>(), parameters ?? new Dictionary<string, string>())
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
|
|
|
BaseAddress = _baseAddress,
|
|
|
|
Endpoint = uri,
|
2020-05-02 12:04:26 +01:00
|
|
|
Method = method,
|
|
|
|
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);
|
2020-05-05 04:26:37 +01:00
|
|
|
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);
|
2020-05-03 12:00:50 +01:00
|
|
|
_httpLogger?.OnRequest(request);
|
2020-05-01 19:05:28 +01:00
|
|
|
IResponse response = await _httpClient.DoRequest(request).ConfigureAwait(false);
|
2020-05-03 12:00:50 +01:00
|
|
|
_httpLogger?.OnResponse(response);
|
2020-05-02 21:48:21 +01:00
|
|
|
if (_retryHandler != null)
|
2020-05-02 18:57:31 +01:00
|
|
|
{
|
2020-05-05 14:30:00 +01:00
|
|
|
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);
|
2020-05-03 12:00:50 +01:00
|
|
|
var newResponse = await _httpClient.DoRequest(request).ConfigureAwait(false);
|
|
|
|
_httpLogger?.OnResponse(newResponse);
|
|
|
|
return newResponse;
|
2020-05-05 14:30:00 +01:00
|
|
|
}).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
|
|
|
{
|
|
|
|
await _authenticator.Apply(request, this).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 07:10:41 +01:00
|
|
|
public Task<IResponse> SendRawRequest(
|
|
|
|
Uri uri,
|
|
|
|
HttpMethod method,
|
|
|
|
IDictionary<string, string> parameters = null,
|
2020-05-13 22:49:54 +01:00
|
|
|
object body = null,
|
|
|
|
IDictionary<string, string> headers = null
|
2020-05-03 07:10:41 +01:00
|
|
|
)
|
|
|
|
{
|
2020-05-13 22:49:54 +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,
|
|
|
|
IDictionary<string, string> parameters = null,
|
2020-05-13 22:49:54 +01:00
|
|
|
object body = null,
|
|
|
|
IDictionary<string, string> headers = null
|
2020-05-03 07:10:41 +01:00
|
|
|
)
|
|
|
|
{
|
2020-05-13 22:49:54 +01:00
|
|
|
var request = CreateRequest(uri, method, parameters, body, headers);
|
2020-05-05 14:30:00 +01:00
|
|
|
IAPIResponse<T> apiResponse = await DoSerializedRequest<T>(request).ConfigureAwait(false);
|
2020-05-01 19:05:28 +01:00
|
|
|
return apiResponse.Body;
|
|
|
|
}
|
|
|
|
|
2020-05-03 08:06:28 +01:00
|
|
|
public async Task<IResponse> SendAPIRequestDetailed(
|
|
|
|
Uri uri,
|
|
|
|
HttpMethod method,
|
|
|
|
IDictionary<string, string> parameters = null,
|
2020-05-13 22:49:54 +01:00
|
|
|
object body = null,
|
|
|
|
IDictionary<string, string> headers = null
|
2020-05-03 08:06:28 +01:00
|
|
|
)
|
|
|
|
{
|
2020-05-13 22:49:54 +01:00
|
|
|
var request = CreateRequest(uri, method, parameters, body, headers);
|
2020-05-05 14:30:00 +01:00
|
|
|
var response = await DoSerializedRequest<object>(request).ConfigureAwait(false);
|
2020-05-03 08:06:28 +01:00
|
|
|
return response.Response;
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:30:00 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:04:26 +01:00
|
|
|
throw response.StatusCode switch
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
2020-05-02 12:04:26 +01:00
|
|
|
HttpStatusCode.Unauthorized => new APIUnauthorizedException(response),
|
|
|
|
_ => new APIException(response),
|
|
|
|
};
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|