Created
November 19, 2024 18:31
-
-
Save Aariq/c76337800318c9a9bfef11edf2431170 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr2) | |
# Requests are composable. For example, you could set a bunch of options in a base request used by many other functions | |
base_req <- | |
request("https://services.usanpn.org/npn_portal/") |> | |
req_retry(max_tries = 3, retry_on_failure = TRUE) |> #retry on errors | |
req_progress(type = "down") |> #display progress bar for large downloads | |
req_throttle(rate = 30/60) |> #limit request rate to 30 requests per minute | |
req_user_agent("rnpn (https://github.com/usa-npn/rnpn/)") | |
base_req | |
# Then build on the base request by adding specific endpoints and queries | |
req_species_by_id <- | |
base_req |> | |
req_url_path_append("species", "getSpeciesById.json") |> | |
req_url_query(species_id = 3) | |
req_species_by_id | |
# Perform the request | |
resp <- req_perform(req_species_by_id) | |
resp | |
# And get the body of the response as a list | |
result <- | |
resp |> | |
resp_body_json() | |
result | |
as.data.frame(result) | |
# https://gist.github.com/Aariq/c76337800318c9a9bfef11edf2431170 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment