public async static Task <List <NewsInformation> > GetByCategoryAsync(NewsCategoryType newsCategory) { var results = new List <NewsInformation>(); var searchUrl = $"https://api.cognitive.microsoft.com/bing/v7.0/news/?mkt=en-US&Category={newsCategory}"; var client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Common.CoreConstants.NewsSearchApiKey); var uri = new Uri(searchUrl); var result = await client.GetStringAsync(uri); var newsResult = JsonConvert.DeserializeObject <NewsResult>(result); results = (from item in newsResult.value select new NewsInformation() { Title = item.name, Description = item.description, ImageUrl = item.image?.thumbnail?.contentUrl, CreatedDate = item.datePublished }).ToList(); return(results.Where(w => !string.IsNullOrWhiteSpace(w.ImageUrl)).Take(10).ToList()); }
public static async Task <List <NewsInformation> > GetByCategoryAsync(NewsCategoryType category) { List <NewsInformation> results = new List <NewsInformation>(); //string searchUrl = $"https://api.cognitive.microsoft.com/bing/v5.0/news/?Category={category}"; //var client = new HttpClient(); //client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Common.CoreConstants.BingNewsSearchApiKey); //var uri = new Uri(searchUrl); //var result = await client.GetStringAsync(uri); //var newsResult = JsonConvert.DeserializeObject<NewsResult>(result); //results = (from item in newsResult.value // select new NewsInformation() { // Title = item.name, // Description = item.description, // //CreatedDate = item.datePublished, // CreatedDate = item.datePublished.ToString(CultureInfo.InstalledUICulture), // ImageUrl = item.image?.thumbnail?.contentUrl, // }).ToList(); return(results.Where(w => !string.IsNullOrEmpty(w.ImageUrl)).Take(10).ToList()); }
public static async Task <List <NewsInformation> > GetByCategoryAsync(NewsCategoryType category) { try { throw new Exception(); List <NewsInformation> results = new List <NewsInformation>(); string searchUrl = $"https://api.cognitive.microsoft.com/bing/v7.0/news/?Category={category}"; var client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Common.CoreConstants.NewsSearchApiKey); var uri = new Uri(searchUrl); var result = await client.GetStringAsync(uri); var newsResult = JsonConvert.DeserializeObject <NewsResult>(result); results = (from item in newsResult.value select new NewsInformation() { Title = item.name, Description = item.description, CreatedDate = item.datePublished, ImageUrl = item.image?.thumbnail?.contentUrl, }).ToList(); return(results.Where(w => !string.IsNullOrEmpty(w.ImageUrl)).Take(10).ToList()); } catch (Exception) { Task <List <NewsInformation> > task = Task.Run(() => { int descriptionCounter = 0, titleCounter = 0; return(new List <NewsInformation> { new NewsInformation { Title = $"Trump uses 'no collusion' 7 times in a single Russia answer", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" }, new NewsInformation { Title = $"{nameof(GetByCategoryAsync)} {titleCounter++}", CreatedDate = DateTime.Now, Description = $"{nameof(NewsInformation)} {descriptionCounter++}", ImageUrl = "/icon.png" } }); }); if (task.Wait(TimeSpan.FromSeconds(10))) { return(task.Result); } else { return(new List <NewsInformation>()); } } }