From 13346b04db6d657216a6653b82d5f85cf2fc3295 Mon Sep 17 00:00:00 2001 From: KazWolfe Date: Tue, 28 Nov 2023 13:52:38 -0800 Subject: [PATCH] Remove second HttpClient from PluginRepository (#1551) Co-authored-by: goat <16760685+goaaats@users.noreply.github.com> --- .../Plugin/Internal/Types/PluginRepository.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Dalamud/Plugin/Internal/Types/PluginRepository.cs b/Dalamud/Plugin/Internal/Types/PluginRepository.cs index 18c528910..8de25aa08 100644 --- a/Dalamud/Plugin/Internal/Types/PluginRepository.cs +++ b/Dalamud/Plugin/Internal/Types/PluginRepository.cs @@ -6,12 +6,14 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Threading; using System.Threading.Tasks; using Dalamud.Logging.Internal; using Dalamud.Networking.Http; using Dalamud.Plugin.Internal.Types.Manifest; using Dalamud.Utility; + using Newtonsoft.Json; namespace Dalamud.Plugin.Internal.Types; @@ -26,8 +28,9 @@ internal class PluginRepository /// public const string MainRepoUrl = "https://kamori.goats.dev/Plugin/PluginMaster"; - private static readonly ModuleLog Log = new("PLUGINR"); + private const int HttpRequestTimeoutSeconds = 20; + private static readonly ModuleLog Log = new("PLUGINR"); private readonly HttpClient httpClient; /// @@ -112,7 +115,8 @@ internal class PluginRepository { Log.Information($"Fetching repo: {this.PluginMasterUrl}"); - using var response = await this.httpClient.GetAsync(this.PluginMasterUrl); + using var response = await this.GetPluginMaster(this.PluginMasterUrl); + response.EnsureSuccessStatusCode(); var data = await response.Content.ReadAsStringAsync(); @@ -204,4 +208,17 @@ internal class PluginRepository return true; } + + private async Task GetPluginMaster(string url, int timeout = HttpRequestTimeoutSeconds) + { + var httpClient = Service.Get().SharedHttpClient; + + var request = new HttpRequestMessage(HttpMethod.Get, url); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + request.Headers.CacheControl = new CacheControlHeaderValue { NoCache = true }; + + using var requestCts = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)); + + return await httpClient.SendAsync(request, requestCts.Token); + } }