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
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
83
Treestar.Shared/AccessLayers/ApiAccessLayer.cs
Normal file
83
Treestar.Shared/AccessLayers/ApiAccessLayer.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user