diff --git a/R/editing.R b/R/editing.R index 187bb41..9199019 100644 --- a/R/editing.R +++ b/R/editing.R @@ -45,9 +45,9 @@ publish_object <- function(mn, } # Check if format ID is valid - if (!(format_id %in% D1_FORMATS)) { + if (!is.null(D1_FORMATS) && !(format_id %in% D1_FORMATS)) { stop(call. = FALSE, - paste0("The format_id of '", format_id, "' is not a valid format ID. See https://cn.dataone.org/cn/v2/formats for the current list. This package stores a copy and may be out of date with that list so please email the author if needed.")) + paste0("The provided format_id of '", format_id, "' is not a valid format ID. Check what you entered against the list of format IDs on https://cn.dataone.org/cn/v2/formats. Note that this list is cached when arcticdatautils is loaded so if you haven't restarted your R session in a while you may have an outdated copy and restarting your session may fix this.")) } # Set up some variables for use later on @@ -138,9 +138,9 @@ update_object <- function(mn, pid, path, format_id=NULL, new_pid=NULL, sid=NULL) } # Check if format ID is valid - if (!(format_id %in% D1_FORMATS)) { + if (!is.null(D1_FORMATS) && !(format_id %in% D1_FORMATS)) { stop(call. = FALSE, - paste0("The format_id of '", format_id, "' is not a valid format ID. See https://cn.dataone.org/cn/v2/formats for the current list. This package stores a copy and may be out of date with that list so please email the author if needed.")) + paste0("The provided format_id of '", format_id, "' is not a valid format ID. Check what you entered against the list of format IDs on https://cn.dataone.org/cn/v2/formats. Note that this list is cached when arcticdatautils is loaded so if you haven't restarted your R session in a while you may have an outdated copy and restarting your session may fix this.")) } message(paste0("Updating object ", pid, " with the file at ", path, ".")) diff --git a/R/sysdata.rda b/R/sysdata.rda deleted file mode 100644 index acb96fc..0000000 Binary files a/R/sysdata.rda and /dev/null differ diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..b34d3f5 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,51 @@ +.onLoad <- function(libname, pkgname) { + load_d1_formats_list("https://cn.dataone.org/cn/v2/formats") + warn_if_outdated() + + invisible() +} + +# Warning: This function produces a side-effect of assigning into the package +# env with <<- to update the value of D1_FORMATS +D1_FORMATS <- NULL +load_d1_formats_list <- function(url) { + req <- httr::GET(url) + + if (httr::status_code(req) != 200) { + warning(paste0("Failed to load an up-to-date list of format IDs from ", url, " because the request to the CN failed. Checking of format IDs is disabled.")) + return(invisible()) + } + + formats_content <- httr::content(req, encoding = "UTF-8") + format_id_nodes <- xml2::xml_find_all(formats_content, "//formatId") + + if (length(format_id_nodes) == 0) { + warning(paste0("Failed to load an up-to-date list of format IDs from ", url, " because the response returned from the CN was not formatted as expected. Checking of format IDs is disabled.")) + return(invisible()) + } + + D1_FORMATS <<- vapply(format_id_nodes, function(x) { xml2::xml_text(x) }, "") +} + +warn_if_outdated <- function() { + installed_version <- packageVersion("arcticdatautils") + + req <- httr::GET("https://api.github.com/repos/nceas/arcticdatautils/releases/latest", + httr::add_headers("Accept", "application/vnd.github.v3+json")) + + release <- httr::content(req) + + if (httr::status_code(req) != 200) { + warning(paste0("The request to check whether the version of arcticdatautils you have installed is the latest, the response from GitHub's API failed. The response was: \n\n", req)) + return(invisible()) + } + + if (!("tag_name" %in% names(release))) { + warning(paste0("While checking to see if your installed version of arcticdatautils is the latest available, the response from GitHub's API was not as expected so checking was not done.")) + return(invisible()) + } + + if (!stringr::str_detect(release$tag_name, paste0("^v", stringr::str_replace_all(as.character(installed_version), "\\.", "\\\\."), "$"))) { + warning(paste0("You do not have the most recent version of arcticdatautils installed. This is just a reminder to update next time you get the chance. Visit https://github.com/NCEAS/arcticdatautils/releases to find the latest release.")) + } +}