示例#1
0
        public static async Task <IEnumerable <int> > GetBestStories()
        {
            var items = new List <int>();

            try
            {
                var dataApi = new HackerNewsAPI.HackerNewsDataAPI(new Uri(HackerNewsDataAPI));
                var item    = await dataApi.GetBestStoriesAsync();

                items.AddRange(item.Where(e => e.GetValueOrDefault(0) > 0).Cast <int>());
            }
            catch (Exception ex)
            {
                Log.Error($"Error attempting to read the Best Story ID List", ex);
            }

            return(items);
        }
示例#2
0
        /// <summary>
        /// This pulls the data for the passed ID.  It intentionally ignores if there was an error attempting to pull from or write to cache
        /// Also if there was a problem attempting to pull the item from the actual source, this error is ignored as well as TBD the reason, so a stub
        /// object will be created in its place
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task <BestStoryInfo> GetBestStoryInfo(int id)
        {
            try
            {
                var result = GetFromCache(id);
                if (result != null)
                {
                    return(result);
                }

                var dataApi = new HackerNewsAPI.HackerNewsDataAPI(new Uri(HackerNewsDataAPI));

                var item = await dataApi.GetItemAsync(id);

                var bestStoryInfo = new BestStoryInfo
                {
                    Id    = item.ID.GetValueOrDefault(0),
                    By    = item.By,
                    Title = item.Title
                };

                SaveToCache(bestStoryInfo);

                return(bestStoryInfo);
            }
            catch (Exception ex)
            {
                Log.Error($"Error attempting to read information from Hacker News for the item {id}", ex);

                var bestStoryInfo = new BestStoryInfo
                {
                    Id    = id,
                    By    = "<unknown>",
                    Title = "<Problem encountered attempting to read informtion from Hacker News>"
                };

                return(bestStoryInfo);
            }
        }