Files
app/APP/Utility/BackendComms.cs
2022-04-19 00:05:16 +02:00

135 lines
5.2 KiB
C#

using System.Net.Http.Json;
using System.Net.Mime;
using System.Text;
using System.Text.Json;
using APP.DTO;
namespace APP.Utility;
internal static class BackendComms
{
private const string ApiBaseUrl = "https://huso.hanami.family/api/";
private const string AuthHeader = "X-HUSO-AUTH";
private static readonly HttpClient Client = new();
private static readonly JsonSerializerOptions JsonSerializerOptions = new() {PropertyNameCaseInsensitive = true};
internal static async Task<List<Anime>?> GetSeason()
{
var resp = await Client.GetAsync(ApiBaseUrl + "season");
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Anime>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<bool> Auth(string username, string secret)
{
if (string.IsNullOrWhiteSpace(username))
{
return false;
}
using var msg = new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + "auth/" + username);
msg.Headers.Add(AuthHeader, secret);
var resp = await Client.SendAsync(msg);
if (!resp.IsSuccessStatusCode)
{
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
}
return resp.IsSuccessStatusCode;
}
internal static async Task<Anime?> GetAnime(long animeId)
{
var resp = await Client.GetAsync(ApiBaseUrl + "anime/" + animeId);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<Anime>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<Anime>?> SearchAnime(string query)
{
var resp = await Client.GetAsync(ApiBaseUrl + "animesearch?q=" + query);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Anime>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<User>?> GetUser()
{
var resp = await Client.GetAsync(ApiBaseUrl + "user");
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<User>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<User?> GetUser(string? username)
{
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
var resp = await Client.GetAsync(ApiBaseUrl + "user/" + username);
if (!resp.IsSuccessStatusCode)
{
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
var users = await resp.Content.ReadFromJsonAsync<ICollection<User>>(JsonSerializerOptions);
return users?.SingleOrDefault();
}
internal static async Task<List<Watch>?> GetWatching()
{
var resp = await Client.GetAsync(ApiBaseUrl + "watch");
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Watch>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<Watch>?> GetWatching(string? username)
{
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
var resp = await Client.GetAsync(ApiBaseUrl + "watch/" + username);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Watch>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<WatchExtended>?> GetWatchingExtended()
{
var resp = await Client.GetAsync(ApiBaseUrl + "watchext");
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<WatchExtended>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<WatchExtended>?> GetWatchingExtended(string? username)
{
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
var resp = await Client.GetAsync(ApiBaseUrl + "watchext/" + username);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<WatchExtended>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<Register?> Register(Register register)
{
using var content = new StringContent(JsonSerializer.Serialize(register), Encoding.UTF8, MediaTypeNames.Application.Json);
using var resp = await Client.PostAsync(ApiBaseUrl + "register", content);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<Register>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
}