61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using DAL;
|
|
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(DatoDGA medicion, MedicionSubterraneaRequest request, DateTime fechaEnvio)
|
|
{
|
|
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");
|
|
|
|
var log = new LogMedicionEnvio();
|
|
|
|
try
|
|
{
|
|
using var response = await _httpClient.SendAsync(req);
|
|
string jsonRecibido = await response.Content.ReadAsStringAsync();
|
|
|
|
if (response == null || !response.IsSuccessStatusCode)
|
|
{
|
|
|
|
}
|
|
|
|
var apiResponse = JsonSerializer.Deserialize<ApiResponse<MedicionSubterraneaResponse>>(jsonRecibido, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
|
|
}
|
|
finally
|
|
{
|
|
await _logMedicionScadaRepository.InsertarLogEnvioAsync(log);
|
|
}
|
|
}
|
|
}
|
|
}
|