Example #1
0
        /// <summary>
        /// Get's the details for the passed in best story IDs
        /// </summary>
        /// <param name="storyIDs"></param>
        /// <returns></returns>
        public static async Task <List <BestStoriesModel> > GetBestStories(List <int> storyIDs, IOptions <AppSettingsModel> appSettings)
        {
            // No IDs, no results
            if (storyIDs.Count == 0)
            {
                return(new List <BestStoriesModel>());
            }

            // Setup our cache, API URLs and variables
            ObjectCache cache      = MemoryCache.Default;
            var         bestStores = cache["bestStories"] as List <BestStoriesModel>;
            var         apiURL     = appSettings.Value.HNStoryDetailsURL;


            // Check the cache if we already have the stories stored
            if (bestStores == null)
            {
                // Setup the expiration for the cache
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(CACHETIMEOUT);

                // Setup our best stories list object
                bestStores = new List <BestStoriesModel>();

                // Loop through our story IDs and generate a list of best stories
                using (HttpClient httpClient = new HttpClient())
                {
                    foreach (var storyID in storyIDs)
                    {
                        // Await the response from the cient API
                        HttpResponseMessage response = await httpClient.GetAsync(apiURL + storyID + ".json");

                        if (response.IsSuccessStatusCode)
                        {
                            var respData = await response.Content.ReadAsStringAsync();

                            var story   = JsonConvert.DeserializeObject <HNStoryModel>(respData);
                            var hnstory = new BestStoriesModel();
                            hnstory.postedBy     = story.by;
                            hnstory.title        = story.title;
                            hnstory.time         = UnixTimeStampToDateTime(story.time);
                            hnstory.uri          = story.url;
                            hnstory.score        = story.score;
                            hnstory.commentCount = story.descendants;
                            bestStores.Add(hnstory);
                        }
                    }
                }
                // And now cache the data
                cache.Set("bestStories", bestStores, policy);
            }
            return(bestStores);
        }
Example #2
0
        public async Task <List <BestStoriesModel> > Get()
        {
            // Setup variables
            string     respString;
            List <int> stories;
            List <BestStoriesModel> bestStories;

            // Setup cache expiration
            var cacheOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(cacheExpirationMin));

            // Now check if we have cached best stories, else pull them from the HackerNewsAPI
            if (!_cache.TryGetValue("bestStories", out bestStories))
            {
                // First get the story IDs
                using (HttpClient httpClient = new HttpClient())
                {
                    // Await the response from the cient API
                    HttpResponseMessage response = await httpClient.GetAsync(HNBestStoriesURL);

                    if (response.IsSuccessStatusCode)
                    {
                        respString = await response.Content.ReadAsStringAsync();

                        stories = JsonConvert.DeserializeObject <List <int> >(respString);
                    }
                    else
                    {
                        // Return an empty value
                        return(new List <BestStoriesModel>());
                    }
                }

                // If we successfully obtained story IDs, we get their author and title, else return empty set
                if (stories.Count > 0)
                {
                    // Setup our best stories list object
                    bestStories = new List <BestStoriesModel>();

                    // Loop through our story IDs and generate a list of best stories
                    using (HttpClient httpClient = new HttpClient())
                    {
                        foreach (var storyID in stories)
                        {
                            // Await the response from the cient API
                            HttpResponseMessage response = await httpClient.GetAsync(HNStoryDetailsURL + storyID + ".json");

                            if (response.IsSuccessStatusCode)
                            {
                                var respData = await response.Content.ReadAsStringAsync();

                                var story   = JsonConvert.DeserializeObject <HNStoryModel>(respData);
                                var hnstory = new BestStoriesModel();
                                hnstory.author = story.by;
                                hnstory.title  = story.title;
                                bestStories.Add(hnstory);
                            }
                            else
                            {
                                // Return an empty value
                                return(new List <BestStoriesModel>());
                            }
                        }
                    }
                    // Now cache the stories
                    _cache.Set("bestStories", bestStories, cacheOptions);
                }
                else
                {
                    // Return an empty value
                    return(new List <BestStoriesModel>());
                }
            }
            return(bestStories);
        }