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."); + } } }