Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to sys env var if R option doesn't exist #934

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export(do_remove_forward)
export(endpoint_serializer)
export(forward)
export(getCharacterSet)
export(getOption_env_default)
export(get_character_set)
export(get_option_or_env)
export(include_file)
export(include_html)
export(include_md)
Expand Down
15 changes: 7 additions & 8 deletions R/options_plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,21 @@ options_plumber <- function(
}

#' Get an option value, alternatively look in environment for value
#' @rdname options_plumber
#' @describeIn options_plumber DOCS
schloerke marked this conversation as resolved.
Show resolved Hide resolved
#' @inheritParams base::options
#' @export
getOption_env_default <- function(x, default = NULL) {
get_option_or_env <- function(x, default = NULL) {

getOption(x, default = {
env_name <- toupper(chartr(".", "_", x))
res <- Sys.getenv(env_name)
if (res == "") {
default
} else {
if (res %in% c("TRUE", "FALSE")) {
return(as.logical(res))
}
res
return(default)
}
if (res %in% c("TRUE", "FALSE")) {
return(as.logical(res))
}
res
})

}
14 changes: 7 additions & 7 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Plumber <- R6Class(
#' @importFrom rlang missing_arg
run = function(
host = '127.0.0.1',
port = getOption_env_default('plumber.port', NULL),
port = get_option_or_env('plumber.port', NULL),
swagger = deprecated(),
debug = missing_arg(),
swaggerCallback = missing_arg(),
Expand Down Expand Up @@ -211,15 +211,15 @@ Plumber <- R6Class(
port <- findPort(port)

# Delay setting max size option. It could be set in `plumber.R`, which is after initialization
private$maxSize <- getOption_env_default('plumber.maxRequestSize', 0) #0 Unlimited
private$maxSize <- get_option_or_env('plumber.maxRequestSize', 0) #0 Unlimited

# Delay the setting of swaggerCallback as long as possible.
# An option could be set in `plumber.R`, which is after initialization
# Order: Run method parameter, internally set value, option, fallback option, NULL
swaggerCallback <-
rlang::maybe_missing(swaggerCallback,
rlang::maybe_missing(private$docs_callback,
getOption_env_default('plumber.docs.callback', getOption_env_default('plumber.swagger.url', NULL))
get_option_or_env('plumber.docs.callback', get_option_or_env('plumber.swagger.url', NULL))
)
)

Expand Down Expand Up @@ -784,7 +784,7 @@ Plumber <- R6Class(
# No endpoint could handle this request. 404
notFoundStep <- function(...) {

if (isTRUE(getOption_env_default("plumber.trailingSlash", FALSE))) {
if (isTRUE(get_option_or_env("plumber.trailingSlash", FALSE))) {
# Redirect to the slash route, if it exists
path <- req$PATH_INFO
# If the path does not end in a slash,
Expand Down Expand Up @@ -813,7 +813,7 @@ Plumber <- R6Class(
# No trailing-slash route exists...
# Try allowed verbs

if (isTRUE(getOption_env_default("plumber.methodNotAllowed", TRUE))) {
if (isTRUE(get_option_or_env("plumber.methodNotAllowed", TRUE))) {
# Notify about allowed verbs
if (is_405(req$pr, req$PATH_INFO, req$REQUEST_METHOD)) {
res$status <- 405L
Expand Down Expand Up @@ -933,7 +933,7 @@ Plumber <- R6Class(
#' If using [options_plumber()], the value must be set before initializing your Plumber router.
#' @param ... Arguments for the visual documentation. See each visual documentation package for further details.
setDocs = function(
docs = getOption_env_default("plumber.docs", TRUE),
docs = get_option_or_env("plumber.docs", TRUE),
...
) {
private$docs_info <- upgrade_docs_parameter(docs, ...)
Expand All @@ -948,7 +948,7 @@ Plumber <- R6Class(
#' See also: [pr_set_docs_callback()]
#' @param callback a callback function for taking action on the docs url. (Also accepts `NULL` values to disable the `callback`.)
setDocsCallback = function(
callback = getOption_env_default('plumber.docs.callback', NULL)
callback = get_option_or_env('plumber.docs.callback', NULL)
) {
# Use callback when defined
if (!length(callback) || !is.function(callback)) {
Expand Down
2 changes: 1 addition & 1 deletion R/pr.R
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ pr_filter <- function(pr,
#' @export
pr_run <- function(pr,
host = '127.0.0.1',
port = getOption_env_default('plumber.port', NULL),
port = get_option_or_env('plumber.port', NULL),
...,
debug = missing_arg(),
docs = missing_arg(),
Expand Down
4 changes: 2 additions & 2 deletions R/pr_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pr_set_debug <- function(pr, debug = interactive()) {
#' }
pr_set_docs <- function(
pr,
docs = getOption_env_default("plumber.docs", TRUE),
docs = get_option_or_env("plumber.docs", TRUE),
...
) {
validate_pr(pr)
Expand Down Expand Up @@ -206,7 +206,7 @@ pr_set_docs <- function(
#' }
pr_set_docs_callback <- function(
pr,
callback = getOption_env_default('plumber.docs.callback', NULL)
callback = get_option_or_env('plumber.docs.callback', NULL)
) {
validate_pr(pr)
pr$setDocsCallback(callback = callback)
Expand Down
2 changes: 1 addition & 1 deletion R/shared-secret-filter.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @noRd
sharedSecretFilter <- function(req, res){
secret <- getOption_env_default("plumber.sharedSecret", NULL)
secret <- get_option_or_env("plumber.sharedSecret", NULL)
if (!is.null(secret)){
supplied <- req$HTTP_PLUMBER_SHARED_SECRET
if (!identical(supplied, secret)){
Expand Down
16 changes: 8 additions & 8 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ mount_docs <- function(pr, host, port, docs_info, callback, quiet = FALSE) {
}

# Build api url
api_url <- getOption_env_default(
api_url <- get_option_or_env(
"plumber.apiURL",
urlHost(
scheme = getOption_env_default("plumber.apiScheme", "http"),
host = getOption_env_default("plumber.apiHost", host),
port = getOption_env_default("plumber.apiPort", port),
path = getOption_env_default("plumber.apiPath", ""),
scheme = get_option_or_env("plumber.apiScheme", "http"),
host = get_option_or_env("plumber.apiHost", host),
port = get_option_or_env("plumber.apiPort", port),
path = get_option_or_env("plumber.apiPath", ""),
changeHostLocation = TRUE
)
)
Expand Down Expand Up @@ -91,8 +91,8 @@ mount_openapi <- function(pr, api_url) {
openapi_fun <- function(req) {
# use the HTTP_REFERER so RSC can find the Docs location to ask
## (can't directly ask for 127.0.0.1)
if (is.null(getOption_env_default("plumber.apiURL")) &&
is.null(getOption_env_default("plumber.apiHost"))) {
if (is.null(get_option_or_env("plumber.apiURL")) &&
is.null(get_option_or_env("plumber.apiHost"))) {
if (is.null(req$HTTP_REFERER)) {
# Prevent leaking host and port if option is not set
api_url <- character(1)
Expand Down Expand Up @@ -256,7 +256,7 @@ registered_docs <- function() {


swagger_redirects <- function() {
if (!isTRUE(getOption_env_default("plumber.legacyRedirects", TRUE))) {
if (!isTRUE(get_option_or_env("plumber.legacyRedirects", TRUE))) {
return(list())
}

Expand Down
6 changes: 3 additions & 3 deletions man/Plumber.Rd

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

9 changes: 7 additions & 2 deletions man/options_plumber.Rd

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

2 changes: 1 addition & 1 deletion man/pr_run.Rd

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

2 changes: 1 addition & 1 deletion man/pr_set_docs.Rd

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

2 changes: 1 addition & 1 deletion man/pr_set_docs_callback.Rd

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

16 changes: 8 additions & 8 deletions tests/testthat/test-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ context("Options")
test_that("Options set and get", {
with_options(list(plumber.port = NULL), {
options_plumber(port = FALSE)
expect_false(getOption_env_default("plumber.port"))
expect_false(get_option_or_env("plumber.port"))
options_plumber(port = NULL)
expect_null(getOption_env_default("plumber.port"))
expect_null(get_option_or_env("plumber.port"))
})
})

test_that("Options set and get", {
with_options(list(plumber.port = NULL), {
Sys.setenv("PLUMBER_PORT" = FALSE)
expect_false(getOption_env_default("plumber.port"))
expect_false(get_option_or_env("plumber.port"))
Sys.unsetenv("PLUMBER_PORT")
expect_null(getOption_env_default("plumber.port"))
expect_null(get_option_or_env("plumber.port"))
})
})

Expand Down Expand Up @@ -63,7 +63,7 @@ test_that("all options used are `options_plumber()` parameters", {
test_that("Legacy swagger redirect can be disabled", {
with_options(
list(
plumber.legacyRedirets = getOption_env_default("plumber.legacyRedirects")
plumber.legacyRedirets = get_option_or_env("plumber.legacyRedirects")
), {
options_plumber(legacyRedirects = TRUE)
redirects <- swagger_redirects()
Expand All @@ -79,12 +79,12 @@ test_that("Legacy swagger redirect can be disabled", {
test_that("docs.callback sync plumber.swagger.url", {
with_options(
list(
plumber.swagger.url = getOption_env_default("plumber.swagger.url"),
plumber.docs.callback = getOption_env_default("plumber.docs.callback")
plumber.swagger.url = get_option_or_env("plumber.swagger.url"),
plumber.docs.callback = get_option_or_env("plumber.docs.callback")
), {
options("plumber.swagger.url" = function(api_url) {cat(api_url)})
opt <- options_plumber(docs.callback = NULL)
expect_null(getOption_env_default("plumber.swagger.url"))
expect_null(get_option_or_env("plumber.swagger.url"))
expect_null(opt$plumber.docs.callback)
}
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-plumber-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ test_that("`swaggerCallback` can be set by option after the pr is created", {

with_options(
list(
plumber.swagger.url = getOption_env_default("plumber.swagger.url"),
plumber.docs.callback = getOption_env_default("plumber.docs.callback")
plumber.swagger.url = get_option_or_env("plumber.swagger.url"),
plumber.docs.callback = get_option_or_env("plumber.docs.callback")
),
{
# set option after init
Expand Down