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

Un-deprecate Plumber$run(swaggerCallback=, debug=) and add Plumber$run(docs=,quiet=) #765

Merged
merged 21 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ plumber 1.0.0.9999 Development version

* OpenAPI Specification can be set using a file path. (@meztez #696)

* Added `quiet = TRUE` to `Plumber$run()` to suppress routine startup messages. (@jcheng5 #765)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* Added `quiet = TRUE` to `Plumber$run()` to suppress routine startup messages. (@jcheng5 #765)
* Added `quiet = TRUE` to `pr_run()` and `Plumber$run()` to suppress routine startup messages. (@jcheng5 #765)


### Bug fixes

* Fixed bug where `httpuv` would return a status of `500` with body `An exception occurred` if no headers were set on the response object. (#745)
Expand Down
21 changes: 14 additions & 7 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,18 @@ Plumber <- R6Class(
#' This value does not need to be explicitly assigned. To explicitly set it, see [options_plumber()].
#' @param debug Deprecated. See `$setDebug()`
#' @param swagger Deprecated. See `$setDocs(docs)` or `$setApiSpec()`
#' @param swaggerCallback Deprecated. See `$setDocsCallback()`
#' @param swaggerCallback An optional single-argument function that is
#' called back with the URL to an OpenAPI user interface when one becomes
#' ready. If missing, defaults to `$setDocsCallback()`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#' @param swaggerCallback An optional single-argument function that is
#' called back with the URL to an OpenAPI user interface when one becomes
#' ready. If missing, defaults to `$setDocsCallback()`.
#' @param swaggerCallback Deprecated. See `$setDocsCallback()`

#' @param quiet If `TRUE`, don't print routine startup messages.
schloerke marked this conversation as resolved.
Show resolved Hide resolved
#' @importFrom lifecycle deprecated
run = function(
host = '127.0.0.1',
port = getOption('plumber.port', NULL),
swagger = deprecated(),
debug = deprecated(),
swaggerCallback = deprecated()
swaggerCallback,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add in ...

Suggested change
swaggerCallback,
swaggerCallback = deprecated(),
...,

quiet = FALSE
) {

if (isTRUE(private$disable_run)) {
schloerke marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -184,14 +188,16 @@ Plumber <- R6Class(
}
}
}
if (lifecycle::is_present(swaggerCallback)) {
lifecycle::deprecate_warn("1.0.0", "run(swaggerCallback = )", "setDocsCallback(callback = )")
self$setDocsCallback(swaggerCallback)

if (missing(swaggerCallback)) {
swaggerCallback <- private$docs_callback
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (missing(swaggerCallback)) {
swaggerCallback <- private$docs_callback
if (lifecycle::is_present(swaggerCallback)) {
lifecycle::deprecate_warn("1.0.0", "run(swaggerCallback = )", "setDocsCallback(callback = )")
self$setDocsCallback(swaggerCallback)

}

port <- findPort(port)

message("Running plumber API at ", urlHost(host = host, port = port, changeHostLocation = FALSE))
if (!isTRUE(quiet)) {
message("Running plumber API at ", urlHost(host = host, port = port, changeHostLocation = FALSE))
}

# Set and restore the wd to make it appear that the proc is running local to the file's definition.
if (!is.null(private$filename)) {
Expand All @@ -205,7 +211,8 @@ Plumber <- R6Class(
host = host,
port = port,
docs_info = private$docs_info,
callback = private$docs_callback
callback = swaggerCallback,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
quiet = quiet
)
on.exit(unmount_docs(self, private$docs_info), add = TRUE)
}
Expand Down
12 changes: 10 additions & 2 deletions R/pr.R
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ pr_filter <- function(pr,
#' @param port A number or integer that indicates the server port that should
#' be listened on. Note that on most Unix-like systems including Linux and
#' Mac OS X, port numbers smaller than 1025 require root privileges.
#' @param swaggerCallback An optional single-argument function that is called
#' back with the URL to an OpenAPI user interface when one becomes ready. If
#' missing, defaults to `$setDocsCallback()`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#' @param swaggerCallback An optional single-argument function that is called
#' back with the URL to an OpenAPI user interface when one becomes ready. If
#' missing, defaults to `$setDocsCallback()`.

#' @param quiet If `TRUE`, don't print routine startup messages.
schloerke marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @examples
#' \dontrun{
Expand All @@ -488,11 +492,15 @@ pr_filter <- function(pr,
#' @export
pr_run <- function(pr,
host = '127.0.0.1',
port = getOption('plumber.port', NULL)
port = getOption('plumber.port', NULL),
swaggerCallback,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
swaggerCallback,
...,

quiet = FALSE
) {
validate_pr(pr)
schloerke marked this conversation as resolved.
Show resolved Hide resolved
pr$run(host = host,
port = port)
port = port,
swaggerCallback = swaggerCallback,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
quiet = quiet)
}


Expand Down
10 changes: 7 additions & 3 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Mount OpenAPI and Docs
#' @noRd
mount_docs <- function(pr, host, port, docs_info, callback) {
mount_docs <- function(pr, host, port, docs_info, callback, quiet = FALSE) {

# return early if not enabled
if (!isTRUE(docs_info$enabled)) {
Expand All @@ -26,14 +26,18 @@ mount_docs <- function(pr, host, port, docs_info, callback) {

# Mount Docs
if (length(registered_docs()) == 0) {
message("No visual documentation options registered. See help(register_docs).")
if (!isTRUE(quiet)) {
message("No visual documentation options registered. See help(register_docs).")
}
return()
}

if (is_docs_available(docs_info$docs)) {
docs_mount <- .globals$docs[[docs_info$docs]]$mount
docs_url <- do.call(docs_mount, c(list(pr, api_url), docs_info$args))
message("Running ", docs_info$docs, " Docs at ", docs_url, sep = "")
if (!isTRUE(quiet)) {
message("Running ", docs_info$docs, " Docs at ", docs_url, sep = "")
}
} else {
return()
}
Expand Down
9 changes: 7 additions & 2 deletions man/Plumber.Rd

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

14 changes: 13 additions & 1 deletion man/pr_run.Rd

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

11 changes: 11 additions & 0 deletions tests/testthat/test-plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,14 @@ test_that("handle method rejects forbidden arguments", {
expect_error(pr$handle("GET", "nested/path/here", function(){}, expr = function(){}),
"can not be supplied to")
})

test_that("quiet=TRUE suppresses startup messages", {
later::later(httpuv::interrupt) # Causes pr_run() to immediately exit
expect_message(pr() %>% pr_run())

later::later(httpuv::interrupt)
expect_message(pr() %>% pr_run(quiet = TRUE), NA)

later::later(httpuv::interrupt)
expect_message(pr()$run(quiet = TRUE), NA)
})