GDoe5
GDoe5

Reputation: 1

How to do API call for each value on list, then record results against each value?

I've managed to set up a successful API call to reddit's API. I'm looking to extract the subreddit rules for a list of subreddits. I have loaded the list of subreddits from a .csv. I'm struggling to understand what I need to do to queue up a number of requests and record the output.

library("httr2")
library("jsonlite")
library("tidyr")
library("curl")
library("dplyr")
library("rlang")
library("tidyverse")
library("data.table")
library("purrr")
# I can't remember which of these I've actually used
# and which I haven't :)

user_secret_access <- "[id:secret]"
base_url_reddit <- "https://reddit.com"
endpoint_rules <- "about/rules.json"
# there are other endpoints, hence splitting it out
# instead of having it as part of subredditlist$urls

subredditlist <- read_csv("subredditlist.csv")
subredditlist$urls <- sprintf("/r/%s/", subredditlist$subreddit)

req_rules <- request(base_url_reddit) %>%
  req_url_path_append(paste0("/r/northernlion/",endpoint_rules)) %>%
  req_body_json(
    list(`grant_type` = "password",
          username = "[username]",
          password = "[password]",
          user = user_secret_access)) %>%
  req_user_agent("GDoe5 rplaceCrossStitch") %>%
  req_throttle(rate = 25 / 60)

results_rules <- req_rules %>%
  req_perform() %>%
  resp_body_string()

output <- fromJSON(results_rules)
output <- map_if(output$rules, is.data.frame, list) %>%
  as_tibble()

Essentially, I want to replace where it says "/r/northerlion/" with a request for each value on subredditlist$urls. I'm looking at using req_perform_sequential() instead of req_perform(), and having req_rules transform into reqs_rules and be a list of requests instead. This is the part where I'm stuck. I keep looking at the last example in the documentation and not understanding. https://httr2.r-lib.org/reference/req_perform_sequential.html.

I believe it's asking me to write a function() but I just don't get it.

reqs_rules <- lapply(subredditlist$urls, req_rules %>%
  req_url_path_append(paste0([?],endpoint_rules)))

I would I believe need to use resps_data() https://httr2.r-lib.org/reference/resps_successes.html to combine the data at the end, but I don't understand how to get these to combine and go with the associated line of subredditlist either (or how to only pull certain columns from the API response).

Upvotes: 0

Views: 50

Answers (0)

Related Questions