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

33 lines
1.1 KiB
C#

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