From 17060bc5f0996fcc3f1ec8d038d90f1065f47e60 Mon Sep 17 00:00:00 2001 From: Alex Ribeiro de Faria <42475620+alexfariakof@users.noreply.github.com> Date: Sat, 8 Jun 2024 17:06:08 -0300 Subject: [PATCH] hotfix/Erro DownloadContentAsync --- .../Repository/HomeBrokerRepositoryTest.cs | 4 +- Repository/HomeBrokerRepository.cs | 48 +++++++++++++++---- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/HomeBrokerXUnit/Repository/HomeBrokerRepositoryTest.cs b/HomeBrokerXUnit/Repository/HomeBrokerRepositoryTest.cs index 043b928..75b57fd 100644 --- a/HomeBrokerXUnit/Repository/HomeBrokerRepositoryTest.cs +++ b/HomeBrokerXUnit/Repository/HomeBrokerRepositoryTest.cs @@ -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); diff --git a/Repository/HomeBrokerRepository.cs b/Repository/HomeBrokerRepository.cs index e88acd6..050e78d 100644 --- a/Repository/HomeBrokerRepository.cs +++ b/Repository/HomeBrokerRepository.cs @@ -25,8 +25,15 @@ public HomeBrokerRepository() { } public async Task> 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); + } } /// @@ -40,17 +47,42 @@ private static long ToUnixTimestamp(DateTime date) } /// - /// 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. /// /// A URL do qual o conteúdo será baixado. - /// Uma tarefa representando a operação assíncrona, contendo o conteúdo baixado da URL. + /// Uma tarefa representando a operação assíncrona, contendo o conteúdo baixado da URL. private static async Task 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."); + } } }