USGS-R / sbtools
Showing 23 of 41 files from the diff.

@@ -5,8 +5,7 @@
Loading
5 5
#' 
6 6
#' @template manipulate_item
7 7
#' @return difftime object
8 -
#' @examples 
9 -
#' \dontrun{
8 +
#' @examples \dontrun{
10 9
#' authenticate_sb('bbadger@@usgs.gov')
11 10
#' sbtools::session_age()
12 11
#' }

@@ -12,13 +12,13 @@
Loading
12 12
#' Queries for ScienceBase items that have matching text in the title or 
13 13
#' description
14 14
#' 
15 -
#' @examples 
15 +
#' @examples \donttest{
16 16
#' #query for a person's name
17 17
#' query_sb_text('Luna Leopold')
18 18
#' 
19 19
#' #query for one of the old river gaging stations
20 20
#' query_sb_text('Lees Ferry')
21 -
#' 
21 +
#' }
22 22
#' 
23 23
#' @export
24 24
query_sb_text = function(text, ..., limit=20, session=current_session()){

@@ -10,14 +10,13 @@
Loading
10 10
#' @importFrom methods is
11 11
#' 
12 12
#' @return Logical, \code{TRUE} or \code{FALSE}
13 -
#' @examples
14 -
#' 
13 +
#' @examples \donttest{
15 14
#' # identifier exists
16 15
#' identifier_exists(sb_id = "4f4e4b24e4b07f02db6aea14")
17 16
#' 
18 17
#' # identifier does not exist
19 18
#' identifier_exists(sb_id = "aaaaaaakkkkkkkbbbbbb")
20 -
#' 
19 +
#' }
21 20
identifier_exists <- function(sb_id, ..., session = current_session()) {
22 21
	#sb_id = as.sbitem(sb_id)
23 22
	if(is(sb_id, 'sbitem')){

@@ -8,9 +8,7 @@
Loading
8 8
#'
9 9
#'@author Luke Winslow
10 10
#'
11 -
#'@examples
12 -
#'
13 -
#'\donttest{
11 +
#'@examples \donttest{
14 12
#'set_endpoint('prod')
15 13
#'
16 14
#'# getting item from production SB servers

@@ -11,9 +11,7 @@
Loading
11 11
#' \code{\link{query_item_identifier}} for finding items based on alternative
12 12
#' identifier.
13 13
#'  
14 -
#' @examples
15 -
#'
16 -
#' \dontrun{
14 +
#' @examples \dontrun{
17 15
#'
18 16
#' session = authenticate_sb("user@@usgs.gov")
19 17
#' item_update_identifier("5485fd99e4b02acb4f0c7e81", "scheme", "type", "key", session=session)

@@ -15,8 +15,7 @@
Loading
15 15
#' @description 
16 16
#' Queries ScienceBase for items with timestamps within a certain date/time range.
17 17
#' 
18 -
#' @examples
19 -
#' \dontrun{
18 +
#' @examples \dontrun{
20 19
#' # find items updated today
21 20
#' query_sb_date(Sys.time(), Sys.time())
22 21
#' 

@@ -9,8 +9,7 @@
Loading
9 9
#' @param session an SB session
10 10
#' @return boolean for whether item exists
11 11
#'
12 -
#' @examples
13 -
#' \dontrun{
12 +
#' @examples \dontrun{
14 13
#' item_exists('mda_streams','ts_doobs','nwis_01018035')
15 14
#' item_exists('mda_streams','site_root','nwis_01018035')
16 15
#' }

@@ -56,8 +56,7 @@
Loading
56 56
#'
57 57
#' @import httr
58 58
#'
59 -
#' @examples 
60 -
#' \dontrun{
59 +
#' @examples \dontrun{
61 60
#' res <- item_create(user_id(), "testing 123")
62 61
#' cat("foo bar", file = "foobar.txt")
63 62
#' item_append_files(res$id, "foobar.txt")
@@ -71,6 +70,8 @@
Loading
71 70
	
72 71
	item <- as.sbitem(sb_id)
73 72
	
73 +
	if(is.null(item)) return(NULL)
74 +
	
74 75
	params <- paste0("?id=", item$id)
75 76
	
76 77
	if(!scrape_files) {

@@ -15,8 +15,7 @@
Loading
15 15
#' 
16 16
#' @importFrom methods new
17 17
#' 
18 -
#' @examples
19 -
#' \dontrun{
18 +
#' @examples \dontrun{
20 19
#' session = authenticate_sb('user@@usgs.gov')
21 20
#' 
22 21
#' #return true as underlying RCurl session is valid

@@ -7,8 +7,7 @@
Loading
7 7
#' @param names List of names of files to rename
8 8
#' @param new_names List of new file names to use
9 9
#' 
10 -
#' @examples 
11 -
#' \dontrun{
10 +
#' @examples \dontrun{
12 11
#' 
13 12
#' names = c('file1.txt', 'file2.txt')
14 13
#' new_names = c('newname1.txt', 'newname2.txt')

@@ -4,10 +4,10 @@
Loading
4 4
#' @param ... Additional parameters are passed on to \code{\link[httr]{GET}}
5 5
#' @return Boolean (TRUE) indicating if a connection to ScienceBase can be established 
6 6
#' and if it is responding as expected. FALSE otherwise. 
7 -
#' @examples 
7 +
#' @examples \donttest{
8 8
#' #TRUE if all is well and SB can be contacted
9 9
#' sb_ping()
10 -
#' 
10 +
#' }
11 11
sb_ping <- function(...) {
12 12
13 13
	tryCatch({

@@ -12,12 +12,12 @@
Loading
12 12
#' Queries for ScienceBase items with a specific DOI identifier. 
13 13
#' In ScienceBase, these are stored as additional unique identifiers.
14 14
#' 
15 -
#' @examples 
15 +
#' @examples \donttest{
16 16
#' #Two example DOI-specific queries
17 17
#' query_sb_doi('10.5066/F7M043G7')
18 18
#' 
19 19
#' query_sb_doi('10.5066/F7Z60M35')
20 -
#' 
20 +
#' }
21 21
#' @export
22 22
query_sb_doi = function(doi, ..., limit=20, session=current_session()){
23 23
	

@@ -7,13 +7,41 @@
Loading
7 7
#' Get the details associated with current ScienceBase user session. 
8 8
#' 
9 9
#' @return list, if not logged in states that, but if logged in, user details
10 -
#' 
10 +
#' If logged in, will include a "jossoSessionId" that can be used for 
11 +
#' authenticated file downloads by appending josso={'jossoSessionId'} to the 
12 +
#' url for the file requiring authentication. This is helpful when passing
13 +
#' a url to a library that will handle the download of a remote file automatically 
14 +
#' such as jsonlite::readJSON or readr::read_csv.
11 15
#' @examples \dontrun{
12 -
#' session_info()
16 +
#' 
17 +
#' session_details()
18 +
#'  
19 +
#' # If logged in, can use jossoSessionId for downloads.
20 +
#' authenticate_sb()
21 +
#' 
22 +
#' temp_json <- tempfile(fileext = ".json")
23 +
#' 
24 +
#' jsonlite::write_json(list(test = "test"), temp_json)
25 +
#' 
26 +
#' item <- item_upload_create(sbtools::user_id(), temp_json)
27 +
#' 
28 +
#' token <- session_details()$jossoSessionId
29 +
#' 
30 +
#' (base_url <- item$file[[1]]$downloadUri)
31 +
#' 
32 +
#' # will fail
33 +
#' try(jsonlite::read_json(base_url))
34 +
#' 
35 +
#' url <- paste0(base_url, "&josso=", token)
36 +
#' 
37 +
#' jsonlite::read_json(url)
38 +
#' 
39 +
#' item_rm(item$id)
40 +
#' 
13 41
#' }
14 42
#' @export
15 43
session_details <- function(..., session = current_session()) {
16 -
	x <- GET(paste0(pkg.env$url_base, "jossoHelper/sessionInfo"), 
44 +
	x <- GET(paste0(pkg.env$url_base, "jossoHelper/sessionInfo?includeJossoSessionId=true"), 
17 45
					 handle = session, timeout = httr::timeout(default_timeout()),
18 46
					 ...)
19 47
	stop_for_status(x)

@@ -11,8 +11,7 @@
Loading
11 11
#' (uses the spatial object's bounding box) or long/lat coordinates defining the bounding box limits. 
12 12
#' 
13 13
#' 
14 -
#' @examples
15 -
#' 
14 +
#' @examples \donttest{
16 15
#' #specify the latitude and longitude points to define the bounding box range. 
17 16
#' # This is simply bottom left and top right points
18 17
#' query_sb_spatial(long=c(-104.4, -95.1), lat=c(37.5, 41.0), limit=3)
@@ -20,7 +19,7 @@
Loading
20 19
#' #use a pre-formatted WKT polygon to grab data
21 20
#' 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))", 
22 21
#' 	                limit=3)
23 -
#' 
22 +
#' }
24 23
#' @export
25 24
#' 
26 25
query_sb_spatial = function(bbox, long, lat, bb_wkt, ..., limit=20, session=current_session()){

@@ -13,8 +13,7 @@
Loading
13 13
#' @import jsonlite
14 14
#' @import httr
15 15
#' 
16 -
#' @examples 
17 -
#' \dontrun{
16 +
#' @examples \dontrun{
18 17
#' authenticate_sb()
19 18
#' 
20 19
#' ex_item = item_create(title='identifier example')

@@ -7,8 +7,7 @@
Loading
7 7
#' @return List serialization of chosen metadata for an SB item
8 8
#' @import httr
9 9
#' @export
10 -
#' @examples 
11 -
#' \donttest{
10 +
#' @examples \donttest{
12 11
#' # Get certain fields from an item
13 12
#' item_get_fields("4f4e4b24e4b07f02db6aea14", c('title', 'citation', 'contacts'))
14 13
#' 

@@ -47,8 +47,7 @@
Loading
47 47
#'  boxes and representational points. This is a alphanumeric string.
48 48
#' }
49 49
#' 
50 -
#' @examples 
51 -
#' \dontrun{
50 +
#' @examples \dontrun{
52 51
#' query_sb(list(q = "water"))
53 52
#' 
54 53
#' # Search by project status

@@ -13,11 +13,9 @@
Loading
13 13
#' files attached to an item and all children items.
14 14
#'
15 15
#' @export
16 -
#' @examples 
17 -
#' \donttest{
16 +
#' @examples \dontrun{
17 +
#' 
18 18
#' item_list_files("4f4e4b24e4b07f02db6aea14")
19 -
#' }
20 -
#' \dontrun{
21 19
#' # list files recursively
22 20
#' ## create item
23 21
#' id <- item_create(user_id(), title="some title")

@@ -15,7 +15,7 @@
Loading
15 15
	
16 16
	r = POST(url=url, ..., httrUserAgent(), accept_json(), body=body, handle=session, 
17 17
					 timeout = httr::timeout(default_timeout())) 
18 -
	handle_errors(r, url, "POST", supported_types)	
18 +
	r <- handle_errors(r, url, "POST", supported_types)	
19 19
	# if (!strsplit(headers(r)[['content-type']], '[;]')[[1]][1] %in% supported_types)
20 20
	# 	stop('POST failed to ',url,'. check authorization and/or content')
21 21
	
@@ -52,7 +52,7 @@
Loading
52 52
																		"error was:\n", e))
53 53
																 return(list(status = 404))
54 54
	})
55 -
	handle_errors(r, url, "GET", supported_types)
55 +
	r <- handle_errors(r, url, "GET", supported_types)
56 56
	session_age_reset()
57 57
	return(r)
58 58
}
@@ -72,8 +72,8 @@
Loading
72 72
#' @keywords internal
73 73
sbtools_PUT <- function(url, body, ..., session) {
74 74
	check_session(session)
75 -
	r = PUT(url = url, ..., httrUserAgent(), body = body, handle = session, timeout = httr::timeout(default_timeout()))
76 -
	handle_errors(r, url, "PUT", NULL)
75 +
	r <- PUT(url = url, ..., httrUserAgent(), body = body, handle = session, timeout = httr::timeout(default_timeout()))
76 +
	r <- handle_errors(r, url, "PUT", NULL)
77 77
	session_age_reset()
78 78
	return(r)
79 79
}
@@ -94,7 +94,7 @@
Loading
94 94
	check_session(session)
95 95
	r = DELETE(url = url, ..., httrUserAgent(), accept_json(), 
96 96
						 handle = session, timeout = httr::timeout(default_timeout()))
97 -
	handle_errors(r, url, "DELETE", NULL)
97 +
	r <- handle_errors(r, url, "DELETE", NULL)
98 98
	session_age_reset()
99 99
	return(r)
100 100
}
@@ -116,6 +116,7 @@
Loading
116 116
117 117
# helpers -------------
118 118
handle_errors <- function(x, url, method, types) {
119 +
	tryCatch({
119 120
	if(is(x, "list")) {
120 121
		if(x$status == 404) warning("Could not access sciencebase")
121 122
		return(NULL)
@@ -123,13 +124,29 @@
Loading
123 124
	
124 125
	if (!is.null(types)) {
125 126
		if (!strsplit(headers(x)[['content-type']], '[;]')[[1]][1] %in% types) {
126 -
			stop(method, ' failed to ', url, '. check authorization and/or content', call. = FALSE)
127 +
			message(method, ' failed to ', url, '. check authorization and/or content', call. = FALSE)
128 +
			return(NULL)
127 129
		}
128 130
	}
129 131
	
130 132
	if ('errors' %in% names(content(x))) {
131 -
		stop(content(x)$errors$message, call. = FALSE)
133 +
		
134 +
		if(length(errors <- content(x)$errors) == 1) {
135 +
			message(errors$message, call. = FALSE)
136 +
		} else {
137 +
			message(paste(sapply(errors, function (x) x$message), collapse = "\n"), call. = FALSE)
138 +
		}
139 +
		
140 +
		return(NULL)
132 141
	}
142 +
	
143 +
	return(x)
144 +
	}, error = function(e) {
145 +
		
146 +
		message(paste("Error when calling sciencebase,", e))
147 +
		return(NULL)
148 +
		
149 +
	})
133 150
}
134 151
135 152
#' @importFrom curl curl_version

@@ -13,8 +13,7 @@
Loading
13 13
#' @description 
14 14
#' Queries ScienceBase for items with matching datatype.
15 15
#' 
16 -
#' @examples 
17 -
#' \donttest{
16 +
#' @examples \donttest{
18 17
#' #query for items with WFS Layer data
19 18
#' query_sb_datatype('Static Map Image')
20 19
#' 
@@ -40,10 +39,10 @@
Loading
40 39
#' coupled with \code{\link{query_sb_datatype}} to query based on the type of data
41 40
#' 
42 41
#' 
43 -
#' @examples 
42 +
#' @examples \dontrun{
44 43
#' #return all datatypes (limit 50 by default)
45 44
#' sb_datatypes()
46 -
#' 
45 +
#' }
47 46
#' 
48 47
#' @export
49 48
sb_datatypes = function(limit=50, session=current_session()){

@@ -10,8 +10,7 @@
Loading
10 10
#' access is restricted due to permissions. 
11 11
#' 
12 12
#' 
13 -
#' @examples 
14 -
#' \donttest{
13 +
#' @examples \donttest{
15 14
#' # Get an item
16 15
#' item_get("4f4e4b24e4b07f02db6aea14")
17 16
#' 
@@ -35,6 +34,8 @@
Loading
35 34
	
36 35
	if(is(res, "list")) {
37 36
		if(res$status == 404) return(NULL)
37 +
	} else if(is.null(res)) {
38 +
		return(NULL)
38 39
	}
39 40
	
40 41
	return(as.sbitem(content(res)))

@@ -13,8 +13,7 @@
Loading
13 13
#'   
14 14
#' @return Returns the session object.
15 15
#'   
16 -
#' @examples
17 -
#' \dontrun{
16 +
#' @examples \dontrun{
18 17
#' # an empty call is sufficient if the session is current, 
19 18
#' # but will break if haven't been logged in before
20 19
#' session_renew()

@@ -7,12 +7,12 @@
Loading
7 7
#' 
8 8
#' 
9 9
#' 
10 -
#' @examples
10 +
#' @examples \donttest{
11 11
#' 
12 12
#' session = current_session()
13 13
#' #null unless currently authenticated
14 14
#' session
15 -
#'
15 +
#' }
16 16
#'@export
17 17
current_session = function(){
18 18
	if(session_expired(pkg.env$session)){
Files Coverage
R 50.09%
Project Totals (50 files) 50.09%

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading