diff --git a/DESCRIPTION b/DESCRIPTION index 3c23326..2897650 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: sbtools Title: USGS ScienceBase Tools Maintainer: David Blodgett -Version: 1.1.15 +Version: 1.1.16 Authors@R: c(person("David", "Blodgett", role=c("cre"), email = "dblodgett@usgs.gov"), person("Luke", "Winslow", role = c("aut"), diff --git a/R/REST_helpers.R b/R/REST_helpers.R index 1442284..6516e0d 100644 --- a/R/REST_helpers.R +++ b/R/REST_helpers.R @@ -15,7 +15,7 @@ sbtools_POST <- function(url, body, ..., session){ r = POST(url=url, ..., httrUserAgent(), accept_json(), body=body, handle=session, timeout = httr::timeout(default_timeout())) - handle_errors(r, url, "POST", supported_types) + r <- handle_errors(r, url, "POST", supported_types) # if (!strsplit(headers(r)[['content-type']], '[;]')[[1]][1] %in% supported_types) # stop('POST failed to ',url,'. check authorization and/or content') @@ -52,7 +52,7 @@ sbtools_GET <- function(url, ..., session = NULL) { "error was:\n", e)) return(list(status = 404)) }) - handle_errors(r, url, "GET", supported_types) + r <- handle_errors(r, url, "GET", supported_types) session_age_reset() return(r) } @@ -72,8 +72,8 @@ sbtools_GET <- function(url, ..., session = NULL) { #' @keywords internal sbtools_PUT <- function(url, body, ..., session) { check_session(session) - r = PUT(url = url, ..., httrUserAgent(), body = body, handle = session, timeout = httr::timeout(default_timeout())) - handle_errors(r, url, "PUT", NULL) + r <- PUT(url = url, ..., httrUserAgent(), body = body, handle = session, timeout = httr::timeout(default_timeout())) + r <- handle_errors(r, url, "PUT", NULL) session_age_reset() return(r) } @@ -94,7 +94,7 @@ sbtools_DELETE <- function(url, ..., session) { check_session(session) r = DELETE(url = url, ..., httrUserAgent(), accept_json(), handle = session, timeout = httr::timeout(default_timeout())) - handle_errors(r, url, "DELETE", NULL) + r <- handle_errors(r, url, "DELETE", NULL) session_age_reset() return(r) } @@ -123,13 +123,24 @@ handle_errors <- function(x, url, method, types) { if (!is.null(types)) { if (!strsplit(headers(x)[['content-type']], '[;]')[[1]][1] %in% types) { - stop(method, ' failed to ', url, '. check authorization and/or content', call. = FALSE) + message(method, ' failed to ', url, '. check authorization and/or content', call. = FALSE) + return(NULL) } } if ('errors' %in% names(content(x))) { - stop(content(x)$errors$message, call. = FALSE) + + if(length(errors <- content(x)$errors) == 1) { + message(errors$message, call. = FALSE) + } else { + message(paste(sapply(errors, function (x) x$message), collapse = "\n"), call. = FALSE) + } + + return(NULL) } + + return(x) + } #' @importFrom curl curl_version diff --git a/R/current_session.R b/R/current_session.R index c1c6eb6..6f0489d 100644 --- a/R/current_session.R +++ b/R/current_session.R @@ -7,12 +7,12 @@ #' #' #' -#' @examples +#' @examples \donttest{ #' #' session = current_session() #' #null unless currently authenticated #' session -#' +#' } #'@export current_session = function(){ if(session_expired(pkg.env$session)){ diff --git a/R/identifier_exists.R b/R/identifier_exists.R index 21fdd5c..206cd02 100644 --- a/R/identifier_exists.R +++ b/R/identifier_exists.R @@ -10,14 +10,13 @@ #' @importFrom methods is #' #' @return Logical, \code{TRUE} or \code{FALSE} -#' @examples -#' +#' @examples \donttest{ #' # identifier exists #' identifier_exists(sb_id = "4f4e4b24e4b07f02db6aea14") #' #' # identifier does not exist #' identifier_exists(sb_id = "aaaaaaakkkkkkkbbbbbb") -#' +#' } identifier_exists <- function(sb_id, ..., session = current_session()) { #sb_id = as.sbitem(sb_id) if(is(sb_id, 'sbitem')){ diff --git a/R/item_exists.R b/R/item_exists.R index e13727a..4b01a91 100644 --- a/R/item_exists.R +++ b/R/item_exists.R @@ -9,8 +9,7 @@ #' @param session an SB session #' @return boolean for whether item exists #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' item_exists('mda_streams','ts_doobs','nwis_01018035') #' item_exists('mda_streams','site_root','nwis_01018035') #' } diff --git a/R/item_get.R b/R/item_get.R index 9f4b3e8..e081d93 100644 --- a/R/item_get.R +++ b/R/item_get.R @@ -10,8 +10,7 @@ #' access is restricted due to permissions. #' #' -#' @examples -#' \donttest{ +#' @examples \donttest{ #' # Get an item #' item_get("4f4e4b24e4b07f02db6aea14") #' @@ -35,6 +34,8 @@ get_item <- function(id, ..., session=current_session()) { if(is(res, "list")) { if(res$status == 404) return(NULL) + } else if(is.null(res)) { + return(NULL) } return(as.sbitem(content(res))) diff --git a/R/item_get_fields.R b/R/item_get_fields.R index 7dd53ca..d3c8252 100644 --- a/R/item_get_fields.R +++ b/R/item_get_fields.R @@ -7,8 +7,7 @@ #' @return List serialization of chosen metadata for an SB item #' @import httr #' @export -#' @examples -#' \donttest{ +#' @examples \donttest{ #' # Get certain fields from an item #' item_get_fields("4f4e4b24e4b07f02db6aea14", c('title', 'citation', 'contacts')) #' diff --git a/R/item_list_files.R b/R/item_list_files.R index fea4665..52fb487 100644 --- a/R/item_list_files.R +++ b/R/item_list_files.R @@ -13,11 +13,9 @@ #' files attached to an item and all children items. #' #' @export -#' @examples -#' \donttest{ +#' @examples \dontrun{ +#' #' item_list_files("4f4e4b24e4b07f02db6aea14") -#' } -#' \dontrun{ #' # list files recursively #' ## create item #' id <- item_create(user_id(), title="some title") diff --git a/R/item_rename_files.R b/R/item_rename_files.R index e9c7482..6ac1c7b 100644 --- a/R/item_rename_files.R +++ b/R/item_rename_files.R @@ -7,8 +7,7 @@ #' @param names List of names of files to rename #' @param new_names List of new file names to use #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' #' names = c('file1.txt', 'file2.txt') #' new_names = c('newname1.txt', 'newname2.txt') diff --git a/R/item_update_identifier.R b/R/item_update_identifier.R index eb53d15..79b73d9 100644 --- a/R/item_update_identifier.R +++ b/R/item_update_identifier.R @@ -11,9 +11,7 @@ #' \code{\link{query_item_identifier}} for finding items based on alternative #' identifier. #' -#' @examples -#' -#' \dontrun{ +#' @examples \dontrun{ #' #' session = authenticate_sb("user@@usgs.gov") #' item_update_identifier("5485fd99e4b02acb4f0c7e81", "scheme", "type", "key", session=session) diff --git a/R/item_upload_files.R b/R/item_upload_files.R index ed57d29..97c0a67 100644 --- a/R/item_upload_files.R +++ b/R/item_upload_files.R @@ -56,8 +56,7 @@ item_upload_create = function(parent_id, files, ..., scrape_files = TRUE, sessio #' #' @import httr #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' res <- item_create(user_id(), "testing 123") #' cat("foo bar", file = "foobar.txt") #' item_append_files(res$id, "foobar.txt") @@ -71,6 +70,8 @@ item_append_files = function(sb_id, files, ..., scrape_files = TRUE, session=cur item <- as.sbitem(sb_id) + if(is.null(item)) return(NULL) + params <- paste0("?id=", item$id) if(!scrape_files) { diff --git a/R/query_item_identifier.R b/R/query_item_identifier.R index 43e4847..e30e85d 100644 --- a/R/query_item_identifier.R +++ b/R/query_item_identifier.R @@ -13,8 +13,7 @@ #' @import jsonlite #' @import httr #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' authenticate_sb() #' #' ex_item = item_create(title='identifier example') diff --git a/R/query_sb.R b/R/query_sb.R index baf1b32..b0ee566 100644 --- a/R/query_sb.R +++ b/R/query_sb.R @@ -47,8 +47,7 @@ #' boxes and representational points. This is a alphanumeric string. #' } #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' query_sb(list(q = "water")) #' #' # Search by project status diff --git a/R/query_sb_datatype.R b/R/query_sb_datatype.R index 17726ed..95c7b1e 100644 --- a/R/query_sb_datatype.R +++ b/R/query_sb_datatype.R @@ -13,8 +13,7 @@ #' @description #' Queries ScienceBase for items with matching datatype. #' -#' @examples -#' \donttest{ +#' @examples \donttest{ #' #query for items with WFS Layer data #' query_sb_datatype('Static Map Image') #' @@ -40,10 +39,10 @@ query_sb_datatype = function(datatype, ..., limit=20, session=current_session()) #' coupled with \code{\link{query_sb_datatype}} to query based on the type of data #' #' -#' @examples +#' @examples \donttest{ #' #return all datatypes (limit 50 by default) #' sb_datatypes() -#' +#' } #' #' @export sb_datatypes = function(limit=50, session=current_session()){ diff --git a/R/query_sb_date.R b/R/query_sb_date.R index 3c9cba7..afff899 100644 --- a/R/query_sb_date.R +++ b/R/query_sb_date.R @@ -15,8 +15,7 @@ #' @description #' Queries ScienceBase for items with timestamps within a certain date/time range. #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' # find items updated today #' query_sb_date(Sys.time(), Sys.time()) #' diff --git a/R/query_sb_doi.R b/R/query_sb_doi.R index a278fe5..7c8c176 100644 --- a/R/query_sb_doi.R +++ b/R/query_sb_doi.R @@ -12,12 +12,12 @@ #' Queries for ScienceBase items with a specific DOI identifier. #' In ScienceBase, these are stored as additional unique identifiers. #' -#' @examples +#' @examples \donttest{ #' #Two example DOI-specific queries #' query_sb_doi('10.5066/F7M043G7') #' #' query_sb_doi('10.5066/F7Z60M35') -#' +#' } #' @export query_sb_doi = function(doi, ..., limit=20, session=current_session()){ diff --git a/R/query_sb_spatial.R b/R/query_sb_spatial.R index f7ccb6b..bb9c934 100644 --- a/R/query_sb_spatial.R +++ b/R/query_sb_spatial.R @@ -11,8 +11,7 @@ #' (uses the spatial object's bounding box) or long/lat coordinates defining the bounding box limits. #' #' -#' @examples -#' +#' @examples \donttest{ #' #specify the latitude and longitude points to define the bounding box range. #' # This is simply bottom left and top right points #' query_sb_spatial(long=c(-104.4, -95.1), lat=c(37.5, 41.0), limit=3) @@ -20,7 +19,7 @@ #' #use a pre-formatted WKT polygon to grab data #' query_sb_spatial(bb_wkt="POLYGON((-104.4 41.0,-95.1 41.0,-95.1 37.5,-104.4 37.5,-104.4 41.0))", #' limit=3) -#' +#' } #' @export #' query_sb_spatial = function(bbox, long, lat, bb_wkt, ..., limit=20, session=current_session()){ diff --git a/R/query_sb_text.R b/R/query_sb_text.R index e316093..f1010df 100644 --- a/R/query_sb_text.R +++ b/R/query_sb_text.R @@ -12,13 +12,13 @@ #' Queries for ScienceBase items that have matching text in the title or #' description #' -#' @examples +#' @examples \donttest{ #' #query for a person's name #' query_sb_text('Luna Leopold') #' #' #query for one of the old river gaging stations #' query_sb_text('Lees Ferry') -#' +#' } #' #' @export query_sb_text = function(text, ..., limit=20, session=current_session()){ diff --git a/R/sb_ping.R b/R/sb_ping.R index e6a6464..ee954ee 100644 --- a/R/sb_ping.R +++ b/R/sb_ping.R @@ -4,10 +4,10 @@ #' @param ... Additional parameters are passed on to \code{\link[httr]{GET}} #' @return Boolean (TRUE) indicating if a connection to ScienceBase can be established #' and if it is responding as expected. FALSE otherwise. -#' @examples +#' @examples \donttest{ #' #TRUE if all is well and SB can be contacted #' sb_ping() -#' +#' } sb_ping <- function(...) { tryCatch({ diff --git a/R/session_age.R b/R/session_age.R index f378207..904e782 100644 --- a/R/session_age.R +++ b/R/session_age.R @@ -5,8 +5,7 @@ #' #' @template manipulate_item #' @return difftime object -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' authenticate_sb('bbadger@@usgs.gov') #' sbtools::session_age() #' } diff --git a/R/session_renew.R b/R/session_renew.R index 4200c17..f40e55b 100644 --- a/R/session_renew.R +++ b/R/session_renew.R @@ -13,8 +13,7 @@ #' #' @return Returns the session object. #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' # an empty call is sufficient if the session is current, #' # but will break if haven't been logged in before #' session_renew() diff --git a/R/session_validate.R b/R/session_validate.R index 8b46d3f..f546031 100644 --- a/R/session_validate.R +++ b/R/session_validate.R @@ -15,8 +15,7 @@ #' #' @importFrom methods new #' -#' @examples -#' \dontrun{ +#' @examples \dontrun{ #' session = authenticate_sb('user@@usgs.gov') #' #' #return true as underlying RCurl session is valid diff --git a/R/set_endpoint.R b/R/set_endpoint.R index b6ca666..b48a626 100644 --- a/R/set_endpoint.R +++ b/R/set_endpoint.R @@ -8,9 +8,7 @@ #' #'@author Luke Winslow #' -#'@examples -#' -#'\donttest{ +#'@examples \donttest{ #'set_endpoint('prod') #' #'# getting item from production SB servers diff --git a/man/current_session.Rd b/man/current_session.Rd index 15f167c..1e3b8a8 100644 --- a/man/current_session.Rd +++ b/man/current_session.Rd @@ -12,9 +12,10 @@ is no authenticated session, returns NULL. Emits a warning if the session has expired. } \examples{ +\donttest{ session = current_session() #null unless currently authenticated session - +} } diff --git a/man/identifier_exists.Rd b/man/identifier_exists.Rd index 10aefe1..0ceea59 100644 --- a/man/identifier_exists.Rd +++ b/man/identifier_exists.Rd @@ -25,11 +25,11 @@ This will also return \code{FALSE} if the identifier exists but is associated with an item that is unavailable due to permission restrictions. } \examples{ - +\donttest{ # identifier exists identifier_exists(sb_id = "4f4e4b24e4b07f02db6aea14") # identifier does not exist identifier_exists(sb_id = "aaaaaaakkkkkkkbbbbbb") - +} } diff --git a/man/item_list_files.Rd b/man/item_list_files.Rd index 5ea5faa..f8d2bfe 100644 --- a/man/item_list_files.Rd +++ b/man/item_list_files.Rd @@ -27,10 +27,9 @@ using \code{\link{item_file_download}}. (advanced) Recursive options lists all files attached to an item and all children items. } \examples{ -\donttest{ -item_list_files("4f4e4b24e4b07f02db6aea14") -} \dontrun{ + +item_list_files("4f4e4b24e4b07f02db6aea14") # list files recursively ## create item id <- item_create(user_id(), title="some title") diff --git a/man/item_update_identifier.Rd b/man/item_update_identifier.Rd index 15fc7b4..765cb1d 100644 --- a/man/item_update_identifier.Rd +++ b/man/item_update_identifier.Rd @@ -35,7 +35,6 @@ additional identifiers or update those already in place. See identifier. } \examples{ - \dontrun{ session = authenticate_sb("user@usgs.gov") diff --git a/man/query_sb_doi.Rd b/man/query_sb_doi.Rd index 7b5aa67..d8c5526 100644 --- a/man/query_sb_doi.Rd +++ b/man/query_sb_doi.Rd @@ -26,9 +26,10 @@ Queries for ScienceBase items with a specific DOI identifier. In ScienceBase, these are stored as additional unique identifiers. } \examples{ +\donttest{ #Two example DOI-specific queries query_sb_doi('10.5066/F7M043G7') query_sb_doi('10.5066/F7Z60M35') - +} } diff --git a/man/query_sb_spatial.Rd b/man/query_sb_spatial.Rd index ff9fbcf..390f4cc 100644 --- a/man/query_sb_spatial.Rd +++ b/man/query_sb_spatial.Rd @@ -36,7 +36,7 @@ Queries ScienceBase based on a spatial bounding box. Accepts either an sp spatia (uses the spatial object's bounding box) or long/lat coordinates defining the bounding box limits. } \examples{ - +\donttest{ #specify the latitude and longitude points to define the bounding box range. # This is simply bottom left and top right points query_sb_spatial(long=c(-104.4, -95.1), lat=c(37.5, 41.0), limit=3) @@ -44,5 +44,5 @@ query_sb_spatial(long=c(-104.4, -95.1), lat=c(37.5, 41.0), limit=3) #use a pre-formatted WKT polygon to grab data query_sb_spatial(bb_wkt="POLYGON((-104.4 41.0,-95.1 41.0,-95.1 37.5,-104.4 37.5,-104.4 41.0))", limit=3) - +} } diff --git a/man/query_sb_text.Rd b/man/query_sb_text.Rd index 3a96a68..8bea702 100644 --- a/man/query_sb_text.Rd +++ b/man/query_sb_text.Rd @@ -26,11 +26,12 @@ Queries for ScienceBase items that have matching text in the title or description } \examples{ +\donttest{ #query for a person's name query_sb_text('Luna Leopold') #query for one of the old river gaging stations query_sb_text('Lees Ferry') - +} } diff --git a/man/sb_datatypes.Rd b/man/sb_datatypes.Rd index fbc619a..b501947 100644 --- a/man/sb_datatypes.Rd +++ b/man/sb_datatypes.Rd @@ -18,8 +18,9 @@ Queries ScienceBase for the list of all available datatypes. This can be coupled with \code{\link{query_sb_datatype}} to query based on the type of data } \examples{ +\donttest{ #return all datatypes (limit 50 by default) sb_datatypes() - +} } diff --git a/man/sb_ping.Rd b/man/sb_ping.Rd index 4b48307..036a330 100644 --- a/man/sb_ping.Rd +++ b/man/sb_ping.Rd @@ -17,7 +17,8 @@ and if it is responding as expected. FALSE otherwise. Ping ScienceBase to see if it's available } \examples{ +\donttest{ #TRUE if all is well and SB can be contacted sb_ping() - +} } diff --git a/man/set_endpoint.Rd b/man/set_endpoint.Rd index 562eed0..2892671 100644 --- a/man/set_endpoint.Rd +++ b/man/set_endpoint.Rd @@ -15,7 +15,6 @@ Sets the internal URLS used to either the production or development (beta) SB server. URLS are stored internally to the package } \examples{ - \donttest{ set_endpoint('prod') diff --git a/tests/testthat/test-REST.R b/tests/testthat/test-REST.R index 6b54fcd..0dba9e2 100644 --- a/tests/testthat/test-REST.R +++ b/tests/testthat/test-REST.R @@ -4,16 +4,16 @@ test_that("generic post fails w/o auth", { skip_on_cran() # auth fails locally: - expect_error(item_append_files("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip'),'Item not found for') + expect_message(item_append_files("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip'),'Item not found for') session <- httr::handle("https://google.com") attributes(session) <- c(attributes(session), list(birthdate=Sys.time())) # auth passes locally, but POST fails due to files not existing: - expect_error(item_append_files("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip', session=session), 'Item not found for') + expect_message(item_append_files("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip', session=session), 'Item not found for') # auth passes, and file is there, but auth fails on sciencebase.gov files <- system.file('extdata',"This_works_new_extension.zip", package='sbtools') - expect_error(item_append_files("54e265a4e4b08de9379b4dfb", files, session=session), + expect_message(item_append_files("54e265a4e4b08de9379b4dfb", files, session=session), 'Item not found for') }) @@ -33,8 +33,8 @@ test_that("generic get w/ and w/o auth", { expect_is(item_get(public_item, session = session), 'sbitem') # 'not found' error for missing items, with or without login - expect_error(item_get(non_item, session = NULL), 'Item not found') - expect_error(item_get(non_item, session = session), 'Item not found') + expect_message(item_get(non_item, session = NULL), 'Item not found') + expect_message(item_get(non_item, session = session), 'Item not found') }) @@ -43,7 +43,7 @@ test_that("REST_helpers tests", { item <- sbtools_GET("https://www.sciencebase.gov/catalog/item/5c4f4a04e4b0708288f78e07") - expect_error(sbtools_GET("https://www.sciencebase.gov/catalog/item/a04e4b0708288f78e07"), + expect_message(sbtools_GET("https://www.sciencebase.gov/catalog/item/a04e4b0708288f78e07"), "Invalid Item ID") expect_error(sbtools_GET("https://www.sciencebase.gov/catalog/item/a04e4b0708288f78e07", session = list(session = "borked")), @@ -62,5 +62,5 @@ test_that("REST_helpers tests", { expect_equal(delete_test$status_code, 405) - expect_error(post_test <- sbtools_POST("https://www.sciencebase.gov/catalog/item/5c4f4a04e4b0708288f78e07", "test", session = session)) + expect_message(post_test <- sbtools_POST("https://www.sciencebase.gov/catalog/item/5c4f4a04e4b0708288f78e07", "test", session = session)) }) diff --git a/tests/testthat/test-auth.R b/tests/testthat/test-auth.R index 76dc70a..c83f76e 100644 --- a/tests/testthat/test-auth.R +++ b/tests/testthat/test-auth.R @@ -54,7 +54,7 @@ test_that("item creation, identifiers, and file upload works", { #remove the test item when done item_rm(item) - expect_error(item_get(item), 'Item not found*.') + expect_message(item_get(item), 'Item not found*.') expect_silent(session_logout()) }) diff --git a/tests/testthat/test_examples.R b/tests/testthat/test_examples.R index 88b8e21..6ba750e 100644 --- a/tests/testthat/test_examples.R +++ b/tests/testthat/test_examples.R @@ -95,7 +95,7 @@ test_that("basic examples work", { wfs_data <- item_get_wfs("58c988bce4b0849ce97b4845"), "item_get_wfs is going to be removed in a future version of sbtools") - expect_equal(as.character(class(wfs_data)), "SpatialPointsDataFrame") + wexpect_equal(as.character(class(wfs_data)), "SpatialPointsDataFrame") suppressWarnings(wfs_data <- item_get_wfs("58c988bce4b0849ce97b4845", as_sf = TRUE))