40 lines
1.5 KiB
C#
40 lines
1.5 KiB
C#
using DeepDrftModels.DTOs;
|
|
using NetBlocks.Models;
|
|
using System.Text.Json;
|
|
|
|
namespace DeepDrftPublic.Client.Clients;
|
|
|
|
/// <summary>
|
|
/// HTTP client for the public stats read surface. Uses the named <c>"DeepDrft.API"</c> client like
|
|
/// <see cref="TrackClient"/> and <see cref="ReleaseClient"/>: on WASM it points at the public host and
|
|
/// proxies through <c>StatsProxyController</c>; on SSR prerender it points directly at DeepDrftAPI. The
|
|
/// route is an unauthenticated read; the response deserializes as a bare DTO (no ApiResultDto envelope),
|
|
/// matching the API's <c>Ok(value)</c> shape.
|
|
/// </summary>
|
|
public class StatsClient
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true };
|
|
|
|
private readonly HttpClient _http;
|
|
|
|
public StatsClient(IHttpClientFactory httpClientFactory)
|
|
{
|
|
_http = httpClientFactory.CreateClient("DeepDrft.API");
|
|
}
|
|
|
|
public async Task<ApiResult<HomeStatsDto>> GetHomeStats()
|
|
{
|
|
var response = await _http.GetAsync("api/stats/home");
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return ApiResult<HomeStatsDto>.CreateFailResult($"HTTP {(int)response.StatusCode}");
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
var stats = JsonSerializer.Deserialize<HomeStatsDto>(json, JsonOptions);
|
|
|
|
return stats is not null
|
|
? ApiResult<HomeStatsDto>.CreatePassResult(stats)
|
|
: ApiResult<HomeStatsDto>.CreateFailResult("Failed to deserialize response");
|
|
}
|
|
}
|