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

32 lines
1.1 KiB
C#

using System.Net.Http.Json;
using System.Text.Json;
using APP.DTO;
namespace APP.Utility;
internal static class BackendComms
{
private const string ApiBaseUrl = "https://huso.hanami.family/api/";
private static readonly HttpClient Client = new();
private static readonly JsonSerializerOptions JsonSerializerOptions = new() {PropertyNameCaseInsensitive = true};
internal static async Task<List<SeasonAnime>?> GetSeason()
{
var resp = await Client.GetAsync(ApiBaseUrl + "season");
if (!resp.IsSuccessStatusCode) return null;
return await resp.Content.ReadFromJsonAsync<List<SeasonAnime>>(JsonSerializerOptions);
}
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) return null;
var users = await resp.Content.ReadFromJsonAsync<ICollection<User>>(JsonSerializerOptions);
return users?.SingleOrDefault();
}
}