109 lines
3.5 KiB
C#
109 lines
3.5 KiB
C#
|
using System.Net.Http.Json;
|
||
|
using System.Net;
|
||
|
using System.Text.Json;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
|
||
|
namespace presence_client.ApiClients;
|
||
|
|
||
|
public abstract class BaseApiClient
|
||
|
{
|
||
|
protected readonly HttpClient _httpClient;
|
||
|
protected readonly ILogger _logger;
|
||
|
protected readonly JsonSerializerOptions _jsonOptions;
|
||
|
|
||
|
protected BaseApiClient(IHttpClientFactory httpClientFactory, ILogger logger)
|
||
|
{
|
||
|
_httpClient = httpClientFactory.CreateClient("PresenceApi");
|
||
|
_logger = logger;
|
||
|
|
||
|
_jsonOptions = new JsonSerializerOptions
|
||
|
{
|
||
|
PropertyNameCaseInsensitive = true,
|
||
|
WriteIndented = true
|
||
|
};
|
||
|
}
|
||
|
|
||
|
protected async Task<T> GetAsync<T>(string endpoint)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
var fullEndpoint = endpoint.StartsWith("api/") ? endpoint : $"api/{endpoint}";
|
||
|
_logger.LogDebug("Sending GET request to {Endpoint}", fullEndpoint);
|
||
|
|
||
|
var response = await _httpClient.GetAsync(fullEndpoint);
|
||
|
|
||
|
if (response.StatusCode == HttpStatusCode.NotFound)
|
||
|
{
|
||
|
_logger.LogWarning("Endpoint {Endpoint} returned 404 Not Found", fullEndpoint);
|
||
|
return default;
|
||
|
}
|
||
|
|
||
|
return await HandleResponse<T>(response);
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
_logger.LogError(ex, "Error in GET request to {Endpoint}", endpoint);
|
||
|
return default;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected async Task<TResponse> PostAsync<TRequest, TResponse>(string endpoint, TRequest data)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
_logger.LogDebug("Sending POST request to {Endpoint} with data {@Data}", endpoint, data);
|
||
|
var response = await _httpClient.PostAsJsonAsync(endpoint, data, _jsonOptions);
|
||
|
return await HandleResponse<TResponse>(response);
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
_logger.LogError(ex, "Error in POST request to {Endpoint}", endpoint);
|
||
|
return default;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected async Task<bool> DeleteAsync(string endpoint)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
_logger.LogDebug("Sending DELETE request to {Endpoint}", endpoint);
|
||
|
var response = await _httpClient.DeleteAsync(endpoint);
|
||
|
|
||
|
if (!response.IsSuccessStatusCode)
|
||
|
{
|
||
|
_logger.LogWarning("DELETE request to {Endpoint} failed with status {StatusCode}",
|
||
|
endpoint, response.StatusCode);
|
||
|
}
|
||
|
|
||
|
return response.IsSuccessStatusCode;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
_logger.LogError(ex, "Error in DELETE request to {Endpoint}", endpoint);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private async Task<T> HandleResponse<T>(HttpResponseMessage response)
|
||
|
{
|
||
|
if (!response.IsSuccessStatusCode)
|
||
|
{
|
||
|
var content = await response.Content.ReadAsStringAsync();
|
||
|
_logger.LogError("Request failed with status {StatusCode}. Response: {Response}",
|
||
|
response.StatusCode, content);
|
||
|
return default;
|
||
|
}
|
||
|
|
||
|
try
|
||
|
{
|
||
|
var result = await response.Content.ReadFromJsonAsync<T>(_jsonOptions);
|
||
|
_logger.LogDebug("Request succeeded with response {@Response}", result);
|
||
|
return result;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
_logger.LogError(ex, "Error deserializing response to type {Type}", typeof(T).Name);
|
||
|
return default;
|
||
|
}
|
||
|
}
|
||
|
}
|