diff --git a/R/AAA.R b/R/AAA.R index 32340d30..c0333edd 100644 --- a/R/AAA.R +++ b/R/AAA.R @@ -5,6 +5,10 @@ pkg.env <- new.env() pkg.env$nldi_base <- "https://api.water.usgs.gov/nldi/linked-data/" pkg.env$local_sf <- requireNamespace("sf", quietly = TRUE) options("dataRetrieval" = list("api_version" = "v0")) + + query_ret <- get_collection() + services <- sapply(query_ret$tags, function(x) x[["name"]]) + pkg.env$api_endpoints <- services } diff --git a/R/construct_api_requests.R b/R/construct_api_requests.R index 3d2279f2..bf2f25d2 100644 --- a/R/construct_api_requests.R +++ b/R/construct_api_requests.R @@ -243,25 +243,27 @@ switch_properties_id <- function(properties, id_name, service){ } } - schema <- check_OGC_requests(endpoint = service, - type = "schema") - all_properties <- names(schema$properties) - - if(all(all_properties[!all_properties %in% c("id", "geometry")] %in% properties)) { - # Cleans up URL if we're asking for everything - properties <- NA_character_ - } else { - if(all(!is.na(properties))){ + if(!all(is.na(properties))){ + + schema <- check_OGC_requests(endpoint = service, + type = "schema") + all_properties <- names(schema$properties) + + if(all(all_properties[!all_properties %in% c("id", "geometry")] %in% properties)) { + # Cleans up URL if we're asking for everything + properties <- NA_character_ + } else { properties <- gsub("-", "_", properties) properties <- properties[!properties %in% c("id", "geometry", paste0(gsub("-", "_", service), "_id"))] + + } + + if(!all(is.na(properties))){ + match.arg(properties, choices = all_properties, + several.ok = TRUE) } - } - - if(!all(is.na(properties))){ - match.arg(properties, choices = all_properties, - several.ok = TRUE) } return(properties) @@ -408,11 +410,7 @@ check_OGC_requests <- function(endpoint = "daily", match.arg(type, c("queryables", "schema")) - query_ret <- get_collection() - - services <- sapply(query_ret$tags, function(x) x[["name"]]) - - match.arg(endpoint, services) + match.arg(endpoint, pkg.env$api_endpoints) req <- base_url() |> httr2::req_url_path_append("collections") |> diff --git a/R/read_waterdata.R b/R/read_waterdata.R index b19e9a7a..1c6172ca 100644 --- a/R/read_waterdata.R +++ b/R/read_waterdata.R @@ -44,11 +44,7 @@ read_waterdata <- function(service, ..., convertType = TRUE){ - query_req <- get_collection() - - endpoints <- sapply(query_req$tags, function(x) x[["name"]]) - - match.arg(service, endpoints) + match.arg(service, pkg.env$api_endpoints) args <- list(...) args[["service"]] <- service