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.Forms;
|
||||
using Neighbourhood.omg.lol.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
|
||||
namespace Neighbourhood.omg.lol
|
||||
{
|
||||
|
@ -15,6 +11,7 @@ namespace Neighbourhood.omg.lol
|
|||
HttpClient _client;
|
||||
JsonSerializerOptions _serializerOptions;
|
||||
public const string BaseUrl = "https://api.omg.lol";
|
||||
private string? apiToken = null;
|
||||
|
||||
public ApiService(string? token = null) {
|
||||
_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="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)
|
||||
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
|
||||
{
|
||||
TResponse? responseData = default;
|
||||
|
@ -118,6 +115,12 @@ namespace Neighbourhood.omg.lol
|
|||
string json = JsonSerializer.Serialize(data, _serializerOptions);
|
||||
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);
|
||||
responseData = await DecodeResponse<TResponse>(response, cancellationToken);
|
||||
|
||||
|
@ -131,9 +134,9 @@ namespace Neighbourhood.omg.lol
|
|||
}
|
||||
|
||||
// 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
|
||||
=> await Request<TResponse, object>(uri, HttpMethod.Get, cancellationToken: cancellationToken);
|
||||
=> await Request<TResponse, object>(uri, HttpMethod.Get, useAuthToken: useAuthToken, cancellationToken: cancellationToken);
|
||||
|
||||
// POST request
|
||||
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);
|
||||
|
||||
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) =>
|
||||
await Delete<BasicResponseData>($"/address/{address}/pastebin/{title}");
|
||||
|
@ -259,7 +264,7 @@ namespace Neighbourhood.omg.lol
|
|||
/// <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);
|
||||
if (token != null) apiToken = token;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -289,7 +289,12 @@ namespace Neighbourhood.omg.lol {
|
|||
public async Task<List<Paste>?> GetPastes(string address, bool forceRefresh = false) {
|
||||
CachedAddress = address;
|
||||
if (forceRefresh || this.CachedAddressPastes == null || this.CachedAddressPastes.Count == 0) {
|
||||
CachedAddressPastes = (await api.GetPastes(address)) ?? new List<Paste>();
|
||||
if (AddressNames?.Contains(address) ?? false) {
|
||||
CachedAddressPastes = (await api.GetMyPastes(address)) ?? new List<Paste>();
|
||||
}
|
||||
else {
|
||||
CachedAddressPastes = (await api.GetPastes(address)) ?? new List<Paste>();
|
||||
}
|
||||
}
|
||||
return CachedAddressPastes;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue