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 GetResponse(Uri uri) where T:IOmgLolResponseData { T? responseData = default(T); try { HttpResponseMessage response = await _client.GetAsync(uri); if (response.IsSuccessStatusCode) { OmgLolResponse? responseObj = await response.Content.ReadFromJsonAsync>(_serializerOptions); if (responseObj != null && responseObj.Request.Success) { responseData = responseObj.Response; } } } catch (Exception ex) { Debug.WriteLine(@"\tERROR {0}", ex.Message); } return responseData; } public async Task> StatuslogLatest() { Uri uri = new Uri("https://api.omg.lol/statuslog/latest"); return (await GetResponse(uri))?.Statuses ?? new List(); } public async Task> Statuslog(string address) { Uri uri = new Uri($"https://api.omg.lol/address/{address}/statuses"); return (await GetResponse(uri))?.Statuses ?? new List(); } public async Task StatuslogBio(string address) { Uri uri = new Uri($"https://api.omg.lol/address/{address}/statuses/bio"); return (MarkupString)Markdown.ToHtml((await GetResponse(uri))?.Bio ?? ""); } } }