Skip to content

Commit

Permalink
hotfix/Erro DownloadContentAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfariakof committed Jun 8, 2024
1 parent 35e8a71 commit 17060bc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions HomeBrokerXUnit/Repository/HomeBrokerRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Repository;
public class HomeBrokerRepositoryTest
{
[Fact]
public void Should_Returns_HistoryData_GetHistoryData()
public async Task Should_Returns_HistoryData_GetHistoryData()
{
// Arrange
var mockHomeBrokerRepository = new HomeBrokerRepository();
var period = new Period(new DateTime(2023, 01,01), new DateTime(2024, 01, 01));

// Act
var result = mockHomeBrokerRepository.GetHistoryData(period).Result;
var result = await mockHomeBrokerRepository.GetHistoryData(period);

// Assert
Assert.NotNull(result);
Expand Down
48 changes: 40 additions & 8 deletions Repository/HomeBrokerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ public HomeBrokerRepository() { }
public async Task<List<MagazineLuizaHistoryPrice>> GetHistoryData(Period period)
{
var downloadLink = $"https://query1.finance.yahoo.com/v7/finance/download/MGLU3.SA?period1={ToUnixTimestamp(period.StartDate)}&period2={ToUnixTimestamp(period.EndDate)}&interval=1d&filter=history&frequency=1d";
string downloadContent = await DownloadContentAsync(downloadLink);
return ProcessCsvData(downloadContent);
try
{
string downloadContent = await DownloadContentAsync(downloadLink);
return ProcessCsvData(downloadContent);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

/// <summary>
Expand All @@ -40,17 +47,42 @@ private static long ToUnixTimestamp(DateTime date)
}

/// <summary>
/// Baixa o conteúdo de uma URL assíncronamente usando o cliente HttpClient.
/// Baixa o conteúdo de uma URL assincronamente usando o cliente HttpClient.
/// </summary>
/// <param name="url">A URL do qual o conteúdo será baixado.</param>
/// <returns>Uma tarefa representando a operação assíncrona, contendo o conteúdo baixado da URL.</returns>
/// <returns>Uma tarefa representando a operação assíncrona, contendo o conteúdo baixado da URL.</returns>
private static async Task<string> DownloadContentAsync(string url)
{
using (var httpClient = new HttpClient())
using (var httpClientHandler = new HttpClientHandler())
{
// Baixa o conteúdo da URL fornecida
string content = await httpClient.GetStringAsync(url);
return content;
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
using (var httpClient = new HttpClient(httpClientHandler))
{
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));
httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");
httpClient.DefaultRequestHeaders.Connection.TryParseAdd("keep-alive");
httpClient.DefaultRequestHeaders.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue() { NoCache = true };
httpClient.DefaultRequestHeaders.Pragma.TryParseAdd("no-cache");
httpClient.DefaultRequestHeaders.Referrer = new Uri("https://query1.finance.yahoo.com/v7/finance/download/MGLU3.SA");

int retries = 3;
int delay = 1000;
while (retries > 0)
{
try
{
string content = await httpClient.GetStringAsync(url);
return content;
}
catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests && retries > 0)
{
await Task.Delay(delay);
delay *= 2;
retries--;
}
}
throw new Exception("Failed to download content after multiple retries.");
}
}
}

Expand Down

0 comments on commit 17060bc

Please sign in to comment.