Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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());

                    client.DefaultRequestHeaders.Add("Referer", $"{_configuration.BaseUri}{_configuration.SearchRoute}");
                    async Task <HttpRequestMessage> SearchRequestBuilder() => await BuildPostFormUrlEncodedRequestAsync(searchPageResponse, cancellationToken);

                    var searchPostResponse = await client.PostAsync(SearchRequestBuilder, new CancellationToken());

                    var redirectUrl   = searchPostResponse.Headers.Location.ToString().Replace($"PS={_configuration.DefaultPageSize}", $"PS={_configuration.DesiredPageSize}");
                    var searchResults = await client.GetAsync(redirectUrl, new CancellationToken());

                    await _logger.LogInformationAsync($"Post search response status: {searchResults.StatusCode}", cancellationToken);

                    searchDataAndResults.SearchResultsPages = new List <HttpResponseMessage> {
                        searchResults
                    };
                    return(searchDataAndResults);
                }
            }
            catch (Exception ex)
            {
                await _logger.LogExceptionAsync($"Search failed!", ex, cancellationToken);

                throw new SearchFailedException(ex.Message, ex.InnerException);
            }
        }