38 lines
1.4 KiB
C#
38 lines
1.4 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using Prefab.Web.Client.Models.Catalog;
|
|
using Prefab.Web.Client.Models.Shared;
|
|
|
|
namespace Prefab.Web.Client.Services;
|
|
|
|
public interface IProductListingService
|
|
{
|
|
Task<Result<ProductListingModel>> GetCategoryProducts(string categorySlug, CancellationToken cancellationToken);
|
|
}
|
|
|
|
public sealed class ProductListingService(HttpClient httpClient) : IProductListingService
|
|
{
|
|
public async Task<Result<ProductListingModel>> GetCategoryProducts(string categorySlug, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(categorySlug);
|
|
|
|
using var response = await httpClient.GetAsync(
|
|
$"/bff/catalog/categories/{Uri.EscapeDataString(categorySlug)}" +
|
|
"/models",
|
|
cancellationToken);
|
|
|
|
var payload = await response.Content.ReadFromJsonAsync<Result<ProductListingModel>>(cancellationToken: cancellationToken);
|
|
|
|
if (payload is not null)
|
|
{
|
|
return payload;
|
|
}
|
|
|
|
var statusCode = (HttpStatusCode)response.StatusCode;
|
|
var exception = new InvalidOperationException($"Response {(int)statusCode} did not contain a valid payload.");
|
|
var unexpectedProblem = ResultProblem.Unexpected("Failed to retrieve category products.", exception, statusCode);
|
|
|
|
return Result<ProductListingModel>.Failure(unexpectedProblem);
|
|
}
|
|
}
|