2024-07-01 23:19:37 +00:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2024-06-06 05:20:09 +00:00
|
|
|
|
using Microsoft.AspNetCore.Components.Forms;
|
2024-05-30 01:06:08 +00:00
|
|
|
|
using Neighbourhood.omg.lol.Models;
|
2024-07-16 02:15:56 +00:00
|
|
|
|
using System;
|
2024-05-30 01:06:08 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Net.Http.Json;
|
2024-06-06 05:20:09 +00:00
|
|
|
|
using System.Text;
|
2024-05-30 01:06:08 +00:00
|
|
|
|
using System.Text.Json;
|
2024-07-16 02:15:56 +00:00
|
|
|
|
using System.Threading;
|
2024-05-30 01:06:08 +00:00
|
|
|
|
|
2024-07-01 23:19:37 +00:00
|
|
|
|
namespace Neighbourhood.omg.lol
|
|
|
|
|
{
|
2024-07-16 00:45:33 +00:00
|
|
|
|
public class ApiService {
|
2024-05-30 01:06:08 +00:00
|
|
|
|
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-07-16 00:45:33 +00:00
|
|
|
|
public ApiService(string? token = null) {
|
2024-05-30 01:06:08 +00:00
|
|
|
|
_client = new HttpClient();
|
2024-06-06 05:20:09 +00:00
|
|
|
|
_client.BaseAddress = new Uri(BaseUrl);
|
2024-06-24 04:51:48 +00:00
|
|
|
|
_client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue(App.Name, App.Version));
|
2024-05-30 01:06:08 +00:00
|
|
|
|
_serializerOptions = new JsonSerializerOptions {
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
2024-07-16 02:15:56 +00:00
|
|
|
|
#if DEBUG
|
2024-05-30 01:06:08 +00:00
|
|
|
|
WriteIndented = true
|
2024-07-16 02:15:56 +00:00
|
|
|
|
#else
|
|
|
|
|
WriteIndented = false
|
|
|
|
|
#endif
|
2024-05-30 01:06:08 +00:00
|
|
|
|
};
|
2024-06-14 07:20:04 +00:00
|
|
|
|
AddToken(token);
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 02:15:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserialize json convenience function with default serializer options
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type to deserialize</typeparam>
|
|
|
|
|
/// <param name="str">The string to deserialize</param>
|
|
|
|
|
/// <returns>The deserialized object if successful, otherwise default</returns>
|
2024-07-11 05:57:53 +00:00
|
|
|
|
public T? Deserialize<T>(string str) {
|
2024-07-16 02:15:56 +00:00
|
|
|
|
T? responseObj = default;
|
2024-07-11 05:57:53 +00:00
|
|
|
|
try {
|
|
|
|
|
responseObj = JsonSerializer.Deserialize<T>(str, _serializerOptions);
|
|
|
|
|
}
|
|
|
|
|
catch (JsonException ex) {
|
|
|
|
|
Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
|
|
|
|
Debug.WriteLine(str);
|
|
|
|
|
}
|
|
|
|
|
return responseObj;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 02:15:56 +00:00
|
|
|
|
#region Base Requests
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Decode the response from an API call
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TResponse">The type of response object we are trying to get</typeparam>
|
|
|
|
|
/// <param name="response">The raw Http Response Message</param>
|
|
|
|
|
/// <param name="cancellationToken">A cancellation token to cancel the operation</param>
|
|
|
|
|
/// <returns>The decoded object if successfull, otherwise default</returns>
|
|
|
|
|
private async Task<TResponse?> DecodeResponse<TResponse>(HttpResponseMessage response, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
|
|
|
|
{
|
|
|
|
|
TResponse? responseData = default;
|
2024-06-06 05:20:09 +00:00
|
|
|
|
try {
|
|
|
|
|
string str = await response.Content.ReadAsStringAsync();
|
2024-05-30 01:06:08 +00:00
|
|
|
|
if (response.IsSuccessStatusCode) {
|
2024-07-16 02:15:56 +00:00
|
|
|
|
OmgLolResponse<TResponse>? responseObj = Deserialize<OmgLolResponse<TResponse>>(str);
|
|
|
|
|
if (responseObj?.Request == null || (responseObj?.Request?.Success ?? false)) {
|
|
|
|
|
responseData = responseObj!.Response;
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-16 02:15:56 +00:00
|
|
|
|
else {
|
|
|
|
|
OmgLolResponse<TResponse>? responseObj = Deserialize<OmgLolResponse<TResponse>>(str);
|
|
|
|
|
throw responseObj == null ? new OmgLolApiException<TResponse>(str) : new OmgLolApiException<TResponse>(responseObj);
|
|
|
|
|
}
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseData;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 02:15:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs a request for the supplied uri, with the supplied Http Method,
|
|
|
|
|
/// with the supplied data in the body (if present)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TResponse">The type of response we are expecting</typeparam>
|
|
|
|
|
/// <typeparam name="TData">The type of data we are sending</typeparam>
|
|
|
|
|
/// <param name="uri">The uri to request</param>
|
|
|
|
|
/// <param name="method">The Http Method to use for the request</param>
|
|
|
|
|
/// <param name="data">The data to send in the body of the request</param>
|
|
|
|
|
/// <param name="file">A FileResult for the file to send in the body of the request as binary data</param>
|
|
|
|
|
/// <param name="cancellationToken">A cancellation token</param>
|
|
|
|
|
/// <returns>The returned data if successful, otherwise default</returns>
|
|
|
|
|
private async Task<TResponse?> Request<TResponse, TData>(string uri, HttpMethod method, TData? data = default, FileResult? file = null, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
2024-07-11 05:57:53 +00:00
|
|
|
|
{
|
|
|
|
|
TResponse? responseData = default;
|
|
|
|
|
try {
|
2024-07-16 02:15:56 +00:00
|
|
|
|
HttpRequestMessage request = new HttpRequestMessage(method, uri);
|
|
|
|
|
Stream? fileStream = null;
|
|
|
|
|
if (file != null) {
|
|
|
|
|
// append "binary" query parameter (if not already present)
|
|
|
|
|
Uri url = new Uri(_client.BaseAddress?.AbsoluteUri + uri);
|
|
|
|
|
if (string.IsNullOrEmpty(url.Query)) uri += "?binary";
|
|
|
|
|
else if (!url.Query.Contains("binary")) uri += "&binary";
|
|
|
|
|
request = new HttpRequestMessage(method, uri);
|
|
|
|
|
|
|
|
|
|
fileStream = await file.OpenReadAsync();
|
|
|
|
|
HttpContent fileStreamContent = new StreamContent(fileStream);
|
|
|
|
|
fileStreamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file.ContentType ?? "application/octet-stream");
|
|
|
|
|
fileStreamContent.Headers.ContentLength = fileStream.Length;
|
|
|
|
|
request.Content = fileStreamContent;
|
2024-07-11 05:57:53 +00:00
|
|
|
|
}
|
2024-07-16 02:15:56 +00:00
|
|
|
|
else if (data != null) {
|
|
|
|
|
string json = JsonSerializer.Serialize(data, _serializerOptions);
|
|
|
|
|
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
2024-06-13 10:02:51 +00:00
|
|
|
|
}
|
2024-07-16 02:15:56 +00:00
|
|
|
|
HttpResponseMessage response = await _client.SendAsync(request, cancellationToken: cancellationToken);
|
|
|
|
|
responseData = await DecodeResponse<TResponse>(response, cancellationToken);
|
2024-06-13 10:02:51 +00:00
|
|
|
|
|
2024-07-16 02:15:56 +00:00
|
|
|
|
fileStream?.Dispose();
|
2024-06-20 06:46:12 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseData;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 02:15:56 +00:00
|
|
|
|
// GET request
|
|
|
|
|
private async Task<TResponse?> Get<TResponse>(string uri, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
|
|
|
|
=> await Request<TResponse, object>(uri, HttpMethod.Get, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
// POST request
|
|
|
|
|
private async Task<TResponse?> Post<TResponse, TData>(string uri, TData data, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
|
|
|
|
=> await Request<TResponse, TData>(uri, HttpMethod.Post, data: data, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
// POST request, but with a file as binary data
|
|
|
|
|
private async Task<TResponse?> PostBinary<TResponse>(string uri, FileResult? fileResult = null, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
|
|
|
|
=> await Request<TResponse, object>(uri, HttpMethod.Post, file: fileResult, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
// PUT request
|
|
|
|
|
private async Task<TResponse?> Put<TResponse, TData>(string uri, TData data, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
|
|
|
|
=> await Request<TResponse, TData>(uri, HttpMethod.Put, data: data, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
// PATCH request
|
|
|
|
|
private async Task<TResponse?> Patch<TResponse, TData>(string uri, TData data, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
|
|
|
|
=> await Request<TResponse, TData>(uri, HttpMethod.Patch, data: data, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
// Delete request
|
|
|
|
|
private async Task<TResponse?> Delete<TResponse>(string uri, CancellationToken cancellationToken = default)
|
|
|
|
|
where TResponse : IOmgLolResponseData
|
|
|
|
|
=> await Request<TResponse, object>(uri, HttpMethod.Delete, cancellationToken: cancellationToken);
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Specific Requests
|
2024-06-05 12:41:08 +00:00
|
|
|
|
public async Task<List<Status>> StatuslogLatest() =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
(await Get<StatusResponseData>("/statuslog/latest"))?.Statuses ?? new List<Status>();
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
|
|
public async Task<List<Status>> Statuslog(string address) =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
(await Get<StatusResponseData>($"/address/{address}/statuses"))?.Statuses ?? new List<Status>();
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
2024-07-23 01:12:52 +00:00
|
|
|
|
public async Task<string> StatuslogBio(string address) =>
|
|
|
|
|
(await Get<StatusBioResponseData>($"/address/{address}/statuses/bio"))?.Bio ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
public async Task<string> PostStatuslogBio(string address, string bio) =>
|
|
|
|
|
(await Post<StatusBioResponseData, PostStatusBio>($"/address/{address}/statuses/bio", new PostStatusBio() { Content = bio }))?.Bio ?? string.Empty;
|
2024-05-31 13:16:09 +00:00
|
|
|
|
|
2024-06-05 12:41:08 +00:00
|
|
|
|
public async Task<AccountResponseData?> AccountInfo() =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
await Get<AccountResponseData>("/account/application/info");
|
2024-05-31 13:16:09 +00:00
|
|
|
|
|
2024-06-05 12:41:08 +00:00
|
|
|
|
public async Task<AddressResponseList?> Addresses() =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
await Get<AddressResponseList>("/account/application/addresses");
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
|
|
public async Task<StatusPostResponseData?> StatusPost(string address, StatusPost statusPost) =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
await Post<StatusPostResponseData, StatusPost>($"/address/{address}/statuses", statusPost);
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
|
|
public async Task<List<Pic>> SomePics() =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
(await Get<SomePicsResponseData>("/pics"))?.Pics ?? new List<Pic>();
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
|
|
public async Task<List<Pic>> SomePics(string address) =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
(await Get<SomePicsResponseData>($"/address/{address}/pics"))?.Pics ?? new List<Pic>();
|
|
|
|
|
|
|
|
|
|
public async Task<PutPicResponseData?> PutPic(string address, string base64Image) =>
|
|
|
|
|
(await Put<PutPicResponseData, PutPic>($"/address/{address}/pics/upload", new PutPic { Pic = base64Image }));
|
2024-06-07 04:25:21 +00:00
|
|
|
|
|
2024-06-06 05:20:09 +00:00
|
|
|
|
public async Task<PutPicResponseData?> PutPic(string address, byte[] bytes) =>
|
|
|
|
|
await PutPic(address, Convert.ToBase64String(bytes));
|
|
|
|
|
|
2024-07-02 00:13:52 +00:00
|
|
|
|
public async Task<PutPicResponseData?> PostPicDescription(string address, string id, string? description) =>
|
2024-06-06 05:20:09 +00:00
|
|
|
|
(await Post<PutPicResponseData, PostPic>($"/address/{address}/pics/{id}", new PostPic { Description = description }));
|
2024-06-21 06:26:11 +00:00
|
|
|
|
public async Task<BasicResponseData?> DeletePic(string address, string id) =>
|
|
|
|
|
(await Delete<BasicResponseData>($"/address/{address}/pics/{id}"));
|
2024-06-20 06:46:12 +00:00
|
|
|
|
|
2024-06-13 10:02:51 +00:00
|
|
|
|
public async Task<PatchStatusResponseData?> PatchStatus(string address, string id, string content, string? emoji) =>
|
|
|
|
|
(await Patch<PatchStatusResponseData, PatchStatus>($"/address/{address}/statuses/", new PatchStatus { Id = id, Content = content, Emoji = emoji }));
|
|
|
|
|
|
2024-06-21 06:26:11 +00:00
|
|
|
|
public async Task<BasicResponseData?> DeleteStatus(string address, string id) =>
|
|
|
|
|
(await Delete<BasicResponseData>($"/address/{address}/statuses/{id}"));
|
2024-06-20 06:46:12 +00:00
|
|
|
|
|
2024-06-11 00:36:48 +00:00
|
|
|
|
public async Task<List<NowData>?> NowGarden() =>
|
|
|
|
|
(await Get<NowResponseData>($"/now/garden"))?.Garden ?? new List<NowData>();
|
|
|
|
|
|
2024-06-18 07:11:28 +00:00
|
|
|
|
public async Task<List<string>?> Directory() =>
|
|
|
|
|
(await Get<DirectoryResponseData>($"/directory"))?.Directory ?? new List<string>();
|
|
|
|
|
|
2024-06-21 06:26:11 +00:00
|
|
|
|
public async Task<NowContentData?> GetNowPage(string address) =>
|
|
|
|
|
(await Get<NowPageResponseData>($"/address/{address}/now"))?.Now;
|
|
|
|
|
|
|
|
|
|
public async Task<BasicResponseData?> PostNowPage(string address, string content, bool listed) =>
|
|
|
|
|
await Post<BasicResponseData, NowContentData>($"/address/{address}/now", new NowContentData { Content = content, Listed = listed ? 1 : 0 });
|
|
|
|
|
|
2024-06-30 23:44:55 +00:00
|
|
|
|
public async Task<List<MarkupString>> Ephemeral() =>
|
|
|
|
|
(await Get<EphemeralResponseData>($"/ephemeral"))?.Content?.Select(s => (MarkupString)s)?.ToList() ?? new List<MarkupString>();
|
|
|
|
|
|
|
|
|
|
public async Task<BasicResponseData?> PostEphemeral(string content) =>
|
|
|
|
|
await Post<BasicResponseData, EphemeralData>("/ephemeral", new EphemeralData { Content = content });
|
|
|
|
|
|
2024-07-05 05:47:05 +00:00
|
|
|
|
public async Task<ProfileResponseData?> GetProfile(string address) =>
|
|
|
|
|
await Get<ProfileResponseData>($"/address/{address}/web");
|
|
|
|
|
|
|
|
|
|
public async Task<BasicResponseData?> PostProfile(string address, string content, bool publish = true) =>
|
|
|
|
|
await Post<BasicResponseData, PostProfile>($"/address/{address}/web", new PostProfile { Content = content, Publish = publish });
|
|
|
|
|
|
|
|
|
|
public async Task<BasicResponseData?> PostProfile(string address, PostProfile data) =>
|
|
|
|
|
await Post<BasicResponseData, PostProfile>($"/address/{address}/web", data);
|
2024-07-11 05:57:53 +00:00
|
|
|
|
public async Task<Dictionary<string, Theme>?> GetThemes() =>
|
|
|
|
|
(await Get<ThemeResponseData>($"/theme/list"))?.Themes;
|
2024-07-05 05:47:05 +00:00
|
|
|
|
|
2024-07-11 05:57:53 +00:00
|
|
|
|
public async Task<MarkupString?> GetThemePreview(string theme) =>
|
|
|
|
|
(MarkupString)((await Get<ThemePreviewResponseData>($"/theme/{theme}/preview"))?.Html ?? string.Empty);
|
|
|
|
|
|
|
|
|
|
public async Task<BasicResponseData?> PostProfilePic(string address, FileResult image) =>
|
2024-07-16 02:15:56 +00:00
|
|
|
|
await PostBinary<BasicResponseData>($"/address/{address}/pfp", fileResult: image);
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Auth
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add the api token into the default headers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="token">The api token</param>
|
|
|
|
|
public 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Remove the api token from the default headers
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void RemoveToken() {
|
|
|
|
|
_client.DefaultRequestHeaders.Remove("Authorization");
|
|
|
|
|
}
|
2024-05-31 13:16:09 +00:00
|
|
|
|
|
2024-06-05 12:41:08 +00:00
|
|
|
|
public async Task<string?> OAuth(string code, string client_id, string client_secret, string redirect_uri) {
|
2024-05-31 13:16:09 +00:00
|
|
|
|
string? token = null;
|
2024-06-06 05:20:09 +00:00
|
|
|
|
string uri = $"/oauth/?code={code}&client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}&scope=everything";
|
2024-05-31 13:16:09 +00:00
|
|
|
|
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-07-16 02:15:56 +00:00
|
|
|
|
#endregion
|
2024-06-11 00:36:48 +00:00
|
|
|
|
|
|
|
|
|
public async Task<MarkupString?> GetHtml(string url) {
|
|
|
|
|
string? raw = null;
|
|
|
|
|
try {
|
|
|
|
|
HttpResponseMessage response = await _client.GetAsync(url);
|
|
|
|
|
if (response.IsSuccessStatusCode) {
|
|
|
|
|
raw = await response.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
return string.IsNullOrEmpty(raw) ? null : (MarkupString)raw;
|
|
|
|
|
}
|
2024-05-30 01:06:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|