Integracion_DGA/DAS/RegistrarMedicion.cs

75 lines
2.7 KiB
C#
Raw Normal View History

2025-07-16 20:39:24 -04:00
using DAL;
2025-07-17 09:22:55 -04:00
using Serilog;
using Shared.DTO.Envios_DGA;
2025-07-01 09:59:59 -04:00
using Shared.DTO.VariablesEntorno;
2025-07-16 20:39:24 -04:00
using System.Globalization;
using System.Net;
using System.Text;
using System.Text.Json;
2025-06-24 16:02:27 -04:00
namespace DAS
2025-06-24 14:46:32 -04:00
{
public class RegistrarMedicion
2025-06-24 14:46:32 -04:00
{
2025-06-24 16:02:27 -04:00
private readonly HttpClient _httpClient;
2025-07-16 20:39:24 -04:00
private readonly LogEnvioRepository _logMedicionScadaRepository;
2025-06-24 16:02:27 -04:00
2025-07-16 20:39:24 -04:00
public RegistrarMedicion(HttpClient httpClient, LogEnvioRepository logMedicionScadaRepository)
2025-06-24 16:02:27 -04:00
{
_httpClient = httpClient;
_logMedicionScadaRepository = logMedicionScadaRepository;
2025-06-24 16:02:27 -04:00
}
2025-07-16 20:39:24 -04:00
public async Task<LogMedicionEnvio> EnviarMedicionAsync(DatoDGA medicion, MedicionSubterraneaRequest request, DateTime fechaEnvio)
2025-06-24 16:02:27 -04:00
{
2025-07-16 20:39:24 -04:00
var log = new LogMedicionEnvio();
try
{
2025-07-17 09:22:55 -04:00
var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss-0000", CultureInfo.InvariantCulture);
var json = JsonSerializer.Serialize(request);
var req = new HttpRequestMessage(HttpMethod.Post, $"{SubterraneaApiUrl.BaseUrl}{SubterraneaApiUrl.EndPoint}SSSSS");
req.Headers.Add("codigoObra", medicion.CODIGO_DGA);
req.Headers.Add("timeStampOrigen", timeStamp);
req.Content = new StringContent(json, Encoding.UTF8, "application/json");
log.JSON_ENVIO = json;
log.FECHA_ENVIO = DateTime.UtcNow;
log.ID_DGA_DATO = medicion.ID;
2025-07-16 20:39:24 -04:00
using var response = await _httpClient.SendAsync(req);
string jsonRecibido = await response.Content.ReadAsStringAsync();
2025-07-17 09:22:55 -04:00
log.JSON_RESPUESTA = jsonRecibido;
2025-07-16 20:39:24 -04:00
if (response == null || !response.IsSuccessStatusCode)
{
2025-07-17 09:22:55 -04:00
log.COMPROBANTE = null;
log.ESTADO_ENVIO = "ERROR";
}
2025-07-16 20:39:24 -04:00
var apiResponse = JsonSerializer.Deserialize<ApiResponse<MedicionSubterraneaResponse>>(jsonRecibido, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
2025-07-17 09:22:55 -04:00
if (apiResponse == null || string.IsNullOrEmpty(apiResponse.Status) || apiResponse.Status != "00")
{
log.COMPROBANTE = null;
log.ESTADO_ENVIO = "ERROR";
}
2025-07-16 20:39:24 -04:00
2025-07-17 09:22:55 -04:00
log.COMPROBANTE = apiResponse.Data.NumeroComprobante ?? null;
log.ESTADO_ENVIO = "EXITO";
2025-07-16 20:39:24 -04:00
}
2025-07-17 09:22:55 -04:00
catch (Exception)
2025-07-01 09:59:59 -04:00
{
2025-07-17 09:22:55 -04:00
log.COMPROBANTE = null;
log.ESTADO_ENVIO = "ERROR";
2025-07-16 20:39:24 -04:00
}
finally
{
await _logMedicionScadaRepository.InsertarLogEnvioAsync(log);
}
2025-07-17 09:22:55 -04:00
return log;
2025-06-24 16:02:27 -04:00
}
2025-06-24 14:46:32 -04:00
}
}