Fix 1813
This commit is contained in:
@@ -13,71 +13,83 @@ namespace Elecciones.Infrastructure.Services;
|
||||
// Implementación de emergencia que usa el comando 'curl' del sistema operativo
|
||||
public class CurlElectoralApiService : IElectoralApiService
|
||||
{
|
||||
private readonly ILogger<CurlElectoralApiService> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly string _baseUrl;
|
||||
private readonly ILogger<CurlElectoralApiService> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly string _baseUrl;
|
||||
|
||||
public CurlElectoralApiService(ILogger<CurlElectoralApiService> logger, IConfiguration configuration)
|
||||
public CurlElectoralApiService(ILogger<CurlElectoralApiService> logger, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_baseUrl = _configuration["ElectoralApi__BaseUrl"] ?? "";
|
||||
}
|
||||
|
||||
private async Task<(int, string)> ExecuteCurlCommand(string arguments)
|
||||
{
|
||||
var process = new Process
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_baseUrl = _configuration["ElectoralApi:BaseUrl"] ?? "";
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "curl",
|
||||
Arguments = arguments,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
string output = await process.StandardOutput.ReadToEndAsync();
|
||||
string error = await process.StandardError.ReadToEndAsync();
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
if (process.ExitCode != 0)
|
||||
{
|
||||
_logger.LogError("Error al ejecutar curl. Exit Code: {ExitCode}. Error: {Error}", process.ExitCode, error);
|
||||
return (-1, error);
|
||||
}
|
||||
|
||||
private async Task<(int, string)> ExecuteCurlCommand(string arguments)
|
||||
// Extraemos el código de estado HTTP del final del stderr si usamos -w
|
||||
// (Simplificación: asumimos 200 si el ExitCode es 0)
|
||||
return (200, output);
|
||||
}
|
||||
|
||||
public async Task<string?> GetAuthTokenAsync()
|
||||
{
|
||||
var username = _configuration["ElectoralApi__Username"];
|
||||
var password = _configuration["ElectoralApi__Password"]; // OJO: Verifica el nombre exacto de la variable en tu .env
|
||||
var arguments = $"-s -H \"username: {username}\" -H \"password: {password}\" \"{_baseUrl}/api/createtoken\"";
|
||||
|
||||
var (status, output) = await ExecuteCurlCommand(arguments);
|
||||
if (status != 200)
|
||||
{
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "curl",
|
||||
Arguments = arguments,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
string output = await process.StandardOutput.ReadToEndAsync();
|
||||
string error = await process.StandardError.ReadToEndAsync();
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
if (process.ExitCode != 0)
|
||||
{
|
||||
_logger.LogError("Error al ejecutar curl. Exit Code: {ExitCode}. Error: {Error}", process.ExitCode, error);
|
||||
return (-1, error);
|
||||
}
|
||||
|
||||
// Extraemos el código de estado HTTP del final del stderr si usamos -w
|
||||
// (Simplificación: asumimos 200 si el ExitCode es 0)
|
||||
return (200, output);
|
||||
_logger.LogError("Curl recibió un código de estado no exitoso. Respuesta: {Output}", output);
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<string?> GetAuthTokenAsync()
|
||||
try
|
||||
{
|
||||
var username = _configuration["ElectoralApi:Username"];
|
||||
var password = _configuration["ElectoralApi:Password"];
|
||||
var arguments = $"-s -H \"username: {username}\" -H \"password: {password}\" \"{_baseUrl}/api/createtoken\"";
|
||||
|
||||
var (status, output) = await ExecuteCurlCommand(arguments);
|
||||
if (status != 200) return null;
|
||||
|
||||
var tokenResponse = JsonSerializer.Deserialize<TokenResponse>(output);
|
||||
return tokenResponse?.Data?.AccessToken;
|
||||
var tokenResponse = JsonSerializer.Deserialize<TokenResponse>(output);
|
||||
return tokenResponse?.Data?.AccessToken;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Falló la deserialización del JSON. La respuesta del servidor NO era un JSON válido. Respuesta recibida: {Output}", output);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Implementa los demás métodos de la interfaz usando el mismo patrón
|
||||
// ...
|
||||
// Por ahora, solo necesitamos GetAuthTokenAsync para validar la conexión.
|
||||
public Task<List<CategoriaDto>?> GetCategoriasAsync(string authToken) => Task.FromResult<List<CategoriaDto>?>(null);
|
||||
public Task<List<CatalogoDto>?> GetCatalogoCompletoAsync(string authToken) => Task.FromResult<List<CatalogoDto>?>(null);
|
||||
public Task<List<AgrupacionDto>?> GetAgrupacionesAsync(string authToken, string distritoId, int categoriaId) => Task.FromResult<List<AgrupacionDto>?>(null);
|
||||
public Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string municipioId) => Task.FromResult<ResultadosDto?>(null);
|
||||
public Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string seccionId) => Task.FromResult<RepartoBancasDto?>(null);
|
||||
public Task<List<string[]>?> GetTelegramasTotalizadosAsync(string authToken, string distritoId, string seccionId) => Task.FromResult<List<string[]>?>(null);
|
||||
public Task<TelegramaFileDto?> GetTelegramaFileAsync(string authToken, string mesaId) => Task.FromResult<TelegramaFileDto?>(null);
|
||||
public Task<ResumenDto?> GetResumenAsync(string authToken, string distritoId) => Task.FromResult<ResumenDto?>(null);
|
||||
public Task<EstadoRecuentoGeneralDto?> GetEstadoRecuentoGeneralAsync(string authToken, string distritoId) => Task.FromResult<EstadoRecuentoGeneralDto?>(null);
|
||||
// Implementa los demás métodos de la interfaz usando el mismo patrón
|
||||
// ...
|
||||
// Por ahora, solo necesitamos GetAuthTokenAsync para validar la conexión.
|
||||
public Task<List<CategoriaDto>?> GetCategoriasAsync(string authToken) => Task.FromResult<List<CategoriaDto>?>(null);
|
||||
public Task<List<CatalogoDto>?> GetCatalogoCompletoAsync(string authToken) => Task.FromResult<List<CatalogoDto>?>(null);
|
||||
public Task<List<AgrupacionDto>?> GetAgrupacionesAsync(string authToken, string distritoId, int categoriaId) => Task.FromResult<List<AgrupacionDto>?>(null);
|
||||
public Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string municipioId) => Task.FromResult<ResultadosDto?>(null);
|
||||
public Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string seccionId) => Task.FromResult<RepartoBancasDto?>(null);
|
||||
public Task<List<string[]>?> GetTelegramasTotalizadosAsync(string authToken, string distritoId, string seccionId) => Task.FromResult<List<string[]>?>(null);
|
||||
public Task<TelegramaFileDto?> GetTelegramaFileAsync(string authToken, string mesaId) => Task.FromResult<TelegramaFileDto?>(null);
|
||||
public Task<ResumenDto?> GetResumenAsync(string authToken, string distritoId) => Task.FromResult<ResumenDto?>(null);
|
||||
public Task<EstadoRecuentoGeneralDto?> GetEstadoRecuentoGeneralAsync(string authToken, string distritoId) => Task.FromResult<EstadoRecuentoGeneralDto?>(null);
|
||||
}
|
||||
Reference in New Issue
Block a user