Beispiel #1
0
        public async Task <FetchArticlesResponse> FetchArticlesBySearchQuery(string query, int pageNumber = 1, int pageSize = Constants.DefaultArticlesPageSize)
        {
            var result = new FetchArticlesResponse(pageNumber, pageSize);

            if (string.IsNullOrWhiteSpace(query))
            {
                return(result);
            }

            var request = new ArticlesRequest
            {
                Query    = query,
                Country  = Countries.US,
                Language = Languages.EN,
                Page     = pageNumber,
                PageSize = pageSize,
            };

            var actionUrl = ToActionRelativeUrl("top-headlines", request);
            var articles  = await GetAsync <ArticlesResult>(actionUrl);

            if (articles?.Articles != null)
            {
                result.Articles   = articles.Articles;
                result.TotalCount = articles.TotalResults;
            }

            return(result);
        }
Beispiel #2
0
        public async Task <FetchArticlesResponse> FetchArticlesBySource(string source, int pageNumber = 1, int pageSize = Constants.DefaultArticlesPageSize)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                throw new ArgumentNullException(nameof(source));
            }

            var request = new ArticlesRequest
            {
                Sources = new List <string> {
                    source
                },
                Language = Languages.EN,
                Page     = pageNumber,
                PageSize = pageSize,
            };

            var actionUrl = ToActionRelativeUrl("top-headlines", request);
            var articles  = await GetAsync <ArticlesResult>(actionUrl);

            var result = new FetchArticlesResponse(pageNumber, pageSize);

            if (articles?.Articles != null)
            {
                result.Articles   = articles.Articles;
                result.TotalCount = articles.TotalResults;
            }

            return(result);
        }
        public async Task <FetchArticlesResponse> FetchArticlesBySource(string source, int pageNumber = 1, int pageSize = Constants.DefaultArticlesPageSize)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                throw new ArgumentNullException(nameof(source));
            }

            System.Diagnostics.Debug.WriteLine($"FakeNewsDataService.FetchArticlesBySource: source {source}, page {pageNumber}, pageSize {pageSize}");

            // Simulate network request
            await Task.Delay(1500);

            var result                   = new FetchArticlesResponse(pageNumber, pageSize);
            var resourceName             = $"{GetType().Namespace}.source.{source}.json";
            var prestoredResponseContent = await resourceName.ReadResourceContent();

            if (!string.IsNullOrWhiteSpace(prestoredResponseContent))
            {
                var articles = JsonConvert.DeserializeObject <ArticlesResult>(prestoredResponseContent);
                if (articles?.Articles != null)
                {
                    result.Articles   = articles.Articles.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                    result.TotalCount = articles.Articles.Count;
                }
            }

            System.Diagnostics.Debug.WriteLine($"FakeNewsDataService.FetchArticlesBySource loaded {result.Articles.Count} (page {result.PageNumber} | size {result.PageSize}) out of total {result.TotalCount} items");

            return(result);
        }
        public async Task <FetchArticlesResponse> FetchArticlesByCategory(Categories?category = null, int pageNumber = 1, int pageSize = Constants.DefaultArticlesPageSize)
        {
            System.Diagnostics.Debug.WriteLine($"FakeNewsDataService.FetchArticlesByCategory: categrory {category}, page {pageNumber}, pageSize {pageSize}");

            // Simulate network request
            await Task.Delay(1500);

            var result                   = new FetchArticlesResponse(pageNumber, pageSize);
            var resourceName             = $"{GetType().Namespace}.category.{category?.ToString().ToLower() ?? "all"}.json";
            var prestoredResponseContent = await resourceName.ReadResourceContent();

            if (!string.IsNullOrWhiteSpace(prestoredResponseContent))
            {
                var articles = JsonConvert.DeserializeObject <ArticlesResult>(prestoredResponseContent);
                if (articles?.Articles != null)
                {
                    result.Articles   = articles.Articles.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                    result.TotalCount = articles.Articles.Count;
                }
            }

            System.Diagnostics.Debug.WriteLine($"FakeNewsDataService.FetchArticlesByCategory loaded {result.Articles.Count} (page {result.PageNumber} | size {result.PageSize}) out of total {result.TotalCount} items");

            return(result);
        }
Beispiel #5
0
        public async Task <FetchArticlesResponse> FetchArticlesByCategory(Categories?category = null, int pageNumber = 1, int pageSize = Constants.DefaultArticlesPageSize)
        {
            var request = new ArticlesRequest
            {
                Country  = Countries.US,
                Language = Languages.EN,
                Category = category,
                Page     = pageNumber,
                PageSize = pageSize,
            };

            var actionUrl = ToActionRelativeUrl("top-headlines", request);
            var articles  = await GetAsync <ArticlesResult>(actionUrl);

            var result = new FetchArticlesResponse(pageNumber, pageSize);

            if (articles?.Articles != null)
            {
                result.Articles   = articles.Articles;
                result.TotalCount = articles.TotalResults;
            }

            return(result);
        }