This commit is contained in:
2024-07-03 22:18:32 +02:00
Unverified
parent 4b333878b8
commit 008dcdf4ec
88 changed files with 3389 additions and 644 deletions

View File

@@ -0,0 +1,43 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace WatchIt.Common.Services.HttpClient;
public class HttpClientService(System.Net.Http.HttpClient httpClient) : IHttpClientService
{
#region PUBLIC METHODS
public async Task<HttpResponse> SendRequestAsync(HttpRequest request)
{
HttpMethod method = request.MethodType switch
{
HttpMethodType.Get => HttpMethod.Get,
HttpMethodType.Post => HttpMethod.Post,
HttpMethodType.Put => HttpMethod.Put,
HttpMethodType.Patch => HttpMethod.Patch,
HttpMethodType.Delete => HttpMethod.Delete,
_ => throw new ArgumentOutOfRangeException()
};
HttpRequestMessage httpRequest = new HttpRequestMessage(method, request.FullUrl);
if (request.Body is not null)
{
string json = JsonSerializer.Serialize(request.Body);
HttpContent content = new StringContent(json);
content.Headers.ContentType!.MediaType = "application/json";
httpRequest.Content = content;
}
foreach (KeyValuePair<string, string> header in request.Headers)
{
httpRequest.Headers.Add(header.Key, header.Value);
}
HttpResponseMessage response = await httpClient.SendAsync(httpRequest);
return new HttpResponse(response);
}
#endregion
}