Fix Service
This commit is contained in:
@@ -94,20 +94,12 @@ public class ElectoralApiService : IElectoralApiService
|
|||||||
|
|
||||||
public async Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string? municipioId, int categoriaId)
|
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");
|
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||||
|
|
||||||
// Construimos la URL base
|
|
||||||
var requestUri = $"/api/resultados/getResultados?distritoId={distritoId}&seccionId={seccionId}&categoriaId={categoriaId}";
|
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))
|
if (!string.IsNullOrEmpty(municipioId))
|
||||||
{
|
{
|
||||||
requestUri += $"&municipioId={municipioId}";
|
requestUri += $"&municipioId={municipioId}";
|
||||||
@@ -115,11 +107,41 @@ public class ElectoralApiService : IElectoralApiService
|
|||||||
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var response = await client.SendAsync(request);
|
var response = await client.SendAsync(request);
|
||||||
return response.IsSuccessStatusCode ? await response.Content.ReadFromJsonAsync<ResultadosDto>() : null;
|
|
||||||
/* }
|
// --- APLICAMOS LA MISMA LÓGICA DEFENSIVA ---
|
||||||
// Si no se pudo obtener un permiso (ej. la cola está llena), devolvemos null.
|
if (response.IsSuccessStatusCode)
|
||||||
return null;*/
|
{
|
||||||
|
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)
|
public async Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string? seccionProvincialId, int categoriaId)
|
||||||
|
|||||||
Reference in New Issue
Block a user