Bumped tokio and other crates version

Also added raw_json = 1 in reqwest parameters
This commit is contained in:
Uttarayan Mondal
2021-03-22 08:36:15 +05:30
parent 2adc76cf41
commit adf5fc178d
3 changed files with 25 additions and 12 deletions
+3 -4
View File
@@ -9,8 +9,7 @@ edition = "2018"
[dependencies] [dependencies]
derive-error = "0.0.5" derive-error = "0.0.5"
reqwest = "0.11.*" reqwest = "0.11.2"
chrono = "0.4.*" json = "0.12.4"
json = "0.12.*" tokio = { version = "1.4.0", features = ["full"] }
tokio = { version = "1.3.*", features = ["full"] }
+3 -3
View File
@@ -8,7 +8,7 @@
//!use crate::rapr::{RaprClient,RaSub}; //!use crate::rapr::{RaprClient,RaSub};
//!#[tokio::main] //!#[tokio::main]
//!async fn main() -> Result<(), rapr::Error> { //!async fn main() -> Result<(), rapr::Error> {
//! let client = RaprClient::new(); //! let client = RaprClient::default();
//! let mut sub = RaSub::new("pics"); //! let mut sub = RaSub::new("pics");
//! client.fetch(10, &mut sub).await?; //! client.fetch(10, &mut sub).await?;
//! for post in sub.posts { //! for post in sub.posts {
@@ -27,7 +27,7 @@ mod tests {
use crate::rapr::{RaSub, RaprClient}; use crate::rapr::{RaSub, RaprClient};
#[tokio::test] #[tokio::test]
async fn subreddit() -> Result<(), crate::rapr::Error> { async fn subreddit() -> Result<(), crate::rapr::Error> {
let client = RaprClient::new(); let client = RaprClient::default();
let mut sub = RaSub::new("rust"); let mut sub = RaSub::new("rust");
client.fetch(10, &mut sub).await?; client.fetch(10, &mut sub).await?;
for post in sub.posts { for post in sub.posts {
@@ -37,7 +37,7 @@ mod tests {
} }
#[tokio::test] #[tokio::test]
async fn title() { async fn title() {
let client = RaprClient::new(); let client = RaprClient::default();
let mut sub = RaSub::new("rust"); let mut sub = RaSub::new("rust");
client.fetch(10, &mut sub).await.unwrap(); client.fetch(10, &mut sub).await.unwrap();
for post in sub.posts { for post in sub.posts {
+18 -4
View File
@@ -1,7 +1,6 @@
extern crate json; extern crate json;
extern crate reqwest; extern crate reqwest;
extern crate tokio; extern crate tokio;
// use chrono::{DateTime, Local};
use std::fmt; use std::fmt;
/// Error enum /// Error enum
@@ -78,6 +77,7 @@ impl RaPost {
} }
/// Parse json from `json['data']['children']` array elements. /// Parse json from `json['data']['children']` array elements.
/// Note: In case of url mission reddit make url the permalink
pub fn parse(post: &json::JsonValue) -> Result<RaPost, Error> { pub fn parse(post: &json::JsonValue) -> Result<RaPost, Error> {
let id = post["name"].as_str().ok_or(Error::UnexpectedJson)?; let id = post["name"].as_str().ok_or(Error::UnexpectedJson)?;
let title = post["title"].as_str().ok_or(Error::UnexpectedJson)?; let title = post["title"].as_str().ok_or(Error::UnexpectedJson)?;
@@ -88,7 +88,6 @@ impl RaPost {
let mut selftext = post["selftext"].as_str(); let mut selftext = post["selftext"].as_str();
if selftext.ok_or(Error::NoneError)?.is_empty() { if selftext.ok_or(Error::NoneError)?.is_empty() {
// Either unwraps and sets new value or reamins ""
selftext = post["selftext_html"].as_str(); selftext = post["selftext_html"].as_str();
} }
let items = RaPostItems::new(upvotes, downvotes, permalink, url); let items = RaPostItems::new(upvotes, downvotes, permalink, url);
@@ -128,18 +127,28 @@ pub struct RaprClient {
} }
impl Default for RaprClient { impl Default for RaprClient {
/// Return the default RaprClient
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl RaprClient { impl RaprClient {
/// Make a RaprClient
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
oauth: None, oauth: None,
rwclient: reqwest::Client::new(), rwclient: reqwest::Client::new(),
} }
} }
/// Make a RaprClient with a custom user_agent (since reddit ratelimits same user agent being
/// used a ton of times)
pub fn with_user_agent(user_agent: &str) -> Result<Self, Error> {
Ok(Self {
oauth: None,
rwclient: reqwest::Client::builder().user_agent(user_agent).build()?,
})
}
pub fn default() -> Self { pub fn default() -> Self {
Self::new() Self::new()
} }
@@ -156,14 +165,18 @@ impl RaprClient {
None => { None => {
self.rwclient self.rwclient
.get(url) .get(url)
.query(&[("limit", count)]) .query(&[("limit", count), ("raw_json", 1)])
.send() .send()
.await? .await?
} }
Some(after) => { Some(after) => {
self.rwclient self.rwclient
.get(url) .get(url)
.query(&[("limit", count.to_string()), ("after", after.to_string())]) .query(&[
("limit", count.to_string().as_str()),
("after", after.as_str()),
("raw_json", "1"),
])
.send() .send()
.await? .await?
} }
@@ -180,6 +193,7 @@ impl RaprClient {
for post in raw_posts { for post in raw_posts {
if post["kind"].as_str().ok_or(Error::NoneError)? == "t3" { if post["kind"].as_str().ok_or(Error::NoneError)? == "t3" {
// 't3' is the 'post' type in reddit api
parsed_posts.push(RaPost::parse(&post["data"])?); parsed_posts.push(RaPost::parse(&post["data"])?);
} }
} }