74 lines
2.6 KiB
C#
74 lines
2.6 KiB
C#
using DAL;
|
|
using Serilog;
|
|
using Shared.DTO.Envios_DGA;
|
|
using Shared.DTO.VariablesEntorno;
|
|
using System.Globalization;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace DAS
|
|
{
|
|
public class RegistrarMedicion
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly LogEnvioRepository _logMedicionScadaRepository;
|
|
|
|
public RegistrarMedicion(HttpClient httpClient, LogEnvioRepository logMedicionScadaRepository)
|
|
{
|
|
_httpClient = httpClient;
|
|
_logMedicionScadaRepository = logMedicionScadaRepository;
|
|
}
|
|
|
|
public async Task<LogMedicionEnvio> EnviarMedicionAsync(DatoDGATemporal medicion, MedicionSubterraneaRequest request, DateTime fechaEnvio)
|
|
{
|
|
var log = new LogMedicionEnvio();
|
|
try
|
|
{
|
|
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;
|
|
|
|
using var response = await _httpClient.SendAsync(req);
|
|
string jsonRecibido = await response.Content.ReadAsStringAsync();
|
|
log.JSON_RESPUESTA = jsonRecibido;
|
|
|
|
if (response == null || !response.IsSuccessStatusCode)
|
|
{
|
|
log.COMPROBANTE = null;
|
|
log.ESTADO_ENVIO = "ERROR";
|
|
}
|
|
|
|
var apiResponse = JsonSerializer.Deserialize<ApiResponse<MedicionSubterraneaResponse>>(jsonRecibido, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
if (apiResponse == null || string.IsNullOrEmpty(apiResponse.Status) || apiResponse.Status != "00")
|
|
{
|
|
log.COMPROBANTE = null;
|
|
log.ESTADO_ENVIO = "ERROR";
|
|
}
|
|
|
|
log.COMPROBANTE = apiResponse.Data.NumeroComprobante ?? null;
|
|
log.ESTADO_ENVIO = "EXITO";
|
|
}
|
|
catch (Exception)
|
|
{
|
|
log.COMPROBANTE = null;
|
|
log.ESTADO_ENVIO = "ERROR";
|
|
}
|
|
finally
|
|
{
|
|
|
|
}
|
|
|
|
return log;
|
|
}
|
|
}
|
|
}
|