Skip to content

Commit 4154782

Browse files
committed
Minor refactor to HttpCommunication
1 parent cbacebc commit 4154782

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/BizHawk.Client.Common/Api/HttpCommunication.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Net.Http;
34
using System.Threading.Tasks;
45

@@ -8,6 +9,8 @@ namespace BizHawk.Client.Common
89
{
910
public sealed class HttpCommunication
1011
{
12+
private const string MIME_FORM_URLENC = "application/x-www-form-urlencoded";
13+
1114
private readonly HttpClient _client = new HttpClient();
1215

1316
private readonly Func<byte[]> _takeScreenshotCallback;
@@ -30,6 +33,15 @@ public HttpCommunication(Func<byte[]> takeScreenshotCallback, string getURL, str
3033
_client.DefaultRequestHeaders.UserAgent.ParseAdd(VersionInfo.UserAgentEscaped);
3134
}
3235

36+
private HttpContent ContentObjectFor(string payload, [ConstantExpected] string mimeType)
37+
=> mimeType switch
38+
{
39+
MIME_FORM_URLENC => new FormUrlEncodedContent([ new("payload", payload) ]),
40+
#pragma warning disable BHI1005 // exception type
41+
_ => throw new NotImplementedException()
42+
#pragma warning restore BHI1005
43+
};
44+
3345
public string ExecGet(string url = null) => Get(url ?? GetUrl).Result;
3446

3547
/// <inheritdoc cref="ExecPostAsForm"/>
@@ -46,7 +58,7 @@ public string ExecPostAsForm(string url = null, string payload = "")
4658
{
4759
return Post(
4860
url ?? PostUrl,
49-
new FormUrlEncodedContent(new Dictionary<string, string> { ["payload"] = payload }),
61+
ContentObjectFor(payload: payload, mimeType: MIME_FORM_URLENC),
5062
sendAdvanceRequest: payload.Length >= ExpectContinueThreshold
5163
).Result;
5264
}
@@ -64,7 +76,7 @@ public async Task<string> Get(string url)
6476
return null;
6577
}
6678

67-
public async Task<string> Post(string url, FormUrlEncodedContent content, bool sendAdvanceRequest = false)
79+
public async Task<string> Post(string url, HttpContent content, bool sendAdvanceRequest = false)
6880
{
6981
_client.DefaultRequestHeaders.ConnectionClose = true;
7082
_client.DefaultRequestHeaders.ExpectContinue = sendAdvanceRequest;

0 commit comments

Comments
 (0)