Skip to content

Commit b2e915f

Browse files
authored
Merge pull request #37 from nationalparkservice/alexa-dev
get_park_units function
2 parents 3165357 + 70bf260 commit b2e915f

8 files changed

Lines changed: 185 additions & 20 deletions

File tree

‎DESCRIPTION‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ Imports:
2626
stringr,
2727
tibble
2828
Depends:
29-
R (>= 3.5)
29+
R (>= 4.1.0)
3030
LazyData: true
3131
VignetteBuilder: knitr
3232
URL: https://nationalparkservice.github.io/NPSdatastore/, https://github.com/nationalparkservice/NPSdatastore
3333
BugReports: https://github.com/nationalparkservice/NPSdatastore/issues
34+
Remotes:
35+
nationalparkservice/QCkit

‎NAMESPACE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export(get_external_links)
1717
export(get_file_info)
1818
export(get_keywords)
1919
export(get_lifecycle_info)
20+
export(get_park_units)
2021
export(get_reference_owners)
2122
export(get_reference_types)
2223
export(search_references_by_id)

‎R/get.R‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,59 @@ get_bibliography <- function(reference_id, nps_internal = FALSE, dev = FALSE) {
356356
return(bib)
357357
}
358358

359+
360+
#' Retrieve the park units from a DataStore reference
361+
#'
362+
#'@inheritParams upload_file_to_reference
363+
#'@inheritParams search_references_by_id
364+
#'
365+
#' @returns A tibble of park codes and park names.
366+
#' @export
367+
#'
368+
#' @examples
369+
#' park_units <- get_park_units(reference_id = 2305911)
370+
#'
371+
get_park_units <- function(reference_id, nps_internal = FALSE, dev = FALSE) {
372+
373+
.validate_ref_id(reference_id)
374+
375+
park_units <- .datastore_request(is_secure = nps_internal, is_dev = dev) |>
376+
httr2::req_url_path_append("Reference", reference_id, "Units") |>
377+
httr2::req_perform()
378+
379+
.validate_resp(park_units)
380+
381+
park_units <- httr2::resp_body_json(park_units)
382+
383+
if(length(park_units) == 0) {
384+
cli::cli_inform("There are no park units associated with this reference.")
385+
} else {
386+
park_units <- paste(sapply(park_units, `[[`, "unitCode"), collapse = ";")
387+
388+
unit_names <- httr2::request("https://irmaservices.nps.gov/Unit/v2/api/") |>
389+
httr2::req_url_path_append(park_units)|>
390+
httr2::req_headers(format = "json") |>
391+
httr2::req_headers(Accept = "application/json") |>
392+
httr2::req_perform()
393+
394+
.validate_resp(unit_names)
395+
396+
unit_names <- httr2::resp_body_json(unit_names)
397+
398+
unit_names <- tibble::as_tibble(
399+
do.call(rbind,
400+
lapply(unit_names, function(x) {
401+
data.frame(
402+
UnitCode = as.character(x[["UnitCode"]])[1],
403+
UnitName = as.character(x[["FullName"]])[1],
404+
stringsAsFactors = FALSE)
405+
}))
406+
)
407+
}
408+
409+
return(unit_names)
410+
}
411+
359412
#' Determine if a reference was created by/for the NPS
360413
#'
361414
#' @inheritParams get_bibliography
@@ -404,4 +457,4 @@ get_lifecycle_info <- function(reference_id, dev = FALSE) {
404457
lifecycle_info <- httr2::resp_body_json(lifecycle_info)
405458

406459
return(lifecycle_info)
407-
}
460+
}

‎R/helpers.R‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,29 +343,48 @@ example_ref_ids <- function(visibility = c("public", "internal", "both"), n, see
343343
}
344344
}
345345

