Fix Service
This commit is contained in:
@@ -18,7 +18,7 @@ public class ElectoralApiService : IElectoralApiService
|
||||
private readonly RateLimiter _rateLimiter;
|
||||
|
||||
public ElectoralApiService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IConfiguration configuration,
|
||||
ILogger<ElectoralApiService> logger,
|
||||
RateLimiter rateLimiter)
|
||||
@@ -45,7 +45,7 @@ public class ElectoralApiService : IElectoralApiService
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
|
||||
|
||||
return await response.Content.ReadFromJsonAsync<TokenResponse>();
|
||||
}
|
||||
return null;
|
||||
@@ -94,20 +94,12 @@ public class ElectoralApiService : IElectoralApiService
|
||||
|
||||
public async Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string? municipioId, int categoriaId)
|
||||
{
|
||||
// "Pedir una ficha". Este método ahora devuelve un "lease" (permiso).
|
||||
// Si no hay fichas, esperará aquí automáticamente hasta que se rellene el cubo.
|
||||
/*
|
||||
using RateLimitLease lease = await _rateLimiter.AcquireAsync(1);
|
||||
using RateLimitLease lease = await _rateLimiter.AcquireAsync(1);
|
||||
if (!lease.IsAcquired) return null;
|
||||
|
||||
// Si se nos concede el permiso para proceder...
|
||||
if (lease.IsAcquired)
|
||||
{*/
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
|
||||
// Construimos la URL base
|
||||
var requestUri = $"/api/resultados/getResultados?distritoId={distritoId}&seccionId={seccionId}&categoriaId={categoriaId}";
|
||||
|
||||
// Añadimos el municipioId a la URL SÓLO si no es nulo o vacío
|
||||
if (!string.IsNullOrEmpty(municipioId))
|
||||
{
|
||||
requestUri += $"&municipioId={municipioId}";
|
||||
@@ -115,11 +107,41 @@ public class ElectoralApiService : IElectoralApiService
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode ? await response.Content.ReadFromJsonAsync<ResultadosDto>() : null;
|
||||
/* }
|
||||
// Si no se pudo obtener un permiso (ej. la cola está llena), devolvemos null.
|
||||
return null;*/
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.SendAsync(request);
|
||||
|
||||
// --- APLICAMOS LA MISMA LÓGICA DEFENSIVA ---
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Leemos el contenido como un string primero para poder loguearlo si falla.
|
||||
var contentString = await response.Content.ReadAsStringAsync();
|
||||
if (string.IsNullOrEmpty(contentString))
|
||||
{
|
||||
_logger.LogWarning("La API devolvió 200 OK pero con cuerpo vacío para getResultados. URI: {uri}", requestUri);
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonSerializer.Deserialize<ResultadosDto>(contentString);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "La API devolvió una respuesta no-JSON para getResultados. URI: {uri}", requestUri);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogWarning("La API devolvió un código de error {statusCode} para getResultados. URI: {uri}", response.StatusCode, requestUri);
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "La petición HTTP a getResultados falló. URI: {uri}", requestUri);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string? seccionProvincialId, int categoriaId)
|
||||
@@ -136,7 +158,7 @@ public class ElectoralApiService : IElectoralApiService
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.SendAsync(request);
|
||||
@@ -156,7 +178,7 @@ public class ElectoralApiService : IElectoralApiService
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_logger.LogWarning("La API devolvió un código de error {statusCode} para getBancas. URI: {uri}", response.StatusCode, requestUri);
|
||||
return null;
|
||||
}
|
||||
@@ -205,7 +227,7 @@ public class ElectoralApiService : IElectoralApiService
|
||||
var requestUri = $"/api/resultados/getFile?mesaId={mesaId}";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.SendAsync(request);
|
||||
|
||||
Reference in New Issue
Block a user