From eccc481b4479a0ca16511b5c00befbb5ece666b3 Mon Sep 17 00:00:00 2001 From: Wave Dev <66224387+wavedevgit@users.noreply.github.com> Date: Fri, 13 Mar 2026 11:15:31 +0000 Subject: [PATCH] Cache headers Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../Discord/DiscordClient.cs | 84 ++++++++++--------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/DiscordChatExporter.Core/Discord/DiscordClient.cs b/DiscordChatExporter.Core/Discord/DiscordClient.cs index 287117c4..70708977 100644 --- a/DiscordChatExporter.Core/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Core/Discord/DiscordClient.cs @@ -75,51 +75,59 @@ public class DiscordClient( { try { - using var apiReq = new HttpRequestMessage( - HttpMethod.Post, - "https://cordapi.dolfi.es/api/v2/properties/web" - ); + // Cache headers from the external API so we don't make this request on every call. + static Dictionary? cachedHeaders; - using var apiRes = await Http.Client.SendAsync(apiReq, innerCancellationToken); - apiRes.EnsureSuccessStatusCode(); - - var apiJson = await apiRes.Content.ReadAsStringAsync(innerCancellationToken); - - using var doc = JsonDocument.Parse(apiJson); - - var root = doc.RootElement; - - string xspBase64 = root.GetProperty("encoded").GetString()!; - - var properties = root.GetProperty("properties"); - - string userAgent = proprties.GetProperty("user_agent").GetString()!; - string browserVersion = proprties.GetProperty("browser_version").GetString()!; - string osType = properties.GetProperty("os").GetString()!; - - string chromeMajor = browserVersion.Split('.')[0]; - - var headers = new Dictionary + if (cachedHeaders is null) { - ["sec-ch-ua-platform"] = $"\"{osType}\"", - ["referer"] = "https://discord.com/app", - ["x-debug-options"] = "bugReporterEnabled", - ["accept-language"] = "en-US,en;q=0.9", + using var apiReq = new HttpRequestMessage( + HttpMethod.Post, + "https://cordapi.dolfi.es/api/v2/properties/web" + ); - ["sec-ch-ua"] = - $"\"Chromium\";v=\"{chromeMajor}\", \"Not;A=Brand\";v=\"99\"", + using var apiRes = await Http.Client.SendAsync(apiReq, innerCancellationToken); + apiRes.EnsureSuccessStatusCode(); - ["sec-ch-ua-mobile"] = "?0", + var apiJson = await apiRes.Content.ReadAsStringAsync(innerCancellationToken); - ["x-discord-timezone"] = "Europe/Warsaw", - ["x-context-properties"] = "eyJsb2NhdGlvbiI6Ii9hcHAifQ==", - ["x-discord-locale"] = "en-US", + using var doc = JsonDocument.Parse(apiJson); - ["user-agent"] = userAgent, - ["x-super-properties"] = xspBase64, - }; + var root = doc.RootElement; - foreach (var kv in headers) + string xspBase64 = root.GetProperty("encoded").GetString()!; + + var properties = root.GetProperty("properties"); + + string userAgent = properties.GetProperty("user_agent").GetString()!; + string browserVersion = properties.GetProperty("browser_version").GetString()!; + string osType = properties.GetProperty("os").GetString()!; + + string chromeMajor = browserVersion.Split('.')[0]; + + var headers = new Dictionary + { + ["sec-ch-ua-platform"] = $"\"{osType}\"", + ["referer"] = "https://discord.com/app", + ["x-debug-options"] = "bugReporterEnabled", + ["accept-language"] = "en-US,en;q=0.9", + + ["sec-ch-ua"] = + $"\"Chromium\";v=\"{chromeMajor}\", \"Not;A=Brand\";v=\"99\"", + + ["sec-ch-ua-mobile"] = "?0", + + ["x-discord-timezone"] = "Europe/Warsaw", + ["x-context-properties"] = "eyJsb2NhdGlvbiI6Ii9hcHAifQ==", + ["x-discord-locale"] = "en-US", + + ["user-agent"] = userAgent, + ["x-super-properties"] = xspBase64, + }; + + cachedHeaders = headers; + } + + foreach (var kv in cachedHeaders) request.Headers.TryAddWithoutValidation(kv.Key, kv.Value); } catch { }