Integracion_DGA/DAS/RegistrarMedicion.cs

62 lines
1.9 KiB
C#
Raw Normal View History

2025-07-16 20:39:24 -04:00
using DAL;
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 timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss-0000", CultureInfo.InvariantCulture);
2025-06-24 16:02:27 -04:00
var json = JsonSerializer.Serialize(request);
2025-07-16 20:39:24 -04:00
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");
2025-06-24 14:46:32 -04:00
2025-07-16 20:39:24 -04:00
var log = new LogMedicionEnvio();
try
{
2025-07-16 20:39:24 -04:00
using var response = await _httpClient.SendAsync(req);
string jsonRecibido = await response.Content.ReadAsStringAsync();
if (response == null || !response.IsSuccessStatusCode)
{
}
2025-07-16 20:39:24 -04:00
var apiResponse = JsonSerializer.Deserialize<ApiResponse<MedicionSubterraneaResponse>>(jsonRecibido, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
catch (Exception ex)
2025-07-01 09:59:59 -04:00
{
2025-07-16 20:39:24 -04:00
}
finally
{
await _logMedicionScadaRepository.InsertarLogEnvioAsync(log);
}
2025-06-24 16:02:27 -04:00
}
2025-06-24 14:46:32 -04:00
}
}