From 8e3a46f8bc9c7d535fbf14772c7b91f1f0aa6210 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 7 Aug 2024 08:26:12 +0100 Subject: [PATCH] mod: impl config for api url --- Mod/API/Authentication.cs | 41 ++++++++++++++++++++++++++++--- Mod/API/Request.cs | 37 +++------------------------- Mod/Configuration/PluginConfig.cs | 16 ++++++++++++ Mod/Consts.cs | 7 ------ Mod/Directory.Build.props | 3 --- Mod/Installers/AppInstaller.cs | 4 +-- Mod/Plugin.cs | 29 +++++++++++++--------- 7 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 Mod/Configuration/PluginConfig.cs delete mode 100644 Mod/Consts.cs diff --git a/Mod/API/Authentication.cs b/Mod/API/Authentication.cs index c1460f3..b30c7e6 100644 --- a/Mod/API/Authentication.cs +++ b/Mod/API/Authentication.cs @@ -1,10 +1,45 @@ -using System; +using ScoreTracker.Configuration; +using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; namespace ScoreTracker.API { + internal class AuthHelper + { + public bool IsLoggedIn; + public string FailReason = ""; + + /// + /// Ensure the user is logged in + /// + /// the task + public async Task EnsureLoggedIn() + { + if (Authentication.IsSignedIn() && await Authentication.ValidateAuthToken()) + { + return; // Already logged in with a valid token + } + + await Authentication.LoginUser( + token => { + IsLoggedIn = true; + Request.PersistHeaders(new Dictionary + { + { "Authorization", $"Bearer {token}" } + }); + }, + reason => + { + FailReason = reason; // Store the reason for failure + Request.HttpClient.DefaultRequestHeaders.Clear(); // Clear headers + } + ); + } + } + internal class Authentication { private static bool _signedIn = false; @@ -54,7 +89,7 @@ namespace ScoreTracker.API } Plugin.Log.Info("Logging in..."); - var request = await Request.PostJsonAsync($"{Consts.ApiUrl}/auth/login", new Dictionary { + var request = await Request.PostJsonAsync($"{PluginConfig.Instance.ApiUrl}/auth/login", new Dictionary { { "ticket", ticket } }, false); if (request.IsSuccessStatusCode) @@ -88,7 +123,7 @@ namespace ScoreTracker.API return false; } - var request = await Request.PostJsonAsync($"{Consts.ApiUrl}/auth/validate", new Dictionary { + var request = await Request.PostJsonAsync($"{PluginConfig.Instance.ApiUrl}/auth/validate", new Dictionary { { "token", _authToken } }, false); diff --git a/Mod/API/Request.cs b/Mod/API/Request.cs index c1844fd..a078555 100644 --- a/Mod/API/Request.cs +++ b/Mod/API/Request.cs @@ -9,36 +9,7 @@ namespace ScoreTracker.API { internal class Request { - private static readonly HttpClient client = new HttpClient(); - - private class AuthHelper - { - public bool IsLoggedIn; - public string FailReason = ""; - - public async Task EnsureLoggedIn() - { - if (Authentication.IsSignedIn() && await Authentication.ValidateAuthToken()) - { - return; // Already logged in with a valid token - } - - await Authentication.LoginUser( - token => { - IsLoggedIn = true; - PersistHeaders(new Dictionary - { - { "Authorization", $"Bearer {token}" } - }); - }, - reason => - { - FailReason = reason; // Store the reason for failure - client.DefaultRequestHeaders.Clear(); // Clear headers - } - ); - } - } + internal static readonly HttpClient HttpClient = new HttpClient(); /// /// Persist the given headers for all future requests @@ -46,10 +17,10 @@ namespace ScoreTracker.API /// the headers to persist public static void PersistHeaders(Dictionary headers) { - client.DefaultRequestHeaders.Clear(); // Clear existing headers + HttpClient.DefaultRequestHeaders.Clear(); // Clear existing headers foreach (var header in headers) { - client.DefaultRequestHeaders.Add(header.Key, header.Value); + HttpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } } @@ -75,7 +46,7 @@ namespace ScoreTracker.API var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); // Send the POST request - var response = await client.PostAsync(url, content); + var response = await HttpClient.PostAsync(url, content); return response; } } diff --git a/Mod/Configuration/PluginConfig.cs b/Mod/Configuration/PluginConfig.cs new file mode 100644 index 0000000..a8b62d9 --- /dev/null +++ b/Mod/Configuration/PluginConfig.cs @@ -0,0 +1,16 @@ +using IPA.Config.Stores; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo(GeneratedStore.AssemblyVisibilityTarget)] +namespace ScoreTracker.Configuration +{ + internal class PluginConfig + { + public static PluginConfig Instance { get; set; } + + /* + * The URL of the API to use + */ + public virtual string ApiUrl { get; set; } = "https://beatsaber.fascinated.cc/api"; + } +} diff --git a/Mod/Consts.cs b/Mod/Consts.cs deleted file mode 100644 index 4d5e788..0000000 --- a/Mod/Consts.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScoreTracker -{ - internal class Consts - { - public static string ApiUrl = "https://beatsaber.fascinated.cc/api"; - } -} diff --git a/Mod/Directory.Build.props b/Mod/Directory.Build.props index b5b1e3c..6f13549 100644 --- a/Mod/Directory.Build.props +++ b/Mod/Directory.Build.props @@ -3,9 +3,6 @@ 4 - true - true - true true diff --git a/Mod/Installers/AppInstaller.cs b/Mod/Installers/AppInstaller.cs index 47e20ad..d2f7ff6 100644 --- a/Mod/Installers/AppInstaller.cs +++ b/Mod/Installers/AppInstaller.cs @@ -1,13 +1,13 @@ using Zenject; -namespace ScoreTracker.Core +namespace ScoreTracker.Installers { internal class AppInstaller : Installer { public override void InstallBindings() { - Plugin.Container = Container; + } } } \ No newline at end of file diff --git a/Mod/Plugin.cs b/Mod/Plugin.cs index a91caa1..4925ba4 100644 --- a/Mod/Plugin.cs +++ b/Mod/Plugin.cs @@ -1,30 +1,35 @@ using IPA; using IPALogger = IPA.Logging.Logger; using SiraUtil.Zenject; -using IPA.Loader; -using Zenject; -using ScoreTracker.Core; using System.Threading.Tasks; using ScoreTracker.API; -using System.Collections.Generic; +using IPA.Config.Stores; +using IPA.Config; +using ScoreTracker.Configuration; +using ScoreTracker.Installers; namespace ScoreTracker { - [Plugin(RuntimeOptions.SingleStartInit)] + [Plugin(RuntimeOptions.DynamicInit)] + [NoEnableDisable] public class Plugin { internal static Plugin Instance { get; private set; } internal static IPALogger Log { get; private set; } - internal static DiContainer Container; // Workaround to access the Zenject container in SceneLoaded [Init] - public Plugin(IPALogger logger, PluginMetadata metadata, Zenjector zenjector) + public Plugin(IPALogger logger, Zenjector zenjector, Config config) { Instance = this; Log = logger; // Setup the logger - // Install our Zenject bindings + // Setup Zenject + zenjector.UseLogger(logger); + zenjector.UseMetadataBinder(); zenjector.Install(Location.App); + + // Setup the config + PluginConfig.Instance = config.Generated(); } [OnStart] @@ -32,11 +37,11 @@ namespace ScoreTracker { Log.Info("OnApplicationStart"); - Task.Run(async () => + // Ensure the user is logged in + Task.Factory.StartNew(async () => { - await Request.PostJsonAsync("http://localhost:7500/test", new Dictionary { - { "boobies", "yes" } - }); + var authHelper = new AuthHelper(); + await authHelper.EnsureLoggedIn(); }); }