public async Task <HackerNewsStory> GetStoryAync(int storyId) { return(await _memoryCache.GetOrCreateAsync <HackerNewsStory>(storyId, async cacheEntry => { cacheEntry.SetSlidingExpiration(TimeSpan.FromSeconds(30)); HackerNewsStory story = new HackerNewsStory(); var response = await _hackerNewsRepo.GetStoryByIdAsync(storyId); story = response.Content.ReadFromJsonAsync <HackerNewsStory>().Result; return story; })); }
public StoryViewModel(HackerNewsStory story, string?image, string?description) { Id = story.Id; Time = story.Time; Title = story.Title; Description = description; Image = image; var res = Uri.TryCreate(story.Url, UriKind.Absolute, out var uri); if (res) { Url = uri; } }
private async Task <HackerNewsStory> GetStoryAsync(int storyId) { return(await _cache.GetOrCreateAsync <HackerNewsStory>(storyId, async cacheEntry => { HackerNewsStory story = new HackerNewsStory(); var response = await _repo.GetStoryByIdAsync(storyId); if (response.IsSuccessStatusCode) { var storyResponse = response.Content.ReadAsStringAsync().Result; story = JsonConvert.DeserializeObject <HackerNewsStory>(storyResponse); } return story; })); }
public static async Task <StoryViewModel> ToViewModelAsync(this HackerNewsStory hackerNewsStory) { string?description = null; string?image = null; try { var meta = await hackerNewsStory.Url.GetMetaAsync().CancelAfter(2000); description = meta.description; image = await meta.image.FormatImage(hackerNewsStory.Url); } catch { } return(new StoryViewModel(hackerNewsStory, image, description)); }
private async Task <HackerNewsStory> GetStoryAsync(int storyId) { return(await cache.GetOrCreateAsync <HackerNewsStory>(storyId, async cacheEntry => { HackerNewsStory story = new HackerNewsStory(); var response = await client.GetAsync(string.Format(StoryApiTemplate, storyId)); if (response.IsSuccessStatusCode) { var storyResponse = response.Content.ReadAsStringAsync().Result; story = JsonConvert.DeserializeObject <HackerNewsStory>(storyResponse); } else { story.Title = string.Format("***Error (Not a title): (ID {0})", storyId); } return story; })); }