diff --git a/.gitignore b/.gitignore index 3a32cd29..01702d93 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ aaa\.Rmd* *.o *.so docs +deposits.code-workspace +.vscode/settings.json diff --git a/R/tokens.R b/R/tokens.R index 6d14277f..5bdd44ff 100644 --- a/R/tokens.R +++ b/R/tokens.R @@ -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") @@ -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 == "") { + 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))] + } } } } diff --git a/man/get_deposits_token.Rd b/man/get_deposits_token.Rd index 446e1eb9..585cfa18 100644 --- a/man/get_deposits_token.Rd +++ b/man/get_deposits_token.Rd @@ -17,8 +17,12 @@ API token for nominated service. } \description{ Tokens should be stored as local environment variables, optionally defined in -a \verb{~/.Renviron} file, and should contain the name of the desired deposits -service. +a \verb{~/.Renviron} file. Tokens can be defined by using the pattern +\code{SERVICE_TOKEN}, where \code{SERVICE} is the name of the service in capital letters +followed by \verb{_SANDBOX} if the sandbox from e.g. zotero should be used. An example +would be \code{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. } \examples{ \dontrun{ diff --git a/vignettes/install-setup.Rmd b/vignettes/install-setup.Rmd index 873bced3..8c25f21c 100644 --- a/vignettes/install-setup.Rmd +++ b/vignettes/install-setup.Rmd @@ -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: