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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![deny(missing_docs)]
use std::time::Duration;
use reqwest::{self, Client};
pub mod types;
static API_BASE_URL: &str = "https://hacker-news.firebaseio.com/v0";
pub struct HnClient {
client: Client,
}
impl HnClient {
pub fn init() -> reqwest::Result<Self> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.build()?;
Ok(Self { client })
}
pub fn get_item(&self, id: u32) -> reqwest::Result<Option<types::Item>> {
self.client.get(&format!("{}/item/{}.json", API_BASE_URL, id)).send()?.json()
}
pub fn get_user(&self, username: &str) -> reqwest::Result<Option<types::User>> {
self.client.get(&format!("{}/user/{}.json", API_BASE_URL, username)).send()?.json()
}
pub fn get_max_item_id(&self) -> reqwest::Result<u32> {
self.client.get(&format!("{}/maxitem.json", API_BASE_URL)).send()?.json()
}
pub fn get_top_stories(&self) -> reqwest::Result<Vec<u32>> {
self.client.get(&format!("{}/topstories.json", API_BASE_URL)).send()?.json()
}
pub fn get_new_stories(&self) -> reqwest::Result<Vec<u32>> {
self.client.get(&format!("{}/newstories.json", API_BASE_URL)).send()?.json()
}
pub fn get_best_stories(&self) -> reqwest::Result<Vec<u32>> {
self.client.get(&format!("{}/beststories.json", API_BASE_URL)).send()?.json()
}
pub fn get_ask_stories(&self) -> reqwest::Result<Vec<u32>> {
self.client.get(&format!("{}/askstories.json", API_BASE_URL)).send()?.json()
}
pub fn get_show_stories(&self) -> reqwest::Result<Vec<u32>> {
self.client.get(&format!("{}/showstories.json", API_BASE_URL)).send()?.json()
}
pub fn get_job_stories(&self) -> reqwest::Result<Vec<u32>> {
self.client.get(&format!("{}/jobstories.json", API_BASE_URL)).send()?.json()
}
pub fn get_updates(&self) -> reqwest::Result<types::Updates> {
self.client.get(&format!("{}/updates.json", API_BASE_URL)).send()?.json()
}
}