Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#' Initialize ScienceBase session and download files
#'
#' @param sb_id chr; ScienceBase ID
#' @param names chr; names of files to download from ScienceBase
#' @param destinations chr; write path location for downloaded files
#' @param renviron_file chr; path to .Renviron file where credentials are cached
#' @param ... additional arguments passed to `sbtools::item_file_download()`
#'
#' @return chr; path to downloaded files
#'
sb_initialize_and_download <- function(sb_id, names, destinations,
renviron_file = ".Renviron", ...) {
# Initialize ScienceBase session
sb_login_cached(renviron_file = renviron_file)
# Download SB files
sbtools::item_file_download(
sb_id = sb_id,
names = names,
destinations = destinations,
...
)
return(destinations)
}
#' Login to ScienceBase using cached credentials
#'
#' @param renviron_file chr; path to .Renviron file
#'
#' @return `TRUE` if logged in. Error if not.
#'
sb_login_cached <- function(renviron_file) {
# If logged in, return TRUE and skip the rest
if (sbtools::is_logged_in()) {
return(TRUE)
}
# Try a token refresh
tryCatch(
sbtools:::token_refresh(),
warning = function(x) {},
error = function(x) FALSE
)
if (sbtools::is_logged_in()) {
return(TRUE)
}
# If .Renviron file does not exist, re-initialize
if (!file.exists(renviron_file)) {
cli::cli_abort(c(
"Could not find the specified file: {.file {renviron_file}}.",
"i" = "Follow the instructions in {.file README.md} to initalize and cache
ScienceBase login credentials."
))
}
# Read .Renviron file
existing <- readLines(renviron_file)
sb_token_idx <- which(startsWith(existing, "sb_token="))
sb_username_idx <- which(startsWith(existing, "sb_username="))
# If SB credentials not found, throw error
if (any(length(sb_token_idx) == 0, length(sb_username_idx) == 0)) {
cli::cli_abort(c(
"Could not find the username or token in the specified file:
{.file {renviron_file}}.",
"i" = "Follow the instructions in {.file README.md} to re-initalize and
cache ScienceBase login credentials."
))
}
# Get ScienceBase credentials
sb_token <- stringr::str_remove(existing[sb_token_idx], "^sb_token=")
sb_username <- stringr::str_remove(existing[sb_username_idx], "^sb_username=")
# Initialize ScienceBase session with cached credentials
sbtools::initialize_sciencebase_session(
username = sb_username,
token_text = sb_token
)
if (sbtools::is_logged_in()) {
return(TRUE)
} else {
cli::cli_abort(c(
"Could not login to ScienceBase using cached credentials.",
"i" = "Follow the instructions in {.file README.md} to re-initalize and
cache ScienceBase login credentials."
))
}
}