diff --git a/DESCRIPTION b/DESCRIPTION index f94a1e2..7d62ad6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: proofr Title: Client for the PROOF API -Version: 0.0.0.91 +Version: 0.0.1.91 Authors@R: person("Scott", "Chamberlain", , "sachamber@fredhutch.org", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1444-9135")) diff --git a/NAMESPACE b/NAMESPACE index 6bd0581..6441900 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,9 +5,11 @@ export(proof_cancel) export(proof_header) export(proof_start) export(proof_status) +export(proof_timeout) importFrom(httr,DELETE) importFrom(httr,GET) importFrom(httr,POST) importFrom(httr,add_headers) importFrom(httr,content) importFrom(httr,stop_for_status) +importFrom(httr,timeout) diff --git a/R/auth.R b/R/auth.R index 1307877..944c30f 100644 --- a/R/auth.R +++ b/R/auth.R @@ -24,6 +24,7 @@ proof_header <- function(token = NULL) { #' @export #' @param username (character) HutchNet username #' @param password (character) HutchNet password +#' @inheritSection proof_status Timeout #' @return A single token (character) for bearer authentication with #' the PROOF API #' @examples @@ -31,10 +32,14 @@ proof_header <- function(token = NULL) { #' # x <- proof_authenticate("username", "password") #' # Sys.getenv("PROOF_TOKEN") proof_authenticate <- function(username, password) { - response <- POST(make_url("authenticate"), body = list( - username = username, - password = password - ), encode = "json") + response <- POST(make_url("authenticate"), + body = list( + username = username, + password = password + ), + encode = "json", + timeout(proofr_env$timeout_sec) + ) stop_for_status(response) parsed <- content(response, as = "parsed") token <- parsed$token diff --git a/R/cancel.R b/R/cancel.R index 9d79e4c..9a8d1df 100644 --- a/R/cancel.R +++ b/R/cancel.R @@ -2,10 +2,15 @@ #' #' @export #' @references +#' @inheritSection proof_status Timeout #' @return A list with a single field: #' - `message` (character) proof_cancel <- function() { - response <- DELETE(make_url("cromwell-server"), proof_header()) + response <- DELETE( + make_url("cromwell-server"), + proof_header(), + timeout(proofr_env$timeout_sec) + ) # FIXME: better error handling - surface error messages stop_for_status(response) content(response, as = "parsed") diff --git a/R/onLoad.R b/R/onLoad.R new file mode 100644 index 0000000..c8123bb --- /dev/null +++ b/R/onLoad.R @@ -0,0 +1,6 @@ +proofr_env <- new.env() # nocov start + +.onLoad <- function(libname, pkgname) { + # use the same timezone throughout the package + proofr_env$timeout_sec <<- 5 +} # nocov end diff --git a/R/proofr-package.R b/R/proofr-package.R index e4e39ca..a13fd5f 100644 --- a/R/proofr-package.R +++ b/R/proofr-package.R @@ -2,6 +2,7 @@ "_PACKAGE" ## usethis namespace: start -#' @importFrom httr GET POST DELETE add_headers content stop_for_status +#' @importFrom httr GET POST DELETE add_headers content +#' stop_for_status timeout ## usethis namespace: end NULL diff --git a/R/start.R b/R/start.R index 69e6741..9d698b2 100644 --- a/R/start.R +++ b/R/start.R @@ -7,6 +7,7 @@ #' @details Does not return PROOF/Cromwell server URL, for that you have to #' periodically call [proof_status()], or wait for the email from the #' PROOF API +#' @inheritSection proof_status Timeout #' @return A list with fields: #' - `job_id` (character) #' - `info` (character) @@ -15,7 +16,8 @@ proof_start <- function(pi_name = NULL) { make_url("cromwell-server"), proof_header(), body = list(pi_name = pi_name), - encode = "json" + encode = "json", + timeout(proofr_env$timeout_sec) ) # FIXME: better error handling - surface error messages stop_for_status(response) diff --git a/R/status.R b/R/status.R index 68a3c3d..fb0e861 100644 --- a/R/status.R +++ b/R/status.R @@ -2,6 +2,10 @@ #' #' @export #' @references +#' @section Timeout: +#' If the PROOF API is unavailable, this function will timeout after +#' 5 seconds. Contact the package maintainer if you get a timeout error. +#' See [proof_timeout()]. #' @return A list with fields: #' - `canJobStart` (logical): can a job to make a Cromwell server be started? #' `FALSE` if server already running; `TRUE` if server not running @@ -12,7 +16,11 @@ #' - `jobInfo` (list): metadata on the Cromwell server. All slots `NULL` #' if server not running proof_status <- function() { - response <- GET(make_url("cromwell-server"), proof_header()) + response <- GET( + make_url("cromwell-server"), + proof_header(), + timeout(proofr_env$timeout_sec) + ) stop_for_status(response) content(response, as = "parsed") } diff --git a/R/timeout.R b/R/timeout.R new file mode 100644 index 0000000..f898e9b --- /dev/null +++ b/R/timeout.R @@ -0,0 +1,11 @@ +#' Set the timeout for all requests to the PROOF API +#' +#' @export +#' @param sec (integer/numeric) number of seconds after which +#' requests will timeout. default: 5 sec (5000 ms) +#' @references +#' @return nothing, side effect of setting the timeout for requests +proof_timeout <- function(sec = 5) { + assert(sec, c("integer", "numeric")) + proofr_env$timeout_sec <- sec +} diff --git a/R/utils.R b/R/utils.R index 6c78b7e..6d3777b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -3,3 +3,12 @@ proof_base <- "https://proof-api.fredhutch.org" make_url <- function(...) { file.path(proof_base, ...) } + +assert <- function(x, y) { + if (!is.null(x)) { + if (!inherits(x, y)) { + stop(deparse(substitute(x)), " must be of class ", + paste0(y, collapse = ", "), call. = FALSE) + } + } +} diff --git a/man/proof_authenticate.Rd b/man/proof_authenticate.Rd index f11712d..b35d1de 100644 --- a/man/proof_authenticate.Rd +++ b/man/proof_authenticate.Rd @@ -18,6 +18,13 @@ the PROOF API \description{ Authenticate with PROOF API } +\section{Timeout}{ + +If the PROOF API is unavailable, this function will timeout after +5 seconds. Contact the package maintainer if you get a timeout error. +See \code{\link[=proof_timeout]{proof_timeout()}}. +} + \examples{ # Sys.getenv("PROOF_TOKEN") # x <- proof_authenticate("username", "password") diff --git a/man/proof_cancel.Rd b/man/proof_cancel.Rd index 0cbcf2e..39d11cc 100644 --- a/man/proof_cancel.Rd +++ b/man/proof_cancel.Rd @@ -15,6 +15,13 @@ A list with a single field: \description{ Cancel/stop PROOF Cromwell server } +\section{Timeout}{ + +If the PROOF API is unavailable, this function will timeout after +5 seconds. Contact the package maintainer if you get a timeout error. +See \code{\link[=proof_timeout]{proof_timeout()}}. +} + \references{ \url{https://github.com/FredHutch/proof-api#delete-cromwell-server} } diff --git a/man/proof_start.Rd b/man/proof_start.Rd index 844b7cd..db18133 100644 --- a/man/proof_start.Rd +++ b/man/proof_start.Rd @@ -25,6 +25,13 @@ Does not return PROOF/Cromwell server URL, for that you have to periodically call \code{\link[=proof_status]{proof_status()}}, or wait for the email from the PROOF API } +\section{Timeout}{ + +If the PROOF API is unavailable, this function will timeout after +5 seconds. Contact the package maintainer if you get a timeout error. +See \code{\link[=proof_timeout]{proof_timeout()}}. +} + \references{ \url{https://github.com/FredHutch/proof-api#post-cromwell-server} } diff --git a/man/proof_status.Rd b/man/proof_status.Rd index 63d081b..6aaddce 100644 --- a/man/proof_status.Rd +++ b/man/proof_status.Rd @@ -22,6 +22,13 @@ if server not running \description{ Get PROOF API job status - is job running, what's its URL... } +\section{Timeout}{ + +If the PROOF API is unavailable, this function will timeout after +5 seconds. Contact the package maintainer if you get a timeout error. +See \code{\link[=proof_timeout]{proof_timeout()}}. +} + \references{ \url{https://github.com/FredHutch/proof-api#get-cromwell-server} } diff --git a/man/proof_timeout.Rd b/man/proof_timeout.Rd new file mode 100644 index 0000000..5dd22ab --- /dev/null +++ b/man/proof_timeout.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/timeout.R +\name{proof_timeout} +\alias{proof_timeout} +\title{Set the timeout for all requests to the PROOF API} +\usage{ +proof_timeout(sec = 5) +} +\arguments{ +\item{sec}{(integer/numeric) number of seconds after which +requests will timeout. default: 5 sec (5000 ms)} +} +\value{ +nothing, side effect of setting the timeout for requests +} +\description{ +Set the timeout for all requests to the PROOF API +} +\references{ +\url{https://httr.r-lib.org/reference/timeout.html} +}