Neighbourhood.omg.lol/RestService.cs

61 lines
2.1 KiB
C#

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");
StatusBioResponseData? responseData = await GetResponse<StatusBioResponseData>(uri);
Debug.WriteLine(responseData?.Bio);
return (MarkupString)Markdown.ToHtml(responseData?.Bio ?? "");
}
}
}