#![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()
}
}