public async Task <SearchDataAndResults> ExecuteSearchAsync(string searchArea, CancellationToken cancellationToken) { try { var searchDataAndResults = new SearchDataAndResults { CookieContainer = new CookieContainer() }; var handler = HttpClientHelpers.CreateHttpClientHandler(_systemConfig, _configuration, searchDataAndResults.CookieContainer); await _logger.LogInformationAsync($"Beginning searches for {searchArea.ToUpper()}...", cancellationToken); using (var client = new HttpClientWrapper(_configuration.BaseUri, handler, _logger, _systemConfig)) { await LogSearchInputsAsync(cancellationToken); var searchPageResponse = await client.GetAsync(_configuration.SearchRoute, new CancellationToken()); var searchDates = await DateChunker.SplitDateRange(_searchConfig.StartDate, _searchConfig.EndDate, _configuration.ChunkSizeDays); searchDataAndResults.SearchResultsPages = new List <HttpResponseMessage>(); client.DefaultRequestHeaders.Add("Referer", $"{_configuration.BaseUri}{_configuration.SearchRoute}"); searchDataAndResults.SearchResultsPages = new List <HttpResponseMessage>(); foreach (KeyValuePair <string, string> range in searchDates) { async Task <HttpRequestMessage> SearchRequestBuilder() => await BuildPostFormUrlEncodedRequestAsync(searchPageResponse, range, cancellationToken); var searchPostResponse = await client.PostAsync(SearchRequestBuilder, new CancellationToken()); await _logger.LogInformationAsync($"Post search response status: {searchPostResponse.StatusCode}", cancellationToken); var searchResults = await client.GetAsync(_configuration.SearchResultsRoute, new CancellationToken()); async Task <HttpRequestMessage> SearchResultsRequestBuilder() => await BuildPostAllFormUrlEncodedRequestAsync(searchResults, cancellationToken); var searchPostResponseAll = await client.PostAsync(SearchResultsRequestBuilder, new CancellationToken()); HttpResponseMessage searchResultsAll; if (searchPostResponseAll.StatusCode == HttpStatusCode.Found) { searchResultsAll = await client.GetAsync(_configuration.SearchResultsRoute, new CancellationToken()); } else { searchResultsAll = searchPostResponseAll; } searchDataAndResults.SearchResultsPages.Add(searchResultsAll); } return(searchDataAndResults); } } catch (Exception ex) { await _logger.LogExceptionAsync($"Search failed!", ex, cancellationToken); throw new SearchFailedException(ex.Message, ex.InnerException); } }
public async Task GivenADateRange_WhenEndDateIsSameAsChunkEndDate_ThenShouldReturnOneChunkUsingEndDate() { var dateRanges = await DateChunker.SplitDateRange("01-03-2018", "30-03-2018", 30); Assert.AreEqual(1, dateRanges.Count); Assert.IsTrue(dateRanges.ContainsKey("01-03-2018")); Assert.AreEqual("30-03-2018", dateRanges["01-03-2018"]); }
public async Task GivenADateRange_WhenEndDateIsGreaterThanChunkEndDate_ThenShouldReturnTwoChunksSecondChunkBeingSmaller() { var dateRanges = await DateChunker.SplitDateRange("01-03-2018", "15-04-2018", 30); Assert.AreEqual(2, dateRanges.Count); Assert.IsTrue(dateRanges.ContainsKey("01-03-2018")); Assert.AreEqual("30-03-2018", dateRanges["01-03-2018"]); Assert.AreEqual("31-03-2018", dateRanges.Keys.Last()); Assert.AreEqual("15-04-2018", dateRanges.Values.Last()); }
public async Task GivenADateRange_WhenEndDateIsGreaterThanChunkEndDate_ThenShouldReturnTwoCFourChunksExactly() { var dateRanges = await DateChunker.SplitDateRange("01-03-2018", "28-06-2018", 30); Assert.AreEqual(4, dateRanges.Count); Assert.IsTrue(dateRanges.ContainsKey("01-03-2018")); Assert.AreEqual("30-03-2018", dateRanges["01-03-2018"]); Assert.IsTrue(dateRanges.ContainsKey("31-03-2018")); Assert.AreEqual("29-04-2018", dateRanges["31-03-2018"]); Assert.IsTrue(dateRanges.ContainsKey("30-04-2018")); Assert.AreEqual("29-05-2018", dateRanges["30-04-2018"]); Assert.AreEqual("30-05-2018", dateRanges.Keys.Last()); Assert.AreEqual("28-06-2018", dateRanges.Values.Last()); }