コード例 #1
0
        private async Task <string> GetContent_WithoutCaching(string url, string path, RegionModel regionModel, bool followRedirects, string requestBaseUrl, IHeaderDictionary headers)
        {
            const int MaxRedirections = 10;

            _ = regionModel ?? throw new ArgumentNullException(nameof(regionModel));

            string results = null;

            try
            {
                if (regionModel.IsHealthy)
                {
                    logger.LogInformation($"{nameof(GetContent)}: Getting child response from: {url}");

                    var response = await GetContentIfRedirectedAsync(requestBaseUrl, url, followRedirects, MaxRedirections, regionModel, headers);

                    if (response != null && !response.IsSuccessStatusCode)
                    {
                        throw new EnhancedHttpException(response.StatusCode, response.ReasonPhrase, url);
                    }

                    responseHandler.Process(response);

                    if (response != null)
                    {
                        results = await response.Content.ReadAsStringAsync();
                    }

                    logger.LogInformation($"{nameof(GetContent)}: Received child response from: {url}");
                }
                else
                {
                    results = !string.IsNullOrWhiteSpace(regionModel.OfflineHtml) ? regionModel.OfflineHtml : markupMessages.GetRegionOfflineHtml(regionModel.PageRegion);
                }
            }
            catch (BrokenCircuitException ex)
            {
                logger.LogError(ex, $"{nameof(ContentRetriever)}: BrokenCircuit: {url} - {ex.Message}");

                if (regionModel.HealthCheckRequired)
                {
                    await appRegistryDataService.SetRegionHealthState(path, regionModel.PageRegion, false);
                }

                results = !string.IsNullOrWhiteSpace(regionModel.OfflineHtml) ? regionModel.OfflineHtml : markupMessages.GetRegionOfflineHtml(regionModel.PageRegion);
            }

            return(results);
        }
        public void MarkupMessagesReturnsRegionOfflineMessage(PageRegion pageRegion, string expected)
        {
            // arrange
            var model = new MarkupMessages();

            // act
            var result = model.GetRegionOfflineHtml(pageRegion);

            // assert
            Assert.Equal(expected, result);
        }
コード例 #3
0
        private Task <string> GetMarkup(List <Task <string> > tasks, string path, PageRegion regionType, IEnumerable <RegionModel> regions, string article, string queryString, IHeaderDictionary headers)
        {
            var pageRegionModel = regions.FirstOrDefault(x => x.PageRegion == regionType);

            if (pageRegionModel == null || string.IsNullOrWhiteSpace(pageRegionModel.RegionEndpoint))
            {
                return(Task.FromResult <string>(null));
            }

            if (!pageRegionModel.IsHealthy)
            {
                return(Task.FromResult(!string.IsNullOrWhiteSpace(pageRegionModel.OfflineHtml) ? pageRegionModel.OfflineHtml : markupMessages.GetRegionOfflineHtml(pageRegionModel.PageRegion)));
            }

            var url = FormatArticleUrl(pageRegionModel.RegionEndpoint, article, queryString);

            var task = contentRetriever.GetContent(url, path, pageRegionModel, true, RequestBaseUrl, headers);

            tasks.Add(task);

            return(task);
        }