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]
derive-error = "0.0.5"
reqwest = "0.11.*"
chrono = "0.4.*"
json = "0.12.*"
tokio = { version = "1.3.*", features = ["full"] }
reqwest = "0.11.2"
json = "0.12.4"
tokio = { version = "1.4.0", features = ["full"] }
+3 -3
View File
@@ -8,7 +8,7 @@
//!use crate::rapr::{RaprClient,RaSub};
//!#[tokio::main]
//!async fn main() -> Result<(), rapr::Error> {
//! let client = RaprClient::new();
//! let client = RaprClient::default();
//! let mut sub = RaSub::new("pics");
//! client.fetch(10, &mut sub).await?;
//! for post in sub.posts {
@@ -27,7 +27,7 @@ mod tests {
use crate::rapr::{RaSub, RaprClient};
#[tokio::test]
async fn subreddit() -> Result<(), crate::rapr::Error> {
let client = RaprClient::new();
let client = RaprClient::default();
let mut sub = RaSub::new("rust");
client.fetch(10, &mut sub).await?;
for post in sub.posts {
@@ -37,7 +37,7 @@ mod tests {
}
#[tokio::test]
async fn title() {
let client = RaprClient::new();
let client = RaprClient::default();
let mut sub = RaSub::new("rust");
client.fetch(10, &mut sub).await.unwrap();
for post in sub.posts {
+18 -4
View File
@@ -1,7 +1,6 @@
extern crate json;
extern crate reqwest;
extern crate tokio;
// use chrono::{DateTime, Local};
use std::fmt;
/// Error enum
@@ -78,6 +77,7 @@ impl RaPost {
}
/// 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> {
let id = post["name"].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();
if selftext.ok_or(Error::NoneError)?.is_empty() {
// Either unwraps and sets new value or reamins ""
selftext = post["selftext_html"].as_str();
}
let items = RaPostItems::new(upvotes, downvotes, permalink, url);
@@ -128,18 +127,28 @@ pub struct RaprClient {
}
impl Default for RaprClient {
/// Return the default RaprClient
fn default() -> Self {
Self::new()
}
}
impl RaprClient {
/// Make a RaprClient
pub fn new() -> Self {
Self {
oauth: None,
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 {
Self::new()
}
@@ -156,14 +165,18 @@ impl RaprClient {
None => {
self.rwclient
.get(url)
.query(&[("limit", count)])
.query(&[("limit", count), ("raw_json", 1)])
.send()
.await?
}
Some(after) => {
self.rwclient
.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()
.await?
}
@@ -180,6 +193,7 @@ impl RaprClient {
for post in raw_posts {
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"])?);
}
}