Init
This commit is contained in:
32
Prefab.Web.Client/Services/HomePageService.cs
Normal file
32
Prefab.Web.Client/Services/HomePageService.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user