From a25df4368ebaa77cd23dc1c558cf28e5cd17b05c Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Thu, 25 Jul 2024 13:27:48 +1000 Subject: [PATCH] Fix for retrieving pastes while logged in --- Classes/ApiService.cs | 23 ++++++++++++++--------- Classes/State.cs | 7 ++++++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Classes/ApiService.cs b/Classes/ApiService.cs index 60ccc54..cd8be96 100644 --- a/Classes/ApiService.cs +++ b/Classes/ApiService.cs @@ -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 /// A FileResult for the file to send in the body of the request as binary data /// A cancellation token /// The returned data if successful, otherwise default - private async Task Request(string uri, HttpMethod method, TData? data = default, FileResult? file = null, CancellationToken cancellationToken = default) + private async Task Request(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(response, cancellationToken); @@ -131,9 +134,9 @@ namespace Neighbourhood.omg.lol } // GET request - private async Task Get(string uri, CancellationToken cancellationToken = default) + private async Task Get(string uri, bool useAuthToken = true, CancellationToken cancellationToken = default) where TResponse : IOmgLolResponseData - => await Request(uri, HttpMethod.Get, cancellationToken: cancellationToken); + => await Request(uri, HttpMethod.Get, useAuthToken: useAuthToken, cancellationToken: cancellationToken); // POST request private async Task Post(string uri, TData data, CancellationToken cancellationToken = default) @@ -242,7 +245,9 @@ namespace Neighbourhood.omg.lol await PostBinary($"/address/{address}/pfp", fileResult: image); public async Task> GetPastes(string address) => - (await Get($"/address/{address}/pastebin"))?.Pastebin ?? new List(); + (await Get($"/address/{address}/pastebin", useAuthToken: false))?.Pastebin ?? new List(); + public async Task> GetMyPastes(string address) => + (await Get($"/address/{address}/pastebin", useAuthToken: true))?.Pastebin ?? new List(); public async Task DeletePaste(string address, string title) => await Delete($"/address/{address}/pastebin/{title}"); @@ -259,7 +264,7 @@ namespace Neighbourhood.omg.lol /// The api token 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; } /// diff --git a/Classes/State.cs b/Classes/State.cs index c47f4c8..399dcff 100644 --- a/Classes/State.cs +++ b/Classes/State.cs @@ -289,7 +289,12 @@ namespace Neighbourhood.omg.lol { public async Task?> GetPastes(string address, bool forceRefresh = false) { CachedAddress = address; if (forceRefresh || this.CachedAddressPastes == null || this.CachedAddressPastes.Count == 0) { - CachedAddressPastes = (await api.GetPastes(address)) ?? new List(); + if (AddressNames?.Contains(address) ?? false) { + CachedAddressPastes = (await api.GetMyPastes(address)) ?? new List(); + } + else { + CachedAddressPastes = (await api.GetPastes(address)) ?? new List(); + } } return CachedAddressPastes; }