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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::error::Error;
use log;
use serde_json;
use reqwest;
use reqwest::blocking::Client;
use reqwest::blocking::Request;
use reqwest::blocking::Response;
use crate::error::HnError;
use crate::error::HttpError;
use crate::model::Id;
use crate::model::firebase::User;
use crate::model::firebase::Item;
use crate::model::firebase::ItemsAndProfiles;
pub struct JsonClient {
http_client: reqwest::blocking::Client,
}
const BASE_URL: &str = "https://hacker-news.firebaseio.com/v0";
#[allow(clippy::new_without_default)]
impl JsonClient {
pub fn new() -> Self {
Self {
http_client: Client::new(),
}
}
fn send(&self, req: Request) -> Result<Response, Box<dyn Error>> {
let resp = self.http_client.execute(req)?;
let status = resp.status().as_u16();
if status != 200 {
let err = HttpError {
url: resp.url().to_string(),
code: status,
};
log::error!("Recieved non 200 status: {:?}", err);
return Err(Box::new(HnError::HttpError(err)));
}
log::debug!("Recieved 200 status, response = {:?}", resp);
Ok(resp)
}
fn get_url(&self, url: &str) -> Result<Response, Box<dyn Error>> {
let req = self.http_client.get(url);
let resp = self.send(req.build()?)?;
Ok(resp)
}
pub fn item(&self, id: Id) -> Result <Item, Box<dyn Error>> {
let url = format!("{base_url}/item/{id}.json",
base_url=BASE_URL,
id=id
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let item: Item = serde_json::from_str(&text)?;
log::debug!("item = {:?}", item);
Ok(item)
}
pub fn max_item(&self) -> Result <Id, Box<dyn Error>> {
let url = format!("{base_url}/maxitem.json",
base_url=BASE_URL,
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let id: Id = serde_json::from_str(&text)?;
log::debug!("maxitem = {:?}", id);
Ok(id)
}
pub fn user(&self, username: String) -> Result<User, Box<dyn Error>> {
let url = format!("{base_url}/user/{id}.json",
base_url=BASE_URL,
id=username
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let user: User = serde_json::from_str(&text)?;
log::debug!("user = {:?}", user);
Ok(user)
}
pub fn new_stories(&self) -> Result<Vec<Id>, Box<dyn Error>> {
let url = format!("{base_url}/newstories.json",
base_url=BASE_URL,
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let ids: Vec<Id> = serde_json::from_str(&text)?;
log::debug!("ids = {:?}", ids);
Ok(ids)
}
pub fn top_stories(&self) -> Result<Vec<Id>, Box<dyn Error>> {
let url = format!("{base_url}/topstories.json",
base_url=BASE_URL,
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let ids: Vec<Id> = serde_json::from_str(&text)?;
log::debug!("ids = {:?}", ids);
Ok(ids)
}
pub fn updates(&self) -> Result<(Vec<Id>, Vec<String>), Box<dyn Error>> {
let url = format!("{base_url}/updates.json",
base_url=BASE_URL,
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let items_and_profiles: ItemsAndProfiles = serde_json::from_str(&text)?;
let items = items_and_profiles.items;
let profiles = items_and_profiles.profiles;
let updates = (items, profiles);
log::debug!("updates = {:?}", updates);
Ok(updates)
}
pub fn ask_stories(&self) -> Result<Vec<Id>, Box<dyn Error>> {
let url = format!("{base_url}/askstories.json",
base_url=BASE_URL,
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let ids: Vec<Id> = serde_json::from_str(&text)?;
log::debug!("ids = {:?}", ids);
Ok(ids)
}
pub fn show_stories(&self) -> Result<Vec<Id>, Box<dyn Error>> {
let url = format!("{base_url}/showstories.json",
base_url=BASE_URL,
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let ids: Vec<Id> = serde_json::from_str(&text)?;
log::debug!("ids = {:?}", ids);
Ok(ids)
}
pub fn job_stories(&self) -> Result<Vec<Id>, Box<dyn Error>> {
let url = format!("{base_url}/jobstories.json",
base_url=BASE_URL,
);
let resp = self.get_url(&url)?;
let text = resp.text()?;
log::debug!("text = {:?}", text);
let ids: Vec<Id> = serde_json::from_str(&text)?;
log::debug!("ids = {:?}", ids);
Ok(ids)
}
}
#[cfg(test)]
mod tests {
use super::JsonClient;
use std::error::Error;
use crate::util::setup;
#[test]
fn test_get_invalid_url() -> Result<(), Box<dyn Error>> {
let client = JsonClient::new();
let url = format!("https://www.google.com/invalid_path");
let _resp = client.get_url(&url)?;
Ok(())
}
#[test]
fn test_item() -> Result<(), Box<dyn Error>> {
setup();
let id_story = 27476206;
let id_comment = 27509155;
let client = JsonClient::new();
let story = client.item(id_story)?;
log::debug!("item = {:?}", story);
assert!(story.is_story());
let comment = client.item(id_comment)?;
log::debug!("item = {:?}", comment);
assert!(comment.is_comment());
Ok(())
}
#[test]
fn test_max_item() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let item = client.max_item()?;
log::debug!("maxitem = {:?}", item);
Ok(())
}
#[test]
fn test_user() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let user = client.user("pg".to_string())?;
log::debug!("user = {:?}", user);
Ok(())
}
#[test]
fn test_new_stories() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let ids = client.new_stories()?;
log::debug!("ids = {:?}", ids);
Ok(())
}
#[test]
fn test_top_stories() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let ids = client.top_stories()?;
log::debug!("ids = {:?}", ids);
Ok(())
}
#[test]
fn test_updates() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let updates = client.updates()?;
log::debug!("updates = {:?}", updates);
Ok(())
}
#[test]
fn test_ask_stories() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let ids = client.ask_stories()?;
log::debug!("ids = {:?}", ids);
Ok(())
}
#[test]
fn test_show_stories() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let ids = client.show_stories()?;
log::debug!("ids = {:?}", ids);
Ok(())
}
#[test]
fn test_job_stories() -> Result<(), Box<dyn Error>> {
setup();
let client = JsonClient::new();
let ids = client.job_stories()?;
log::debug!("ids = {:?}", ids);
Ok(())
}
}