Fix for retrieving pastes while logged in
This commit is contained in:
parent
d14ee65af4
commit
a25df4368e
2 changed files with 20 additions and 10 deletions
|
@ -1,13 +1,9 @@
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Components.Forms;
|
|
||||||
using Neighbourhood.omg.lol.Models;
|
using Neighbourhood.omg.lol.Models;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Neighbourhood.omg.lol
|
namespace Neighbourhood.omg.lol
|
||||||
{
|
{
|
||||||
|
@ -15,6 +11,7 @@ namespace Neighbourhood.omg.lol
|
||||||
HttpClient _client;
|
HttpClient _client;
|
||||||
JsonSerializerOptions _serializerOptions;
|
JsonSerializerOptions _serializerOptions;
|
||||||
public const string BaseUrl = "https://api.omg.lol";
|
public const string BaseUrl = "https://api.omg.lol";
|
||||||
|
private string? apiToken = null;
|
||||||
|
|
||||||
public ApiService(string? token = null) {
|
public ApiService(string? token = null) {
|
||||||
_client = new HttpClient();
|
_client = new HttpClient();
|
||||||
|
@ -94,7 +91,7 @@ namespace Neighbourhood.omg.lol
|
||||||
/// <param name="file">A FileResult for the file to send in the body of the request as binary data</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>
|
/// <param name="cancellationToken">A cancellation token</param>
|
||||||
/// <returns>The returned data if successful, otherwise default</returns>
|
/// <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)
|
private async Task<TResponse?> Request<TResponse, TData>(string uri, HttpMethod method, TData? data = default, FileResult? file = null, bool useAuthToken = true, CancellationToken cancellationToken = default)
|
||||||
where TResponse : IOmgLolResponseData
|
where TResponse : IOmgLolResponseData
|
||||||
{
|
{
|
||||||
TResponse? responseData = default;
|
TResponse? responseData = default;
|
||||||
|
@ -118,6 +115,12 @@ namespace Neighbourhood.omg.lol
|
||||||
string json = JsonSerializer.Serialize(data, _serializerOptions);
|
string json = JsonSerializer.Serialize(data, _serializerOptions);
|
||||||
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(useAuthToken) {
|
||||||
|
if (apiToken == null) apiToken = Task.Run(() => SecureStorage.GetAsync("accounttoken")).GetAwaiter().GetResult();
|
||||||
|
if (apiToken != null) request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiToken);
|
||||||
|
}
|
||||||
|
|
||||||
HttpResponseMessage response = await _client.SendAsync(request, cancellationToken: cancellationToken);
|
HttpResponseMessage response = await _client.SendAsync(request, cancellationToken: cancellationToken);
|
||||||
responseData = await DecodeResponse<TResponse>(response, cancellationToken);
|
responseData = await DecodeResponse<TResponse>(response, cancellationToken);
|
||||||
|
|
||||||
|
@ -131,9 +134,9 @@ namespace Neighbourhood.omg.lol
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET request
|
// GET request
|
||||||
private async Task<TResponse?> Get<TResponse>(string uri, CancellationToken cancellationToken = default)
|
private async Task<TResponse?> Get<TResponse>(string uri, bool useAuthToken = true, CancellationToken cancellationToken = default)
|
||||||
where TResponse : IOmgLolResponseData
|
where TResponse : IOmgLolResponseData
|
||||||
=> await Request<TResponse, object>(uri, HttpMethod.Get, cancellationToken: cancellationToken);
|
=> await Request<TResponse, object>(uri, HttpMethod.Get, useAuthToken: useAuthToken, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
// POST request
|
// POST request
|
||||||
private async Task<TResponse?> Post<TResponse, TData>(string uri, TData data, CancellationToken cancellationToken = default)
|
private async Task<TResponse?> Post<TResponse, TData>(string uri, TData data, CancellationToken cancellationToken = default)
|
||||||
|
@ -242,7 +245,9 @@ namespace Neighbourhood.omg.lol
|
||||||
await PostBinary<BasicResponseData>($"/address/{address}/pfp", fileResult: image);
|
await PostBinary<BasicResponseData>($"/address/{address}/pfp", fileResult: image);
|
||||||
|
|
||||||
public async Task<List<Paste>> GetPastes(string address) =>
|
public async Task<List<Paste>> GetPastes(string address) =>
|
||||||
(await Get<PastesResponseData>($"/address/{address}/pastebin"))?.Pastebin ?? new List<Paste>();
|
(await Get<PastesResponseData>($"/address/{address}/pastebin", useAuthToken: false))?.Pastebin ?? new List<Paste>();
|
||||||
|
public async Task<List<Paste>> GetMyPastes(string address) =>
|
||||||
|
(await Get<PastesResponseData>($"/address/{address}/pastebin", useAuthToken: true))?.Pastebin ?? new List<Paste>();
|
||||||
|
|
||||||
public async Task<BasicResponseData?> DeletePaste(string address, string title) =>
|
public async Task<BasicResponseData?> DeletePaste(string address, string title) =>
|
||||||
await Delete<BasicResponseData>($"/address/{address}/pastebin/{title}");
|
await Delete<BasicResponseData>($"/address/{address}/pastebin/{title}");
|
||||||
|
@ -259,7 +264,7 @@ namespace Neighbourhood.omg.lol
|
||||||
/// <param name="token">The api token</param>
|
/// <param name="token">The api token</param>
|
||||||
public void AddToken(string? token = null) {
|
public void AddToken(string? token = null) {
|
||||||
if (token == null) token = Task.Run(() => SecureStorage.GetAsync("accounttoken")).GetAwaiter().GetResult();
|
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);
|
if (token != null) apiToken = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -289,8 +289,13 @@ namespace Neighbourhood.omg.lol {
|
||||||
public async Task<List<Paste>?> GetPastes(string address, bool forceRefresh = false) {
|
public async Task<List<Paste>?> GetPastes(string address, bool forceRefresh = false) {
|
||||||
CachedAddress = address;
|
CachedAddress = address;
|
||||||
if (forceRefresh || this.CachedAddressPastes == null || this.CachedAddressPastes.Count == 0) {
|
if (forceRefresh || this.CachedAddressPastes == null || this.CachedAddressPastes.Count == 0) {
|
||||||
|
if (AddressNames?.Contains(address) ?? false) {
|
||||||
|
CachedAddressPastes = (await api.GetMyPastes(address)) ?? new List<Paste>();
|
||||||
|
}
|
||||||
|
else {
|
||||||
CachedAddressPastes = (await api.GetPastes(address)) ?? new List<Paste>();
|
CachedAddressPastes = (await api.GetPastes(address)) ?? new List<Paste>();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return CachedAddressPastes;
|
return CachedAddressPastes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue