Updated lots of stuff, got multi scrape working, need to test not-nullable chapter novel ids with our current model, now supports sqlite and postgres concurrently (and easy add more), need to get it deployed/do auth
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-07-16 17:17:43 -04:00
parent eab3399268
commit d98324c11e
73 changed files with 1591 additions and 680 deletions

View File

@@ -0,0 +1,83 @@
using System.Net.Mime;
using System.Text;
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using Treestar.Shared.Models;
namespace Treestar.Shared.AccessLayers;
public abstract class ApiAccessLayer
{
private readonly HttpClient _httpClient;
protected ApiAccessLayer(string apiBaseUrl)
{
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)
{
var response = await _httpClient.SendAsync(message);
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);
}
}