Skip to content

Commit

Permalink
Merge pull request #24 from R-ArcGIS/unset
Browse files Browse the repository at this point in the history
Provide unset token functionality
  • Loading branch information
JosiahParry authored Feb 16, 2024
2 parents 53680c9 + 18dce51 commit 7ec0505
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export(parse_esri_json)
export(refresh_token)
export(remote_ptype_tbl)
export(set_arc_token)
export(unset_arc_token)
export(validate_crs)
export(validate_or_refresh_token)
importFrom(Rcpp,sourceCpp)
Expand Down
21 changes: 19 additions & 2 deletions R/arc-token.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ token_env <- rlang::env()
#' These functions are used to set, fetch, and check authorization tokens.
#'
#' @param token for `arc_token()`, the name of a token to fetch. For `set_arc_token()`,
#' it is an `httr2_token` that will be set.
#' it is an `httr2_token` that will be set. For `unset_arc_token()`, a character
#' vector of token names to be unset.
#' @param ... named arguments to set `httr2_token`. Must be valid names and must be an `httr2_token`.
#' @inheritParams cli::cli_abort
#' @importFrom cli cli_abort
Expand All @@ -20,7 +21,8 @@ token_env <- rlang::env()
#' However, they can be any valid character scalar. `set_arc_token()` will create
#' store a token with the name `ARCGIS_TOKEN`. However, you can alternatively
#' set the tokens by name using a key-value pair. The key is what you would pass
#' to `arc_token()` to fetch the `httr2_token` object.
#' to `arc_token()` to fetch the `httr2_token` object. To remove a token that has
#' been set, use `unset_arc_token()`.
#'
#' `obj_check_token()` is a developer oriented function that can be used to check
#' if an object is indeed an `httr2_token`. To check if a token has expired,
Expand Down Expand Up @@ -53,6 +55,10 @@ token_env <- rlang::env()
#' # fetch token by name
#' arc_token("org_a")
#' arc_token("org_b")
#'
#' # unset tokens
#' unset_arc_token()
#' unset_arc_token(c("org_a", "org_b"))
arc_token <- function(token = "ARCGIS_TOKEN") {
# returns NULL if not found
token_env[[token]]
Expand Down Expand Up @@ -103,6 +109,17 @@ set_arc_token <- function(token, ...) {

}

#' @export
unset_arc_token <- function(token = NULL) {
if (is.null(token)) {
token <- "ARCGIS_TOKEN"
rlang::env_unbind(token_env, token)
} else {
rlang::env_unbind(token_env, token)
}
cli::cli_alert_success("Token{?s} {.var {token}} {?has/have} been unset.")
}

#' @export
#' @rdname token
obj_check_token <- function(token, call = rlang::caller_env()) {
Expand Down
6 changes: 4 additions & 2 deletions man/token.Rd

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

0 comments on commit 7ec0505

Please sign in to comment.