feat: se agregan logs, y se hacen cambios en el modulo registrar mediciones
This commit is contained in:
parent
5bd9c2a1a6
commit
4b6204d9e7
32 changed files with 648 additions and 625 deletions
|
@ -3,106 +3,107 @@ using Newtonsoft.Json;
|
|||
|
||||
namespace BLL.Integracion_DGA
|
||||
{
|
||||
public class ApiService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public ApiService(HttpClient httpClient)
|
||||
public class ApiService
|
||||
{
|
||||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
|
||||
}
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public async Task<string> GetApiResponseAsync(string apiUrl, Dictionary<string, string> headers = null)
|
||||
{
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
// Configura la URL base
|
||||
httpClient.BaseAddress = new Uri(apiUrl);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
|
||||
|
||||
// Configura los encabezados personalizados si se proporcionan
|
||||
if (headers != null)
|
||||
public ApiService(HttpClient httpClient)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
{
|
||||
request.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
|
||||
}
|
||||
|
||||
// Realiza la solicitud con la solicitud personalizada
|
||||
try
|
||||
public async Task<string> GetApiResponseAsync(string apiUrl, Dictionary<string, string> headers = null)
|
||||
{
|
||||
var response = await httpClient.SendAsync(request);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Maneja errores aquí según sea necesario.
|
||||
Console.WriteLine($"Error en la solicitud: {response.StatusCode}");
|
||||
throw new HttpRequestException($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
}
|
||||
}catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error: " + ex.Message);
|
||||
throw new Exception($"Error en la solicitud HTTP: {ex.Message}");
|
||||
// Maneja la excepción según sea necesario
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> PostApiResponseAsync(string apiUrl, Dictionary<string, string> headers = null, object data = null)
|
||||
{
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(apiUrl);
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
|
||||
|
||||
// Serializa el objeto data a JSON
|
||||
//string jsonData = JsonSerializer.Serialize(data);
|
||||
string jsonData = JsonConvert.SerializeObject(data);
|
||||
// Crea el contenido de la solicitud HTTP con el cuerpo JSON
|
||||
HttpContent contenido = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
||||
request.Content = contenido;
|
||||
|
||||
// Configura los encabezados personalizados si se proporcionan
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
request.Headers.Add(header.Key, header.Value);
|
||||
// Configura la URL base
|
||||
httpClient.BaseAddress = new Uri(apiUrl);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
|
||||
|
||||
// Configura los encabezados personalizados si se proporcionan
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
{
|
||||
request.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// Realiza la solicitud con la solicitud personalizada
|
||||
try
|
||||
{
|
||||
var response = await httpClient.SendAsync(request);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Maneja errores aquí según sea necesario.
|
||||
Console.WriteLine($"Error en la solicitud: {response.StatusCode}");
|
||||
throw new HttpRequestException($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error: " + ex.Message);
|
||||
throw new Exception($"Error en la solicitud HTTP: {ex.Message}");
|
||||
// Maneja la excepción según sea necesario
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Realiza la solicitud POST
|
||||
HttpResponseMessage response = await httpClient.SendAsync(request);
|
||||
|
||||
// Verifica si la solicitud fue exitosa
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// Lee la respuesta como una cadena
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// La solicitud no fue exitosa, maneja el error según sea necesario
|
||||
Console.WriteLine($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
throw new HttpRequestException($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
public async Task<string> PostApiResponseAsync(string apiUrl, Dictionary<string, string> headers = null, object data = null)
|
||||
{
|
||||
// Maneja excepciones si ocurren
|
||||
Console.WriteLine($"Error en la solicitud HTTP: {ex.Message}");
|
||||
throw new Exception($"Error en la solicitud HTTP: {ex.Message}");
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(apiUrl);
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
|
||||
|
||||
// Serializa el objeto data a JSON
|
||||
//string jsonData = JsonSerializer.Serialize(data);
|
||||
string jsonData = JsonConvert.SerializeObject(data);
|
||||
// Crea el contenido de la solicitud HTTP con el cuerpo JSON
|
||||
HttpContent contenido = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
||||
request.Content = contenido;
|
||||
|
||||
// Configura los encabezados personalizados si se proporcionan
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
{
|
||||
request.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// Realiza la solicitud POST
|
||||
HttpResponseMessage response = await httpClient.SendAsync(request);
|
||||
|
||||
// Verifica si la solicitud fue exitosa
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// Lee la respuesta como una cadena
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// La solicitud no fue exitosa, maneja el error según sea necesario
|
||||
Console.WriteLine($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
throw new HttpRequestException($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Maneja excepciones si ocurren
|
||||
Console.WriteLine($"Error en la solicitud HTTP: {ex.Message}");
|
||||
throw new Exception($"Error en la solicitud HTTP: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
using DAL;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using DAL;
|
||||
using Shared.DTO.Envios_DGA;
|
||||
using Shared.DTO.VariablesEntorno;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DAS
|
||||
{
|
||||
public class RegistrarMedicion
|
||||
public class RegistrarMedicion
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly LogMedicionScadaRepository _logMedicionScadaRepository;
|
||||
|
@ -20,10 +19,6 @@ namespace DAS
|
|||
|
||||
public async Task<bool> EnviarMedicionAsync(string codigoObra, MedicionSubterraneaRequest request, long idMedicion)
|
||||
{
|
||||
request.Autenticacion.Password = CredencialDGA.Password;
|
||||
request.Autenticacion.RutEmpresa = CredencialDGA.RutEmpresa; //TODO: condicionar rut empresa
|
||||
request.Autenticacion.RutUsuario = CredencialDGA.RutUsuario;
|
||||
|
||||
var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss-0000");
|
||||
|
||||
var json = JsonSerializer.Serialize(request);
|
||||
|
@ -32,7 +27,7 @@ namespace DAS
|
|||
content.Headers.Add("codigoObra", codigoObra);
|
||||
content.Headers.Add("timeStampOrigen", timeStamp);
|
||||
|
||||
var response = await _httpClient.PostAsync($"{SubterraneaApiUrl.BaseUrl}{SubterraneaApiUrl.EndPoint}", content);
|
||||
/*var response = await _httpClient.PostAsync($"{SubterraneaApiUrl.BaseUrl}{SubterraneaApiUrl.EndPoint}", content);
|
||||
string jsonRecibido = await response.Content.ReadAsStringAsync();
|
||||
string estado = response.IsSuccessStatusCode ? "OK" : "ERROR";
|
||||
string comprobante = string.Empty;
|
||||
|
@ -62,10 +57,10 @@ namespace DAS
|
|||
FechaEnvio = DateTime.UtcNow,
|
||||
IdMedicionSmartscadaOperacion = idMedicion
|
||||
};
|
||||
*/
|
||||
//await _logMedicionScadaRepository.InsertarLogMedicionScadaAsync(logMedicionScada);
|
||||
|
||||
await _logMedicionScadaRepository.InsertarLogMedicionScadaAsync(logMedicionScada);
|
||||
|
||||
return response.IsSuccessStatusCode;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue