2024-05-30 01:06:08 +00:00
|
|
|
|
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;
|
2024-05-31 13:16:09 +00:00
|
|
|
|
public const string BaseUrl = "https://api.omg.lol";
|
2024-05-30 01:06:08 +00:00
|
|
|
|
|
2024-05-31 13:16:09 +00:00
|
|
|
|
public RestService(string? token = null) {
|
2024-05-30 01:06:08 +00:00
|
|
|
|
_client = new HttpClient();
|
|
|
|
|
_serializerOptions = new JsonSerializerOptions {
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
|
|
|
|
WriteIndented = true
|
|
|
|
|
};
|
2024-05-31 13:16:09 +00:00
|
|
|
|
addToken(token);
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 13:16:09 +00:00
|
|
|
|
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<T?> Get<T>(Uri uri) where T:IOmgLolResponseData {
|
2024-05-30 01:06:08 +00:00
|
|
|
|
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() {
|
2024-05-31 13:16:09 +00:00
|
|
|
|
Uri uri = new Uri($"{BaseUrl}/statuslog/latest");
|
|
|
|
|
return (await Get<StatusResponseData>(uri))?.Statuses ?? new List<Status>();
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<Status>> Statuslog(string address) {
|
2024-05-31 13:16:09 +00:00
|
|
|
|
Uri uri = new Uri($"{BaseUrl}/address/{address}/statuses");
|
|
|
|
|
return (await Get<StatusResponseData>(uri))?.Statuses ?? new List<Status>();
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<MarkupString> StatuslogBio(string address) {
|
2024-05-31 13:16:09 +00:00
|
|
|
|
Uri uri = new Uri($"{BaseUrl}/address/{address}/statuses/bio");
|
|
|
|
|
StatusBioResponseData? responseData = await Get<StatusBioResponseData>(uri);
|
2024-05-31 01:27:01 +00:00
|
|
|
|
return (MarkupString)Markdown.ToHtml(responseData?.Bio ?? "");
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
2024-05-31 13:16:09 +00:00
|
|
|
|
|
|
|
|
|
public async Task<AccountResponseData?> AccountInfo() {
|
|
|
|
|
Uri uri = new Uri($"{BaseUrl}/account/application/info");
|
|
|
|
|
AccountResponseData? responseData = await Get<AccountResponseData>(uri);
|
|
|
|
|
return responseData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AddressResponseList?> Addresses() {
|
|
|
|
|
Uri uri = new Uri($"{BaseUrl}/account/application/addresses");
|
|
|
|
|
AddressResponseList? responseData = await Get<AddressResponseList>(uri);
|
|
|
|
|
return responseData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string?> 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<TokenResponseData>(_serializerOptions);
|
|
|
|
|
if (responseObj != null && !string.IsNullOrEmpty(responseObj.AccessToken)) {
|
|
|
|
|
token = responseObj.AccessToken;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
return token;
|
|
|
|
|
}
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|