-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoryService.cs
42 lines (33 loc) · 1.11 KB
/
StoryService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace hn_top
{
class StoryService
{
private const String BaseAPI = "https://hacker-news.firebaseio.com/v0";
private readonly JsonSerializerOptions options;
private readonly HttpClient client;
public StoryService()
{
options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
client = new HttpClient();
}
public async Task<int[]> GetTopStories()
{
var response = await client.GetAsync($"{BaseAPI}/topstories.json");
string stories = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<int[]>(stories);
}
public async Task<Story> GetStory(int id)
{
var response = await client.GetAsync($"{BaseAPI}/item/{id}.json");
string story = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Story>(story, options);
}
}
}