Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ aaa\.Rmd*
*.o
*.so
docs
deposits.code-workspace
.vscode/settings.json
43 changes: 32 additions & 11 deletions R/tokens.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#' Retrieve a token for a specified deposits service.
#'
#' Tokens should be stored as local environment variables, optionally defined in
#' a `~/.Renviron` file, and should contain the name of the desired deposits
#' service.
#' a `~/.Renviron` file. Tokens can be defined by using the pattern
#' `SERVICE_TOKEN`, where `SERVICE` is the name of the service in capital letters
#' followed by `_SANDBOX` if the sandbox from e.g. zotero should be used. An example
#' would be `ZOTERO_SANDBOX_TOKEN` for the sandbox of zotero.
#' If these are not found, an heuristic algorythm is used to find the token name
#' which should contain the name of the desired deposits service.
#'
#' @param service Name of desired service; must be a value in the "name" column
#' of \link{deposits_services}.
#' @param sandbox If `TRUE`, retrieve token for sandbox, rather than actual API.
#' @return API token for nominated service.
#'
#'
#' @md
#' @examples
#' \dontrun{
#' token <- get_deposits_token (service = "figshare")
Expand All @@ -20,14 +25,30 @@ get_deposits_token <- function (service = NULL, sandbox = FALSE) {
checkmate::assert_character (service, len = 1L)

service <- gsub ("\\-", ".*", service)
e <- Sys.getenv ()
e <- e [grep (service, names (e), ignore.case = TRUE)]
if (length (e) != 1L) {
if (grepl ("^zenodo$", service, ignore.case = TRUE)) {
if (sandbox && any (grepl ("sandbox", names (e), ignore.case = TRUE))) {
e <- e [grep ("sandbox", names (e), ignore.case = TRUE)]
} else {
e <- e [which (!grepl ("sandbox", names (e), ignore.case = TRUE))]

# Check for fixed environmental variables ZENODO_TOKEN and ZENODO_SANDBOX_TOKEN

e <- ""
if (grepl("^zenodo$", service, ignore.case = TRUE)) {
if (sandbox) {
e <- Sys.getenv("ZENODO_SANDBOX_TOKEN")
} else {
e <- Sys.getenv("ZENODO_TOKEN")
}
} else if (grepl("^figshare$", service, ignore.case = TRUE)) {
e <- Sys.getenv("FIGSHARE_TOKEN")
}

if (e == "") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (e == "") {
if (!nzchar (e)) {

e <- Sys.getenv()
e <- e[grep(service, names(e), ignore.case = TRUE)]
if (length(e) != 1L) {
if (grepl("^zenodo$", service, ignore.case = TRUE)) {
if (sandbox && any(grepl("sandbox", names(e), ignore.case = TRUE))) {
e <- e[grep("sandbox", names(e), ignore.case = TRUE)]
} else {
e <- e[which(!grepl("sandbox", names(e), ignore.case = TRUE))]
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions man/get_deposits_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions vignettes/install-setup.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@ settings:
- [figshare/account/applications](https://figshare.com/account/applications).

It is not necessary to create or register applications on any of these
services; this package uses personal tokens only. The tokens need to be
stored as local environment variables the names of which must include the names
services; this package uses personal tokens only.

The tokens need to be
stored as local environment variables the names of which must be

- ZENOODO_TOKEN
- ZENODO_SANDBOX_TOKEN
- FIGSHARE_TOKEN

in which case the tokens are read from these environmental variables. If the respective one (responding
to the `service` and `sandbox` arguments of `deposits_service()`) is not found,
a heuristic algorytm is used to find environmental variables which include the names
of the respective services, as defined by the "name" column returned from
`deposits_service()`, as shown above. This can be done as in the following
example:
Expand Down