Files
prefab-page-detail/Prefab.Web.Client/Services/ICategoriesPageService.cs
2025-10-27 17:39:18 -04:00

31 lines
1.1 KiB
C#

using System.Net.Http.Json;
using Prefab.Web.Client.Models.Shared;
using Prefab.Web.Client.Pages;
namespace Prefab.Web.Client.Services;
public interface ICategoriesPageService
{
Task<Result<CategoriesPageModel>> GetPage(CancellationToken cancellationToken);
}
public sealed class CategoriesPageService(HttpClient httpClient) : ICategoriesPageService
{
public async Task<Result<CategoriesPageModel>> GetPage(CancellationToken cancellationToken)
{
using var response = await httpClient.GetAsync("bff/categories/page", cancellationToken);
var result = await response.Content.ReadFromJsonAsync<Result<CategoriesPageModel>>(cancellationToken);
if (result is not null)
{
return result;
}
var exception = new InvalidOperationException($"Response {(int)response.StatusCode} did not contain a valid payload.");
var unexpectedProblem = ResultProblem.Unexpected("Failed to retrieve categories.", exception, response.StatusCode);
return Result<CategoriesPageModel>.Failure(unexpectedProblem);
}
}