346+
#' Check HTTP response and throw a helpful error if needed
347+
#'
348+
#' @param resp HTTP response as returned by `httr2::req_perform()`
349+
#' @param nice_msg_400 Optional. Custom message for errors in the 400 range
350+
#' @param nice_msg_500 Optional. Custom message for errors in the 500 range
351+
#' @param details Optional. Name of JSON field in response body that contains more info about the error
352+
#' @param call Caller environment
353+
#'
354+
#' @keywords internal
355+
#'
346356
.validate_resp <- function(resp,
347357
nice_msg_400,
348358
nice_msg_500,
359+
details,
349360
call = rlang::caller_env()) {
350361

351362
if (httr2::resp_is_error(resp)) {
352-
if (missing(nice_msg_400)) {
353-
nice_msg_400 <- c("i" = "There's a problem with your API request. Check reference IDs, search terms, etc. for typos.",
354-
"i" = "If you are an NPS user attempting to edit a reference or access non-public data, verify that you are on the NPS network, have the appropriate permissions, and have set {.arg nps_internal = TRUE} if applicable.",
355-
"i" = "Verify that {.arg dev} is set correctly.")
356-
}
357-
if (missing(nice_msg_500)) {
358-
nice_msg_500 <- c("i" = "Something seems to have gone wrong on DataStore's end. For troubleshooting help, take a screenshot of this error and contact the package maintainer or the DataStore helpdesk.")
359-
}
360363

361364
status_num <- httr2::resp_status(resp)
362365

363-
if (floor(status_num/100) == 5) {
366+
if (floor(status_num/100) == 5) { # error in 500 range (likely server-side)
367+
if (missing(nice_msg_500)) {
368+
nice_msg_500 <- "Something seems to have gone wrong on DataStore's end. For troubleshooting help, take a screenshot of this error and contact the package maintainer or the DataStore helpdesk."
369+
}
364370
nice_msg <- nice_msg_500
365-
} else if (floor(status_num/100) == 4) {
371+
} else if (floor(status_num/100) == 4) { # error in 400 range (likely client-side)
372+
if (missing(nice_msg_400)) {
373+
nice_msg_400 <- c("There's a problem with your API request. Check reference IDs, search terms, etc. for typos.",
374+
"If you are an NPS user attempting to edit a reference or access non-public data, verify that you are on the NPS network, have the appropriate permissions, and have set {.arg nps_internal = TRUE} if applicable.",
375+
"Verify that {.arg dev} is set correctly.")
376+
}
366377
nice_msg <- nice_msg_400
367378
}
368379

380+
names(nice_msg) <- rep("i", length(nice_msg))
381+
382+
if(!missing(details)) {
383+
resp_body <- httr2::resp_body_json(resp)
384+
detail_msg <- glue::glue("DETAILS: {resp_body[[details]]}")
385+
nice_msg <- c(nice_msg, "i" = detail_msg)
386+
}
387+
369388
http_err <- glue::glue("HTTP {status_num}: {httr2::resp_status_desc(resp)}")
370389
cli::cli_abort(c(http_err, nice_msg), call = call)
371390
}

‎R/set.R‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ add_external_link <- function(reference_id, url, description, last_verified = fo
451451
}
452452

453453
links <- list(resourceId = 0,
454-
userSort = 0,
455-
description = description,
456-
uri = url,
457-
lastVerified = last_verified)
454+
userSort = 0,
455+
description = description,
456+
uri = url,
457+
lastVerified = last_verified)
458458

459459
added_link <- .datastore_request(is_secure = TRUE, is_dev = dev) |>
460460
httr2::req_url_path_append("Reference", reference_id, "ExternalLinks") |>
@@ -539,8 +539,8 @@ set_by_for_nps <- function(reference_id, by_for_nps, dev = TRUE, interactive = T
539539
# Set values
540540
nps_internal <- TRUE
541541
by_for_nps <- dplyr::case_when(by_for_nps ~ 'true',
542-
!by_for_nps ~ 'false',
543-
.default = NA_character_) # convert by_for_nps to a string for the API
542+
!by_for_nps ~ 'false',
543+
.default = NA_character_) # convert by_for_nps to a string for the API
544544

545545
# Verify that we're modifying the right reference
546546
if (interactive) {
@@ -665,14 +665,22 @@ set_license <- function(reference_id, license_type_id, dev = TRUE, interactive =
665665
#'
666666
set_lifecycle_active <- function(reference_id, dev = TRUE, interactive = TRUE) {
667667
.validate_ref_id(reference_id)
668+
# Verify that we're modifying the right reference
669+
if (interactive) {
670+
.user_validate_ref_title(ref_id = reference_id,
671+
is_secure = TRUE,
672+
is_dev = dev)
673+
}
668674

669675
lifecycle_info <- .datastore_request(is_secure = TRUE, is_dev = dev) |>
670676
httr2::req_url_path_append("Reference", reference_id, "Lifecycle", "Active") |>
671677
httr2::req_body_json(list()) |>
672678
httr2::req_method("PUT") |>
673679
httr2::req_perform()
674680

675-
.validate_resp(lifecycle_info)
681+
.validate_resp(lifecycle_info,
682+
nice_msg_500 = "DataStore could not change the reference lifecycle. See below for details.",
683+
details = "exceptionMessage")
676684

677685
lifecycle_info <- get_lifecycle_info(reference_id = reference_id, dev = dev)
678686

@@ -694,14 +702,23 @@ set_lifecycle_active <- function(reference_id, dev = TRUE, interactive = TRUE) {
694702
#'
695703
set_lifecycle_draft <- function(reference_id, dev = TRUE, interactive = TRUE) {
696704
.validate_ref_id(reference_id)
705+
# Verify that we're modifying the right reference
706+
if (interactive) {
707+
.user_validate_ref_title(ref_id = reference_id,
708+
is_secure = TRUE,
709+
is_dev = dev)
710+
}
711+
697712

698713
lifecycle_info <- .datastore_request(is_secure = TRUE, is_dev = dev) |>
699714
httr2::req_url_path_append("Reference", reference_id, "Lifecycle", "Draft") |>
700715
httr2::req_body_json(list()) |>
701716
httr2::req_method("PUT") |>
702717
httr2::req_perform()
703718

704-
.validate_resp(lifecycle_info)
719+
.validate_resp(lifecycle_info,
720+
nice_msg_500 = "DataStore could not change the reference lifecycle. See below for details.",
721+
details = "exceptionMessage")
705722

706723
lifecycle_info <- get_lifecycle_info(reference_id = reference_id, dev = dev)
707724

‎man/dot-validate_resp.Rd‎

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/get_park_units.Rd‎

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-helpers.R‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,22 @@ test_that("example_ref_ids returns example reference IDs", {
3434
expect_warning(example_ref_ids(n = 500), "exceeds total number")
3535

3636
})
37+
38+
test_that(".get_ref_profile_url constructs the correct URL", {
39+
40+
public <- .get_ref_profile_url(public_refs[37], is_dev = FALSE)
41+
public_dev <- .get_ref_profile_url(public_refs[5], is_dev = TRUE)
42+
secure <- .get_ref_profile_url(internal_refs[22], is_dev = FALSE)
43+
secure_dev <- .get_ref_profile_url(internal_refs[9], is_dev = TRUE)
44+
45+
46+
expect_type(public_dev, "character")
47+
expect_type(public, "character")
48+
expect_type(secure_dev, "character")
49+
expect_type(secure, "character")
50+
51+
expect_equal(substr(public_dev, 1, 52), "https://irmadev.nps.gov/DataStore/Reference/Profile/")
52+
expect_equal(substr(public, 1, 49), "https://irma.nps.gov/DataStore/Reference/Profile/")
53+
expect_equal(substr(secure_dev, 1, 52), "https://irmadev.nps.gov/DataStore/Reference/Profile/")
54+
expect_equal(substr(secure, 1, 49), "https://irma.nps.gov/DataStore/Reference/Profile/")
55+
})

0 commit comments

Comments
 (0)