OrangeGuidanceTomestone/client/Helpers/ServerHelper.cs
2022-09-07 23:13:55 -04:00

33 lines
1.1 KiB
C#

using System.Net.Http.Headers;
namespace OrangeGuidanceTomestone.Helpers;
internal static class ServerHelper {
internal static HttpRequestMessage GetRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
if (!tail.StartsWith('/')) {
tail = '/' + tail;
}
var url = $"https://tryfingerbuthole.anna.lgbt{tail}";
var req = new HttpRequestMessage(method, url);
if (content != null) {
req.Content = content;
if (contentType != null) {
req.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
}
}
if (apiKey != null) {
req.Headers.Add("X-Api-Key", apiKey);
}
return req;
}
internal static async Task<HttpResponseMessage> SendRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
var req = GetRequest(apiKey, method, tail, contentType, content);
return await new HttpClient().SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
}
}