using Microsoft.Extensions.Logging; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.MobileApp; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Json; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; namespace OnlineSalesAutoCrop.CoreAPI.Services.Services.Integrations; public class IntegrationHttpService : IIntegrationHttpService { private readonly HttpClient _httpClient; private readonly ILogger _logger; public IntegrationHttpService(HttpClient httpClient, ILogger logger) { _httpClient = httpClient; _logger = logger; } public async Task GetCustomerLedgerAsync(string customerNumber, string companyCode, CancellationToken cancellationToken = default) { try { var requestUri = $"http/get_customer_ledger?customer={Uri.EscapeDataString(customerNumber)}&company={Uri.EscapeDataString(companyCode)}"; var response = await _httpClient.GetAsync(requestUri, cancellationToken); if (!response.IsSuccessStatusCode) { var body = await response.Content.ReadAsStringAsync(cancellationToken); _logger.LogError("Customer ledger call failed: {StatusCode} — {Body}", response.StatusCode, body); response.EnsureSuccessStatusCode(); } var payload= await response.Content.ReadFromJsonAsync< SapODataResponse>(cancellationToken: cancellationToken); return payload?.D.Results.FirstOrDefault(); } catch(Exception) { throw; } } public async Task> GetMaterialStockAsync(string plantCode, CancellationToken cancellationToken = default) { try { var requestUri = $"http/get_plantwise_available_stocks?plant={Uri.EscapeDataString(plantCode)}"; var response = await _httpClient.GetAsync(requestUri, cancellationToken); if (!response.IsSuccessStatusCode) { var body = await response.Content.ReadAsStringAsync(cancellationToken); _logger.LogError("Material Stock call failed: {StatusCode} — {Body}", response.StatusCode, body); response.EnsureSuccessStatusCode(); } var payload = await response.Content.ReadFromJsonAsync>(cancellationToken: cancellationToken); return payload?.D.Results; } catch (Exception) { throw; } } public async Task> SendSAPOrdersAsync(GetOrderForSAPResponse request, CancellationToken cancellationToken = default) { try { var requestUri = "http/sales_order"; var options = new JsonSerializerOptions { PropertyNamingPolicy = null }; var json = JsonSerializer.Serialize(request, options); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync(requestUri, content, cancellationToken); if (!response.IsSuccessStatusCode) { var body = await response.Content.ReadAsStringAsync(cancellationToken); _logger.LogError("Save Orders call failed: {StatusCode} — {Body}", response.StatusCode, body); return new Tuple(body, string.Empty); } var responseString = await response.Content.ReadAsStringAsync(cancellationToken); using var document = JsonDocument.Parse(responseString); var contentJson = document.RootElement .GetProperty("content") .GetRawText(); string salesOrder = document.RootElement .GetProperty("content") .GetProperty("properties") .GetProperty("SalesOrder") .GetString(); return new Tuple(contentJson, salesOrder); } catch (Exception) { throw; } } }