OrangeGuidanceTomestone/client/Helpers/ServerHelper.cs

33 lines
1.1 KiB
C#
Raw Normal View History

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