Neighbourhood.omg.lol/RestService.cs

60 lines
2 KiB
C#
Raw Normal View History

2024-05-30 01:06:08 +00:00
using Markdig;
using Microsoft.AspNetCore.Components;
using Neighbourhood.omg.lol.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Neighbourhood.omg.lol {
public class RestService {
HttpClient _client;
JsonSerializerOptions _serializerOptions;
public RestService() {
_client = new HttpClient();
_serializerOptions = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
WriteIndented = true
};
}
private async Task<T?> GetResponse<T>(Uri uri) where T:IOmgLolResponseData {
T? responseData = default(T);
try {
HttpResponseMessage response = await _client.GetAsync(uri);
if (response.IsSuccessStatusCode) {
OmgLolResponse<T>? responseObj = await response.Content.ReadFromJsonAsync<OmgLolResponse<T>>(_serializerOptions);
if (responseObj != null && responseObj.Request.Success) {
responseData = responseObj.Response;
}
}
}
catch (Exception ex) {
Debug.WriteLine(@"\tERROR {0}", ex.Message);
}
return responseData;
}
public async Task<List<Status>> StatuslogLatest() {
Uri uri = new Uri("https://api.omg.lol/statuslog/latest");
return (await GetResponse<StatusResponseData>(uri))?.Statuses ?? new List<Status>();
}
public async Task<List<Status>> Statuslog(string address) {
Uri uri = new Uri($"https://api.omg.lol/address/{address}/statuses");
return (await GetResponse<StatusResponseData>(uri))?.Statuses ?? new List<Status>();
}
public async Task<MarkupString> StatuslogBio(string address) {
Uri uri = new Uri($"https://api.omg.lol/address/{address}/statuses/bio");
return (MarkupString)Markdown.ToHtml((await GetResponse<StatusBioResponseData>(uri))?.Bio ?? "");
}
}
}