From 8e6fe3faa435e3ee3e5fc14bb51a66c24205c898 Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 10:55:06 -0600 Subject: [PATCH 01/12] initial commit: add ds and ds_secure apis as global variables; helper functions to access them. --- R/utils.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 R/utils.R diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..d2b2741 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,18 @@ +## assign global package variables + +#initiate new environment accessible from within package: +.pkgglobalenv <- new.env(parent=emptyenv()) + +#data_store API base URL: +assign("ds_api", "https://irmadevservices.nps.gov/datastore/v6/rest/", envir=.pkgglobalenv) + +#data_store secure API base URL: +assign("ds_secure_api", "https://irmadevservices.nps.gov/datastore-secure/v6/rest/", envir=.pkgglobalenv) + +.ds_api <- function(x){ + get("ds_api", envir = .pkgglobalenv) +} + +.ds_secure_api <- function(x){ + get("ds_secure_api", envir = .pkgglobalenv) +} \ No newline at end of file From a62a9238db92ae75549d3dcbe1269af6ab47cafc Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 10:56:02 -0600 Subject: [PATCH 02/12] update to global variables for ds apis; update to v6 --- R/get_data_packages.R | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/R/get_data_packages.R b/R/get_data_packages.R index eae9e2f..6ab6761 100644 --- a/R/get_data_packages.R +++ b/R/get_data_packages.R @@ -52,9 +52,7 @@ get_data_packages <- function(reference_id, #check for newer version: if(force == FALSE){ cat("Working on: ", crayon::bold$green(reference_id[i]), ".\n", sep="") - url <- paste0( - "https://irmaservices.nps.gov/datastore-secure/v5/rest/ReferenceCodeSearch?q=", - reference_id[i]) + url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id[i]) #api call to see if ref exists test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm")) status_code <- httr::stop_for_status(test_req)$status_code @@ -89,8 +87,9 @@ get_data_packages <- function(reference_id, #check for a newer version: version <-ref_data$mostRecentVersion if(!is.na(version)){ - newest_url <- paste0( - "https://irmaservices.nps.gov/datastore-secure/v5/rest/ReferenceCodeSearch?q=", version) + newest_url <- paste0(.ds_secure_api(), + "ReferenceCodeSearch?q=", + version) new_req <- httr::GET(newest_url, httr::authenticate(":", ":", "ntlm")) new_status <- httr::stop_for_status(new_req)$status_code if(!new_status == 200){ @@ -142,9 +141,10 @@ get_data_packages <- function(reference_id, } #get HoldingID from the ReferenceID - defaults to the first holding - rest_holding_info_url <- paste0( - "https://irmaservices.nps.gov/datastore-secure/v4/rest/reference/", - reference_id[i], "/DigitalFiles") + rest_holding_info_url <- paste0(.ds_secure_api(), + "reference/", + reference_id[i], + "/DigitalFiles") xml <- suppressMessages(httr::content(httr::GET(rest_holding_info_url, httr::authenticate(":", ":", "ntlm")))) @@ -156,9 +156,9 @@ get_data_packages <- function(reference_id, for(j in seq_along(xml)){ #get file URL tryCatch( - {rest_download_url <- paste0( - "https://irmaservices.nps.gov/datastore-secure", - "/v4/rest/DownloadFile/",xml[[j]]$resourceId)}, + {rest_download_url <- paste0(.ds_secure_api(), + "DownloadFile/", + xml[[j]]$resourceId)}, error = function(e){ cat(crayon::red$bold( "ERROR: You do not have permissions to access ", @@ -234,9 +234,9 @@ get_data_packages <- function(reference_id, #check for newer version: if(force == FALSE){ cat("Working on: ", crayon::bold$green(reference_id[i]), ".\n", sep="") - url <- paste0( - "https://irmaservices.nps.gov/datastore/v5/rest/ReferenceCodeSearch?q=", - reference_id[i]) + url <- paste0(.ds_api(), + "ReferenceCodeSearch?q=", + reference_id[i]) #api call to see if ref exists test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm")) status_code <- httr::stop_for_status(test_req)$status_code @@ -279,9 +279,9 @@ get_data_packages <- function(reference_id, #Look for a newer version: version <-ref_data$mostRecentVersion if(!is.na(version)){ - newest_url <- paste0( - "https://irmaservices.nps.gov/datastore/v5/rest/ReferenceCodeSearch?q=", - version) + newest_url <- paste0(.ds_api(), + "ReferenceCodeSearch?q=", + version) new_req <- httr::GET(newest_url, httr::authenticate(":", ":", "ntlm")) new_status <- httr::stop_for_status(new_req)$status_code if(!new_status == 200){ @@ -333,9 +333,10 @@ get_data_packages <- function(reference_id, dir.create(destination_dir) } # get the HoldingID from the ReferenceID - rest_holding_info_url <- paste0( - "https://irmaservices.nps.gov/datastore/v4/rest/reference/", - reference_id[i], "/DigitalFiles") + rest_holding_info_url <- paste0(.ds_api(), + "reference/", + reference_id[i], + "/DigitalFiles") xml <- httr::content(httr::GET(rest_holding_info_url)) #test whether requires secure=TRUE & VPN; alert user: From bfa767eaf4f4f6251b69f207ed5e63b2c567e5f5 Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 11:03:28 -0600 Subject: [PATCH 03/12] update DataStore api to v6 --- R/getReferenceInfo.R | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/R/getReferenceInfo.R b/R/getReferenceInfo.R index 6f8ecca..4855ec5 100644 --- a/R/getReferenceInfo.R +++ b/R/getReferenceInfo.R @@ -18,11 +18,11 @@ #' get_park_taxon_refs("APIS", 126749) #' } get_park_taxon_refs <- function(park_code, taxon_code) { - url <- paste( - "https://irmaservices.nps.gov/datastore-secure/v5/rest/UnitSpeciesSearch/", - park_code, "/", taxon_code, - sep = "" - ) + url <- paste0(.ds_secure_api(), + "UnitSpeciesSearch/", + park_code, + "/", + taxon_code) DSReference <- httr::content(httr::GET( url, httr::authenticate(":", ":", "ntlm") )) %>% @@ -97,10 +97,9 @@ get_park_taxon_url <- function(park_code, taxon_code) { #' get_ref_info(2266196, "Title") #' } get_ref_info <- function(holding_id, field) { - url <- paste0( - "https://irmaservices.nps.gov/datastore/v4/rest/Profile/", - holding_id - ) + url <- paste0(.ds_api(), + "Profile/", + holding_id) DSReference <- httr::content(httr::GET( url, httr::authenticate(":", ":", "ntlm") )) From ba5db61e82e90c73aa2501ddfb4e011a09d59653 Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 11:16:00 -0600 Subject: [PATCH 04/12] update version; update links to issues etc --- DESCRIPTION | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d9ef764..6e95913 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: NPSutils Type: Package Title: Collection of Functions to read/write information from the NPS Data Store -Version: 0.2.0.3 +Version: 0.2.1 Authors@R: c( person(given = "Robert", family = "Baker", email = "robert_baker@nps.gov", role = c("aut", "cre"), @@ -36,5 +36,5 @@ Suggests: knitr, rmarkdown VignetteBuilder: knitr -URL: https://github.com/RobLBaker/NPSutils -BugReports: https://github.com/RobLBaker/NPSutils/issues +URL: https://github.com/nationalparkservice/NPSutils +BugReports: https://github.com/nationalparkservice/NPSutils/issues From d22f289a1fa08a38ff890efb35cd905834bc2d5f Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 11:16:13 -0600 Subject: [PATCH 05/12] update version to 0.3.0 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6e95913..8761db8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: NPSutils Type: Package Title: Collection of Functions to read/write information from the NPS Data Store -Version: 0.2.1 +Version: 0.3.0 Authors@R: c( person(given = "Robert", family = "Baker", email = "robert_baker@nps.gov", role = c("aut", "cre"), From 9715e60d9478211e756cdb2396281119b6c3f837 Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 11:16:32 -0600 Subject: [PATCH 06/12] Add info about lots of updates including datastore api v6 --- NEWS.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 12a57e5..9bfa8b6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,16 +1,20 @@ -* added new functionality to `get_data_packages()`: it will now check to see if a DataStore reference ID is invalid or not. It will also check whether the reference is a data package or not. Substantial feedback is reported to the user if the flag force is set to FALSE. -* added a new function `rm_local_packages()` which can delete one or more (or all) packages within a folder called "data" (where your packages should be if you downloaded them with `get_data_packages()`). This only deletes local copies and does not delete any data from DataStore. -* changed function name `load_metadata()` is now `load_pkg_metadata()` so as not to conflict with `DPchecker::load_metadata()` -* updated all function names to be snake_case -* updated all file names to match function names -* updated `get_data_packages()` to (when `force = FALSE`) check for newer versions of requested data packages and prompt the user to download the newest version if they so choose. -* updated `get_data_packages()` to: +# NPSutils 0.3.0 + + * updated all datastore api requests from v4/v5 to v6 (units service remains at v2) + * add global variables for base datastore api urls and helper functions to access them in utils.R + * added new functionality to `get_data_packages()`: it will now check to see if a DataStore reference ID is invalid or not. It will also check whether the reference is a data package or not. Substantial feedback is reported to the user if the flag force is set to FALSE. + * added a new function `rm_local_packages()` which can delete one or more (or all) packages within a folder called "data" (where your packages should be if you downloaded them with `get_data_packages()`). This only deletes local copies and does not delete any data from DataStore. + * changed function name `load_metadata()` is now `load_pkg_metadata()` so as not to conflict with `DPchecker::load_metadata()` + * updated all function names to be snake_case + * updated all file names to match function names + * updated `get_data_packages()` to (when `force = FALSE`) check for newer versions of requested data packages and prompt the user to download the newest version if they so choose. + * updated `get_data_packages()` to: * include a force option. Force defaults to false for a verbose & interactive function. Setting force = TRUE removes the interactive portions and eliminates all informative messages except critical errors. * `get_data_packages()` now inspects the location the data packages are being written to. If a folder with the data package reference id already exists, the function will prompt the user asking if they want to re-download and overwrite the existing data package (when force = FALSE) * `get_data_packages()` now returns the full path to the data package folders, including the "/data" directory they are all in. -* update `get_park_taxon_refs()` to hit v5/rest services -* update documentation: make it clear that the taxon_code parameter in `get_park_taxon_refs()` is the IRMA taxon code, not the ITIS TSN. -* update documentation: make it explicit when `get_park_taxon_refs()` and `get_park_taxon_citations()` are hitting secure servers and warn users that the results may also need to be restricted. + * update `get_park_taxon_refs()` to hit v5/rest services + * update documentation: make it clear that the taxon_code parameter in `get_park_taxon_refs()` is the IRMA taxon code, not the ITIS TSN. + * update documentation: make it explicit when `get_park_taxon_refs()` and `get_park_taxon_citations()` are hitting secure servers and warn users that the results may also need to be restricted. # NPSutils 0.2.0.3 * added `map_wkt()` function to map points, polygons, or both from Well Known Text coordinates (WKT). WKT is used in place to GPS coordinates when sensitive species locations have been "fuzzed". In this case, providing a polygon rather than the an exact (albeit fuzzed) is preferable as it is clear that the location is not exact. WKT is an efficient way to store geographic shapes such as polygons in flat files such as .csv. From 7debf63c55a9375c8a37a9ae9d12f7f14d5ad58e Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 11:16:46 -0600 Subject: [PATCH 07/12] update to version 0.3.0 --- README.Rmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.Rmd b/README.Rmd index 92eac8f..7395ff7 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,7 +18,8 @@ knitr::opts_chunk$set( [![CodeFactor](https://www.codefactor.io/repository/github/roblbaker/npsutils/badge)](https://www.codefactor.io/repository/github/roblbaker/npsutils) -# NPS Data Store Utilities - NPSutils +# NPS Data Store Utilities - NPSutils +##v0.3.0 This package is a collection of functions to acquire metadata and data from the [National Park Service Data Store](https://irma.nps.gov/DataStore/). This is an early version of this package and many features will be added in 2023. Please request enhancements and bug fixes through [Issues](https://github.com/nationalparkservice/NPSutils/issues). From 902681a53e3793e458b66d1f4b3d7a1b74c54b45 Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 11:17:15 -0600 Subject: [PATCH 08/12] fix typo in documentation --- R/get_data_packages.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_data_packages.R b/R/get_data_packages.R index 6ab6761..023385a 100644 --- a/R/get_data_packages.R +++ b/R/get_data_packages.R @@ -2,7 +2,7 @@ #' #' @description `get_data_packages()` creates a directory called "data" in the current working directory (or user-specified location, see the `path` option). For each data package, it writes a new sub-directory within the "data" directory named with the corresponding data package reference ID. All the data package files are then copied to the directory specific to the data package. #' -#' @detail In the default mode (force = FALSE), `get_data_packages()` will inform a user if the directory for that data package already exists and give the user the option to overwrite it (or skip downloading). In the default mode (force = FALSE), `get_data_packages()` will also check to see if there are newer versions of the requested data package and if there are newer versions, `get_data_packages()` will inform the user of when the requested data package was issued, when the newest data package was issued, and will then ask the user if they would like to download the newest version instead of the requested version. In the default mode (force = FALSE), `get_data_packages()` will warn a user if the reference ID supplied was not found (does not exist or requires VPN) and if a reference ID refers to a product that is not a data package, `get_data_packages()` will ask if the user wants to attempt to download it anyway. +#' @details In the default mode (force = FALSE), `get_data_packages()` will inform a user if the directory for that data package already exists and give the user the option to overwrite it (or skip downloading). In the default mode (force = FALSE), `get_data_packages()` will also check to see if there are newer versions of the requested data package and if there are newer versions, `get_data_packages()` will inform the user of when the requested data package was issued, when the newest data package was issued, and will then ask the user if they would like to download the newest version instead of the requested version. In the default mode (force = FALSE), `get_data_packages()` will warn a user if the reference ID supplied was not found (does not exist or requires VPN) and if a reference ID refers to a product that is not a data package, `get_data_packages()` will ask if the user wants to attempt to download it anyway. #' #' Although `get_data_packages()` is designed to handle the data package reference type on DataStore, it should in theory work on any reference type and download all files associated with a reference (it ignores web links/web resources associated with a reference). If the reference includes a .zip file, the file will be downloaded and the contents extracted. The original .zip archive file will then be deleted. If the .zip contains files with the same name as files in the parent directory, the parent directory files will be over-written by the contents of the .zip archive. #' From 19f3c1b2688be48e6525d40fdf3830e2734f0063 Mon Sep 17 00:00:00 2001 From: Rob Baker Date: Wed, 30 Aug 2023 11:17:42 -0600 Subject: [PATCH 09/12] autoupdate via devtools::document & pkgdown --- docs/404.html | 4 +-- docs/LICENSE-text.html | 4 +-- docs/LICENSE.html | 4 +-- docs/articles/NPSutils.html | 8 +++--- docs/articles/index.html | 4 +-- docs/authors.html | 12 ++++----- docs/index.html | 8 +++--- docs/news/index.html | 27 +++++++++++++++++--- docs/pkgdown.yml | 2 +- docs/reference/get_data_packages.html | 11 +++++--- docs/reference/get_park_code.html | 6 ++--- docs/reference/get_park_taxon_citations.html | 6 ++--- docs/reference/get_park_taxon_refs.html | 6 ++--- docs/reference/get_park_taxon_url.html | 6 ++--- docs/reference/get_ref_info.html | 6 ++--- docs/reference/get_unit_code.html | 6 ++--- docs/reference/get_unit_code_info.html | 6 ++--- docs/reference/get_unit_info.html | 6 ++--- docs/reference/index.html | 4 +-- docs/reference/load_data_package.html | 6 ++--- docs/reference/load_data_packages.html | 6 ++--- docs/reference/load_domains.html | 6 ++--- docs/reference/load_pkg_metadata.html | 6 ++--- docs/reference/map_wkt.html | 6 ++--- docs/reference/rm_local_packages.html | 6 ++--- docs/reference/validate_data_package.html | 6 ++--- 26 files changed, 102 insertions(+), 76 deletions(-) diff --git a/docs/404.html b/docs/404.html index 4819c4e..174853b 100644 --- a/docs/404.html +++ b/docs/404.html @@ -32,7 +32,7 @@ NPSutils - 0.2.0.3 + 0.3.0 @@ -50,7 +50,7 @@