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?> GetSeason() { var resp = await Client.GetAsync(ApiBaseUrl + "season"); if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync>(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task 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 GetAnime(long animeId) { var resp = await Client.GetAsync(ApiBaseUrl + "anime/" + animeId); if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task?> SearchAnime(string query) { var resp = await Client.GetAsync(ApiBaseUrl + "animesearch?q=" + query); if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync>(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task?> GetUser() { var resp = await Client.GetAsync(ApiBaseUrl + "user"); if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync>(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task 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>(JsonSerializerOptions); return users?.SingleOrDefault(); } internal static async Task?> GetWatching() { var resp = await Client.GetAsync(ApiBaseUrl + "watch"); if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync>(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task?> 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>(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task?> GetWatchingExtended() { var resp = await Client.GetAsync(ApiBaseUrl + "watchext"); if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync>(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task?> 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>(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } internal static async Task 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(JsonSerializerOptions); LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode); return null; } }