Skip to content

Commit

Permalink
use existing session when fetching subreddit json
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdallas committed Jun 6, 2023
1 parent 8ab9b10 commit 1bf708e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ async fn main() -> Result<(), GertError> {
posts.push(post);
} else {
for subreddit in &subreddits {
let subposts = Subreddit::new(subreddit).get_posts(feed, limit, period).await?;
let subposts = Subreddit::new(subreddit, &session).get_posts(feed, limit, period).await?;
posts.extend(
subposts
.into_iter()
Expand Down
16 changes: 9 additions & 7 deletions src/subreddit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ use log::debug;
use reqwest::Client;
use std::fmt::Write;

pub struct Subreddit {
pub struct Subreddit<'a> {
/// Name of subreddit.
pub name: String,
url: String,
client: Client,
client: &'a Client,
}

impl Subreddit {
impl Subreddit<'_> {
/// Create a new `Subreddit` instance.
pub fn new(name: &str) -> Subreddit {
pub fn new<'a>(name: &'a str, session: &'a Client) -> Subreddit<'a> {
let subreddit_url = format!("https://www.reddit.com/r/{}", name);

Subreddit { name: name.to_owned(), url: subreddit_url, client: Client::new() }
Subreddit { name: name.to_owned(), url: subreddit_url, client:session }
}

async fn get_feed(
Expand All @@ -35,8 +35,10 @@ impl Subreddit {
if let Some(a) = after {
let _ = write!(url, "&after={}", a);
}

Ok(self.client.get(&url.to_owned()).send().await?.json::<Listing>().await?)
let url = &url.to_owned();
debug!("Fetching posts from {}]", url);
Ok(self.client.get(url).send().await.expect("Bad response").json::<Listing>().await.expect("Failed to parse JSON"))
// Ok(self.client.get(url).send().await.expect("Bad response").json::<Listing>().await.expect("Failed to parse JSON"))
}

pub async fn get_posts(
Expand Down

0 comments on commit 1bf708e

Please sign in to comment.