Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTR2 POST requests in get_data functions #37

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions R/lc_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,32 @@ lc_get_data <- function(metric = NULL,
showPctFull = NULL,
countOnly = NULL) {
# Base API URL.
req <- httr2::request("https://java.epa.gov/StreamCAT/LakeCat/metrics?")
query_url <- "https://java.epa.gov/StreamCAT/LakeCat/metrics?"
# Collapse comids into a single string separated by a comma.
if (!is.null(comid))
comid <- paste(comid, collapse = ",")
# Create the query based on user inputs.
# req_url_query silently ignores NULLs.
query <- httr2::req_url_query(
.req = req,
name = metric,
comid = comid,
areaOfInterest = aoi,
showAreaSqKm = showAreaSqKm,
showPctFull = showPctFull,
countOnly = countOnly
)
# Send HTTP request
resp <- httr2::req_perform(req = query)
# Extract the body of the response.
resp_body <- httr2::resp_body_string(resp, encoding = "UTF-8")
# Transform the string response into a data frame.
final_df <- utils::read.csv(text = resp_body,
fileEncoding = "UTF8")
post_body=""
if (!is.null(metric)) post_body <- paste0(post_body,"&name=",metric)
if (!is.null(comid)) post_body <- paste0(post_body,"&comid=",comid)
if (!is.null(aoi)) post_body <- paste0(post_body,"&areaOfInterest=",aoi)
if (!is.null(showAreaSqKm)) post_body <- paste0(post_body,"&showAreaSqKm=",showAreaSqKm)
if (!is.null(showPctFull)) post_body <- paste0(post_body,"&showPctFull=",showPctFull)
if (!is.null(countOnly)) post_body <- paste0(post_body,"&countOnly=",countOnly)
post_body = substring(post_body, 2)

df <- httr2::request(query_url) |>
# insert body to ensure return of hundreds of COMIDs
httr2::req_body_raw(post_body) |>
# perform the request
httr2::req_perform() |>
# extract response body as string
httr2::resp_body_string(encoding = 'UTF-8') |>
# Transform the string response into a data frame.
readr::read_csv()
# End of function. Return a data frame.
return(final_df)
return(df)
}

#' @title Get NLCD Data
Expand Down
44 changes: 23 additions & 21 deletions R/sc_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,36 @@ sc_get_data <- function(metric = NULL,
conus = NULL,
countOnly = NULL) {
# Base API URL.
req <- httr2::request("https://java.epa.gov/StreamCAT/metrics?")
query_url <- "https://java.epa.gov/StreamCAT/metrics?"
# Collapse comids into a single string separated by a comma.
if (!is.null(comid))
comid <- paste(comid, collapse = ",")
# Create the query based on user inputs.
# req_url_query silently ignores NULLs.
query <- httr2::req_url_query(
.req = req,
name = metric,
comid = comid,
areaOfInterest = aoi,
state = state,
county = county,
region = region,
showAreaSqKm = showAreaSqKm,
showPctFull = showPctFull,
conus = conus,
countOnly = countOnly
)
post_body=""
if (!is.null(metric)) post_body <- paste0(post_body,"&name=",metric)
if (!is.null(comid)) post_body <- paste0(post_body,"&comid=",comid)
if (!is.null(aoi)) post_body <- paste0(post_body,"&areaOfInterest=",aoi)
if (!is.null(state)) post_body <- paste0(post_body,"&state=",state)
if (!is.null(county)) post_body <- paste0(post_body,"&county=",county)
if (!is.null(region)) post_body <- paste0(post_body,"&region=",region)
if (!is.null(showAreaSqKm)) post_body <- paste0(post_body,"&showAreaSqKm=",showAreaSqKm)
if (!is.null(showPctFull)) post_body <- paste0(post_body,"&showPctFull=",showPctFull)
if (!is.null(conus)) post_body <- paste0(post_body,"&conus=",conus)
if (!is.null(countOnly)) post_body <- paste0(post_body,"&countOnly=",countOnly)
post_body = substring(post_body, 2)
# Send HTTP request
resp <- httr2::req_perform(req = query)
# Extract the body of the response.
resp_body <- httr2::resp_body_string(resp, encoding = "UTF-8")
# Transform the string response into a data frame.
final_df <- utils::read.csv(text = resp_body,
fileEncoding = "UTF8")
df <- httr2::request(query_url) |>
# insert body to ensure return of hundreds of COMIDs
httr2::req_body_raw(post_body) |>
# perform the request
httr2::req_perform() |>
# extract response body as string
httr2::resp_body_string(encoding = 'UTF-8') |>
# Transform the string response into a data frame.
readr::read_csv()
# End of function. Return a data frame.
return(final_df)
return(df)
}

#' @title Get NLCD Data
Expand Down
Loading