fix: Resolve encoding issue for AgroFetcher and authorization for BcrFetcher
This commit is contained in:
@@ -11,19 +11,23 @@ namespace Mercados.Infrastructure.DataFetchers
|
||||
public class BcrDataFetcher : IDataFetcher
|
||||
{
|
||||
#region Clases DTO para la respuesta de la API de BCR
|
||||
private class BcrTokenResponse {
|
||||
private class BcrTokenResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public TokenData? Data { get; set; }
|
||||
}
|
||||
private class TokenData {
|
||||
private class TokenData
|
||||
{
|
||||
[JsonPropertyName("token")]
|
||||
public string? Token { get; set; }
|
||||
}
|
||||
private class BcrPreciosResponse {
|
||||
private class BcrPreciosResponse
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public List<BcrPrecioItem>? Data { get; set; }
|
||||
}
|
||||
private class BcrPrecioItem {
|
||||
private class BcrPrecioItem
|
||||
{
|
||||
[JsonPropertyName("precio_Cotizacion")]
|
||||
public decimal PrecioCotizacion { get; set; }
|
||||
[JsonPropertyName("variacion_Precio_Cotizacion")]
|
||||
@@ -65,19 +69,26 @@ namespace Mercados.Infrastructure.DataFetchers
|
||||
_logger.LogInformation("Iniciando fetch para {SourceName}.", SourceName);
|
||||
try
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var client = _httpClientFactory.CreateClient(); // Creamos el cliente una vez
|
||||
|
||||
var token = await GetAuthTokenAsync(client);
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
return (false, "No se pudo obtener el token de autenticación de BCR.");
|
||||
}
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
// Añadimos el token a las cabeceras por defecto de este cliente para todas las peticiones futuras.
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); // Aseguramos que no haya doble "Bearer"
|
||||
|
||||
var cotizaciones = new List<CotizacionGrano>();
|
||||
var fechaDesde = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
|
||||
var fechaHasta = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
|
||||
foreach (var grain in _grainIds)
|
||||
{
|
||||
var response = await client.GetFromJsonAsync<BcrPreciosResponse>(
|
||||
$"{BaseUrl}/PreciosCamara?idGrano={grain.Value}&fechaConcertacionDesde={DateTime.Now.AddDays(-3):yyyy-MM-dd}&fechaConcertacionHasta={DateTime.Now:yyyy-MM-dd}");
|
||||
var requestUrl = $"{BaseUrl}/PreciosCamara?idGrano={grain.Value}&fechaConcertacionDesde={fechaDesde}&fechaConcertacionHasta={fechaHasta}";
|
||||
|
||||
// Usamos el mismo cliente que ya tiene el token configurado.
|
||||
var response = await client.GetFromJsonAsync<BcrPreciosResponse>(requestUrl);
|
||||
|
||||
var latestRecord = response?.Data?.OrderByDescending(r => r.FechaOperacionPizarra).FirstOrDefault();
|
||||
if (latestRecord != null)
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.6" />
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.6" />
|
||||
<PackageReference Include="ThreeFourteen.Finnhub.Client" Version="1.2.0" />
|
||||
<PackageReference Include="YahooFinanceApi" Version="2.3.3" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using System.Text;
|
||||
using Mercados.Infrastructure;
|
||||
using Mercados.Infrastructure.DataFetchers;
|
||||
using Mercados.Infrastructure.Persistence;
|
||||
using Mercados.Infrastructure.Persistence.Repositories;
|
||||
using Mercados.Worker;
|
||||
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
// --- Configuración del Host ---
|
||||
// Esto prepara el host del servicio, permitiendo la inyección de dependencias,
|
||||
// la configuración desde appsettings.json y el logging.
|
||||
|
||||
Reference in New Issue
Block a user