use std::collections::HashMap;
use failure::Error;
use hyper::{Body, Request};
use json::Value;
use url::Url;
use data::{Comment, Comments, Listing, Post, Thing};
use net::{body_from_map, uri_params_from_map};
use {App, Sort};
impl App {
pub fn load_post(&self, fullname: &str) -> Result<Post, Error> {
let mut params: HashMap<&str, &str> = HashMap::new();
params.insert("names", fullname);
let req = Request::get(format!("https://www.reddit.com/by_id/{}/.json", fullname)).body(Body::empty()).unwrap();
let response = self.conn.run_request(req)?;
Post::from_value(&response, self)
}
pub fn get_posts(&self, sub: &str, sort: Sort) -> Result<Value, Error> {
let req = Request::get(
Url::parse_with_params(
&format!(
"https://www.reddit.com/r/{}/.\
json",
sub
),
sort.param(),
)?
.into_string(),
)
.body(Body::empty())
.unwrap();
self.conn.run_request(req)
}
pub fn create_comment_stream(&self, sub: &str) -> Comments {
Comments::new(self, sub)
}
pub fn get_recent_comments(&self, sub: &str, limit: Option<i32>, before: Option<&str>) -> Result<Listing<Comment>, Error> {
let limit_str;
let mut params: HashMap<&str, &str> = HashMap::new();
if let Some(limit) = limit {
limit_str = limit.to_string();
params.insert("limit", &limit_str);
}
if let Some(ref before) = before {
params.insert("before", before);
}
let req = Request::get(uri_params_from_map(&format!("https://www.reddit.com/r/{}/comments.json", sub), ¶ms)?).body(Body::empty()).unwrap();
let resp = self.conn.run_request(req)?;
let comments = Listing::from_value(&resp["data"]["children"], "", self)?;
Ok(comments)
}
pub fn get_comment_tree(&self, post: &str) -> Result<Listing<Comment>, Error> {
let mut params: HashMap<&str, &str> = HashMap::new();
params.insert("limit", "2147483648");
params.insert("depth", "2147483648");
let req = Request::get(format!("https://www.reddit.com/comments/{}/.json", post)).body(body_from_map(¶ms)).unwrap();
let data = self.conn.run_request(req)?;
let data = data[1]["data"]["children"].clone();
Listing::from_value(&data, post, self)
}
}