Refactor and novel18 support (added cookie support in general to AbstractScraper.cs
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
93
Common/AccessLayers/ApiAccessLayer.cs
Normal file
93
Common/AccessLayers/ApiAccessLayer.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System.Net.Mime;
|
||||
using System.Text;
|
||||
using Common.Models;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Common.AccessLayers;
|
||||
|
||||
public abstract class ApiAccessLayer
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly IAccessLayerAuthenticationProvider _authenticationProvider;
|
||||
protected readonly ILogger Logger;
|
||||
|
||||
protected ApiAccessLayer(string apiBaseUrl, IAccessLayerAuthenticationProvider authenticationProvider, ILogger logger)
|
||||
{
|
||||
_authenticationProvider = authenticationProvider;
|
||||
Logger = logger;
|
||||
var handler = new HttpClientHandler()
|
||||
{
|
||||
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
||||
};
|
||||
_httpClient = new HttpClient(handler);
|
||||
_httpClient.BaseAddress = new Uri(apiBaseUrl, UriKind.Absolute);
|
||||
}
|
||||
|
||||
private async Task<HttpResponseWrapper> SendRequest(HttpRequestMessage message)
|
||||
{
|
||||
await _authenticationProvider.AddAuthentication(message);
|
||||
var response = await _httpClient.SendAsync(message);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Logger.LogError("Response returned status code {statusCode} with reason {reason} and content {content}", response.StatusCode, response.ReasonPhrase, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
return new HttpResponseWrapper()
|
||||
{
|
||||
HttpResponseMessage = response
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<HttpResponseWrapper<T>> SendRequest<T>(HttpRequestMessage message)
|
||||
{
|
||||
var wrapper = await SendRequest(message);
|
||||
if (wrapper.HttpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
var parsedJson =
|
||||
JsonConvert.DeserializeObject<T>(await wrapper.HttpResponseMessage.Content.ReadAsStringAsync());
|
||||
return new HttpResponseWrapper<T>
|
||||
{
|
||||
HttpResponseMessage = wrapper.HttpResponseMessage,
|
||||
ResponseObject = parsedJson
|
||||
};
|
||||
}
|
||||
return new HttpResponseWrapper<T>
|
||||
{
|
||||
HttpResponseMessage = wrapper.HttpResponseMessage,
|
||||
ResponseObject = default(T)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
protected HttpRequestMessage CreateRequestMessage(string endpoint, HttpMethod method, Dictionary<string, string>? queryParams = null,
|
||||
object? data = null)
|
||||
{
|
||||
HttpRequestMessage message = new HttpRequestMessage();
|
||||
string uri = endpoint;
|
||||
if (queryParams != null)
|
||||
{
|
||||
uri = QueryHelpers.AddQueryString(endpoint, queryParams);
|
||||
}
|
||||
message.RequestUri = new Uri(uri, UriKind.Relative);
|
||||
message.Method = method;
|
||||
if (data != null)
|
||||
{
|
||||
message.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, MediaTypeNames.Application.Json);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
protected async Task<HttpResponseWrapper> SendRequest(string endpoint, HttpMethod method, Dictionary<string, string>? queryParams = null, object? data = null)
|
||||
{
|
||||
HttpRequestMessage message = CreateRequestMessage(endpoint, method, queryParams, data);
|
||||
return await SendRequest(message);
|
||||
}
|
||||
|
||||
protected async Task<HttpResponseWrapper<T>> SendRequest<T>(string endpoint, HttpMethod method, Dictionary<string, string>? queryParams = null, object? data = null)
|
||||
{
|
||||
HttpRequestMessage message = CreateRequestMessage(endpoint, method, queryParams, data);
|
||||
return await SendRequest<T>(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user