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