From 41547824aa29eef70229ea0e2abdd75d36ab2091 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 17 Jan 2025 01:27:05 +1000 Subject: [PATCH] Minor refactor to `HttpCommunication` --- .../Api/HttpCommunication.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/BizHawk.Client.Common/Api/HttpCommunication.cs b/src/BizHawk.Client.Common/Api/HttpCommunication.cs index 29bf152017..be64aec4bd 100644 --- a/src/BizHawk.Client.Common/Api/HttpCommunication.cs +++ b/src/BizHawk.Client.Common/Api/HttpCommunication.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Net.Http; using System.Threading.Tasks; @@ -8,6 +9,8 @@ namespace BizHawk.Client.Common { public sealed class HttpCommunication { + private const string MIME_FORM_URLENC = "application/x-www-form-urlencoded"; + private readonly HttpClient _client = new HttpClient(); private readonly Func _takeScreenshotCallback; @@ -30,6 +33,15 @@ namespace BizHawk.Client.Common _client.DefaultRequestHeaders.UserAgent.ParseAdd(VersionInfo.UserAgentEscaped); } + private HttpContent ContentObjectFor(string payload, [ConstantExpected] string mimeType) + => mimeType switch + { + MIME_FORM_URLENC => new FormUrlEncodedContent([ new("payload", payload) ]), +#pragma warning disable BHI1005 // exception type + _ => throw new NotImplementedException() +#pragma warning restore BHI1005 + }; + public string ExecGet(string url = null) => Get(url ?? GetUrl).Result; /// @@ -46,7 +58,7 @@ namespace BizHawk.Client.Common { return Post( url ?? PostUrl, - new FormUrlEncodedContent(new Dictionary { ["payload"] = payload }), + ContentObjectFor(payload: payload, mimeType: MIME_FORM_URLENC), sendAdvanceRequest: payload.Length >= ExpectContinueThreshold ).Result; } @@ -64,7 +76,7 @@ namespace BizHawk.Client.Common return null; } - public async Task Post(string url, FormUrlEncodedContent content, bool sendAdvanceRequest = false) + public async Task Post(string url, HttpContent content, bool sendAdvanceRequest = false) { _client.DefaultRequestHeaders.ConnectionClose = true; _client.DefaultRequestHeaders.ExpectContinue = sendAdvanceRequest;