using Markdig; using Microsoft.AspNetCore.Components; using Neighbourhood.omg.lol.Models; using System.Diagnostics; using System.Net.Http.Json; using System.Text.Json; using System.Text.RegularExpressions; 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; } private async Task Post(Uri uri, TData data) where TResponse : IOmgLolResponseData { TResponse? responseData = default(TResponse); try { HttpResponseMessage response = await _client.PostAsJsonAsync(uri, data); 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() => (await Get(new Uri($"{BaseUrl}/statuslog/latest")))?.Statuses ?? new List(); public async Task> Statuslog(string address) => (await Get(new Uri($"{BaseUrl}/address/{address}/statuses")))?.Statuses ?? new List(); public async Task StatuslogBio(string address) { StatusBioResponseData? responseData = await Get(new Uri($"{BaseUrl}/address/{address}/statuses/bio")); return (MarkupString)Markdown.ToHtml(responseData?.Bio ?? ""); } public async Task AccountInfo() => await Get(new Uri($"{BaseUrl}/account/application/info")); public async Task Addresses() => await Get(new Uri($"{BaseUrl}/account/application/addresses")); public async Task StatusPost(string address, StatusPost statusPost) => await Post(new Uri($"{BaseUrl}/address/{address}/statuses"), statusPost); public async Task> SomePics() => (await Get(new Uri($"{BaseUrl}/pics")))?.Pics ?? new List(); public async Task> SomePics(string address) => (await Get(new Uri($"{BaseUrl}/address/{address}/pics")))?.Pics ?? new List(); public async Task SomePic(string address, string id) => (await Get(new Uri($"{BaseUrl}/address/{address}/pics/{id}"))); public async Task> Ephemeral() { List notes = new List(); Uri Uri = new Uri($"https://eph.emer.al/"); try { var response = await _client.GetAsync(Uri); var str = await response.Content.ReadAsStringAsync(); string pattern = @"

(.*?)<\/p>"; var matches = Regex.Matches(str, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); foreach (Match match in matches) { notes.Add(match.Groups[1].Value); } } catch (Exception ex) { Debug.WriteLine(ex); } return notes; } public async Task OAuth(string code, string client_id, string client_secret, string redirect_uri) { string? token = null; Uri uri = new Uri($"{BaseUrl}/oauth/?code={code}&client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}&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; } } }