using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net.Http.Json; using System.Text.Json; using System.Threading.Tasks; using Commerce.Contracts.DTOs; using Generic.Contracts.Generics; using Generic.Services; namespace ECommerceModule.Services; public class ProductAPIConsumer { private readonly HttpClient client; public ProductAPIConsumer(IHttpClientFactory httpClientFactory) { this.client = httpClientFactory.CreateClient("Commerce"); } public virtual async Task GetProduct(string id) { try { var culture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName ?? "ar"; var response = await client.GetFromJsonAsync($"product/{id}"); return response!; } catch (HttpRequestException) { // _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint); throw; } } public virtual async Task<(List, int)> FetchProduct(ProductFilter filter, Pagination pagination) { try { var culture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName ?? "ar"; var filterWithCulture = new ProductFilter { Name = filter?.Name, Price = filter?.Price, Discount = filter?.Discount, IsBundle = filter?.IsBundle, CategoryId = filter?.CategoryId, Random = filter?.Random, Language = culture }; var query = new AdvancedQueryBuilder() .AddPreText("products?") .AddModels(filterWithCulture, pagination) .ToString(); var products = new List(); var response = await client.GetAsync(query); var itemsCount = 0; if (response.IsSuccessStatusCode) { if (response.Headers.TryGetValues("x-pagination", out var values)) { itemsCount = int.Parse(values.First()); } products = await response.Content.ReadFromJsonAsync>(); } return (products ?? new List(), itemsCount); } catch (HttpRequestException) { throw; } } // public virtual async Task> GetRandomProducts() // { // try // { // var response = await client.GetFromJsonAsync>($"product/random"); // return response; // } // catch (HttpRequestException ex) // { // // _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint); // throw; // } // } public virtual async Task UpdateProduct(ProductVM product) { try { var response = await client.PutAsJsonAsync( "product", product, new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ); return response.IsSuccessStatusCode; } catch (HttpRequestException) { // _logger.LogError(ex, "GET request failed for endpoint: Endpoint", endpoint); throw; } } public virtual async Task CreateProduct(ProductVM product) { try { var response = await client.PostAsJsonAsync( "product", product, new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ); return response.IsSuccessStatusCode; } catch (HttpRequestException) { // _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint); throw; } } public virtual async Task DeleteProduct(string id) { try { var response = await client.DeleteAsync($"product/{id}"); return response.IsSuccessStatusCode; } catch (HttpRequestException) { // _logger.LogError(ex, "GET request failed for endpoint: {Endpoint}", endpoint); throw; } } }