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