using Markdig; using Microsoft.AspNetCore.Components; using Neighbourhood.omg.lol.Models; using System.Diagnostics; using System.Net.Http.Json; using System.Text.Json; namespace Neighbourhood.omg.lol { public class RestService { HttpClient _client; JsonSerializerOptions _serializerOptions; public const string BaseUrl = "https://api.omg.lol"; public RestService(string? token = null) { _client = new HttpClient(); _serializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, WriteIndented = true }; addToken(token); } private void addToken(string? token = null) { if (token == null) token = Task.Run(() => SecureStorage.GetAsync("accounttoken")).GetAwaiter().GetResult(); if (token != null) _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); } private async Task Get(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($"{BaseUrl}/statuslog/latest"); return (await Get(uri))?.Statuses ?? new List(); } public async Task> Statuslog(string address) { Uri uri = new Uri($"{BaseUrl}/address/{address}/statuses"); return (await Get(uri))?.Statuses ?? new List(); } public async Task StatuslogBio(string address) { Uri uri = new Uri($"{BaseUrl}/address/{address}/statuses/bio"); StatusBioResponseData? responseData = await Get(uri); return (MarkupString)Markdown.ToHtml(responseData?.Bio ?? ""); } public async Task AccountInfo() { Uri uri = new Uri($"{BaseUrl}/account/application/info"); AccountResponseData? responseData = await Get(uri); return responseData; } public async Task Addresses() { Uri uri = new Uri($"{BaseUrl}/account/application/addresses"); AddressResponseList? responseData = await Get(uri); return responseData; } public async Task OAuth(string code) { string? token = null; Uri uri = new Uri($"{BaseUrl}/oauth/?code={code}&client_id=ea14dafd3e92cbcf93750c35cd81a031&client_secret=ec28b8653f1d98b4eef3f7a20858c43b&redirect_uri=https://neatnik.net/adam/bucket/omgloloauth/&scope=everything"); try { HttpResponseMessage response = await _client.GetAsync(uri); if (response.IsSuccessStatusCode) { TokenResponseData? responseObj = await response.Content.ReadFromJsonAsync(_serializerOptions); if (responseObj != null && !string.IsNullOrEmpty(responseObj.AccessToken)) { token = responseObj.AccessToken; } } } catch (Exception ex) { Debug.WriteLine(@"\tERROR {0}", ex.Message); } return token; } } }