using DeepDrftModels.DTOs;
using Models.Common;
using NetBlocks.Models;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
namespace DeepDrftPublic.Client.Clients;
///
/// HTTP client for the release read surface (Phase 9). Uses the named "DeepDrft.API"
/// client like : on WASM it points at the public host and proxies
/// through ReleaseProxyController; on SSR prerender it points directly at DeepDrftAPI.
/// All routes are unauthenticated reads. Responses deserialize as bare DTOs (no ApiResultDto
/// envelope), matching the API's Ok(value) shape.
///
public class ReleaseClient
{
private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true };
private readonly HttpClient _http;
public ReleaseClient(IHttpClientFactory httpClientFactory)
{
_http = httpClientFactory.CreateClient("DeepDrft.API");
}
public async Task>> GetPaged(
string? medium,
int page,
int pageSize,
string? sortColumn = null,
bool sortDescending = false,
string? search = null,
string? genre = null)
{
var queryArgs = new Dictionary
{
["page"] = page.ToString(),
["pageSize"] = pageSize.ToString()
};
if (!string.IsNullOrEmpty(medium))
queryArgs["medium"] = medium;
if (!string.IsNullOrEmpty(search))
queryArgs["q"] = search;
if (!string.IsNullOrEmpty(genre))
queryArgs["genre"] = genre;
if (!string.IsNullOrEmpty(sortColumn))
queryArgs["sortColumn"] = sortColumn;
if (sortDescending)
queryArgs["sortDescending"] = "true";
string query = QueryString.Create(queryArgs).ToString();
var response = await _http.GetAsync($"api/release{query}");
if (!response.IsSuccessStatusCode)
return ApiResult>.CreateFailResult($"HTTP {(int)response.StatusCode}");
var json = await response.Content.ReadAsStringAsync();
var paged = JsonSerializer.Deserialize>(json, JsonOptions);
return paged is not null
? ApiResult>.CreatePassResult(paged)
: ApiResult>.CreateFailResult("Failed to deserialize response");
}
public async Task> GetByEntryKey(string entryKey)
{
var response = await _http.GetAsync($"api/release/{Uri.EscapeDataString(entryKey)}");
if (!response.IsSuccessStatusCode)
return ApiResult.CreateFailResult($"HTTP {(int)response.StatusCode}");
var json = await response.Content.ReadAsStringAsync();
var release = JsonSerializer.Deserialize(json, JsonOptions);
return release is not null
? ApiResult.CreatePassResult(release)
: ApiResult.CreateFailResult("Failed to deserialize response");
}
}