Skip to content

Commit

Permalink
Merge pull request #31 from getwilds/dev
Browse files Browse the repository at this point in the history
proofr v0.3.0 Updates
  • Loading branch information
tefirman authored Sep 16, 2024
2 parents fda4cbb + 12a49b6 commit 51a2c43
Show file tree
Hide file tree
Showing 29 changed files with 207 additions and 123 deletions.
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Package: proofr
Title: Client for the PROOF API
Version: 0.2.0.94
Version: 0.2.1.91
Authors@R:
person("Scott", "Chamberlain", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-1444-9135"))
Description: Client for the PROOF API.
URL: http://getwilds.org/proofr/ (website)
https://github.com/getwilds/proofr (devel)
URL: http://getwilds.org/proofr/, https://github.com/getwilds/proofr
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Depends: R (>= 4.1.0)
Imports:
cli,
glue,
httr
httr2
Suggests:
jsonlite,
knitr,
rmarkdown,
testthat (>= 3.0.0),
webmockr,
webmockr (>= 1.0.0),
withr
Config/testthat/edition: 3
VignetteBuilder: knitr
20 changes: 10 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
export(proof_authenticate)
export(proof_cancel)
export(proof_header)
export(proof_info)
export(proof_start)
export(proof_status)
export(proof_timeout)
importFrom(cli,cli_progress_bar)
importFrom(cli,cli_progress_update)
importFrom(httr,DELETE)
importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(httr,http_error)
importFrom(httr,http_status)
importFrom(httr,status_code)
importFrom(httr,stop_for_status)
importFrom(httr,timeout)
importFrom(httr2,req_body_json)
importFrom(httr2,req_error)
importFrom(httr2,req_headers)
importFrom(httr2,req_method)
importFrom(httr2,req_perform)
importFrom(httr2,req_timeout)
importFrom(httr2,request)
importFrom(httr2,resp_body_json)
importFrom(httr2,resp_status)
30 changes: 16 additions & 14 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ find_token <- function(token = NULL) {
#' Get header for PROOF API calls
#'
#' @export
#' @param req An `httr2` request. required
#' @param token (character) PROOF API token. optional
#' @return A `request` S3 class with the HTTP header that can be passed
#' to `httr::GET()`, `httr::POST()`, etc.
proof_header <- function(token = NULL) {
add_headers(Authorization = paste0("Bearer ", find_token(token)))
#' @return An `httr2_request` S3 class adding an HTTP header for
#' `Authorization` with the value in `token`
proof_header <- function(req, token = NULL) {
req_headers(req, Authorization = paste0("Bearer ", find_token(token)))
}

#' Authenticate with PROOF API
Expand All @@ -27,20 +28,21 @@ proof_header <- function(token = NULL) {
#' @inheritSection proof_status Timeout
#' @return A single token (character) for bearer authentication with
#' the PROOF API
#' @details We strongly recommend to not supply `password` as plain text like
#' `proof_authenticate(username = "jane", password = "mypassword")`, but rather
#' pull in your password from an environment variable stored outside of R like
#' `proof_authenticate(username = "jane", password = Sys.getenv("HUTCH_PWD"))`
proof_authenticate <- function(username, password) {
assert(username, "character")
assert(password, "character")

response <- POST(make_url("authenticate"),
body = list(
response <- request(make_url("authenticate")) |>
req_body_json(list(
username = username,
password = password
),
encode = "json",
timeout(proofr_env$timeout_sec)
)
stop_for_status(response)
parsed <- content(response, as = "parsed")
token <- parsed$token
token
)) |>
req_timeout(proofr_env$timeout_sec) |>
req_perform() |>
resp_body_json()
response$token
}
15 changes: 7 additions & 8 deletions R/cancel.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
#'
#' @export
#' @inheritParams proof_start
#' @references <https://github.com/FredHutch/proof-api#delete-cromwell-server>
#' @inheritSection proof_status Timeout
#' @return On success, a list with a single field:
#' - `message` (character)
#'
#' If run when there's no Cromwell server running, an HTTP error
proof_cancel <- function(token = NULL) {
response <- DELETE(
make_url("cromwell-server"),
proof_header(token),
timeout(proofr_env$timeout_sec)
)
stop_for_message(response)
content(response, as = "parsed")
request(make_url("cromwell-server")) |>
req_method("DELETE") |>
proof_header(token) |>
req_timeout(proofr_env$timeout_sec) |>
req_error(body = error_body) |>
req_perform() |>
resp_body_json()
}
17 changes: 5 additions & 12 deletions R/http.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
stop_for_message <- function(response) {
if (http_error(response)) {
parsed <- tryCatch(content(response), error = function(e) e)
if (inherits(parsed, "error")) stop_for_status(response)
if (!is.list(parsed)) stop_for_status(response)
msg <- glue::glue(
"{http_status(response)$reason}",
" (HTTP {status_code(response)})",
" {parsed$message}"
)
stop(msg, call. = FALSE)
}
error_body <- function(response) {
parsed <- resp_body_json(response)
glue::glue(
"Additional context: {parsed$message}"
)
}
16 changes: 16 additions & 0 deletions R/info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' Get information about PROOF server
#'
#' @export
#' @return A list with fields:
#' - `branch` (character): git branch of API
#' - `commit_sha` (character): SHA of the git commit of the API
#' - `short_commit_sha` (character): the first eight characters of `commit_sha`
#' - `commit_message` (character): commit message of API's most recent commit
#' - `tag` (character): tag of most recent commit/release version
proof_info <- function() {
request(make_url("info")) |>
req_timeout(proofr_env$timeout_sec) |>
req_error(body = error_body) |>
req_perform() |>
resp_body_json()
}
11 changes: 8 additions & 3 deletions R/proofr-package.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#' @keywords internal
#' @section Base URL for PROOF API:
#' The base URL for the PROOF API can be changed by setting the environment
#' variable `PROOF_API_BASE_URL`. It can be set for an R session or for
#' function by function use as we check that env var in each function call
#' to the API
"_PACKAGE"

## usethis namespace: start
#' @importFrom httr GET POST DELETE add_headers content
#' stop_for_status timeout http_status status_code
#' http_error
#' @importFrom httr2 request req_perform req_headers
#' req_timeout req_body_json req_method req_error
#' resp_status resp_body_json
## usethis namespace: end
NULL
20 changes: 8 additions & 12 deletions R/start.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
#' here as a string. Either pass it using [Sys.getenv()] or save your
#' token as an env var and then passing nothing to this param and we'll find
#' it
#' @references <https://github.com/FredHutch/proof-api/#post-cromwell-server>
#' @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. See the link to proof-api for more details about the
#' slurm account.
#' PROOF API
#' @inheritSection proof_status Timeout
#' @section Cromwell Server uptime:
#' The Cromwell server started by this function will run for 7 days
Expand All @@ -26,13 +24,11 @@
#' - `job_id` (character) - the job ID
#' - `info` (character) - message
proof_start <- function(slurm_account = NULL, token = NULL) {
response <- POST(
make_url("cromwell-server"),
proof_header(token),
body = list(slurm_account = slurm_account),
encode = "json",
timeout(proofr_env$timeout_sec)
)
stop_for_message(response)
content(response, as = "parsed")
request(make_url("cromwell-server")) |>
req_body_json(list(slurm_account = slurm_account)) |>
proof_header(token) |>
req_timeout(proofr_env$timeout_sec) |>
req_error(body = error_body) |>
req_perform() |>
resp_body_json()
}
14 changes: 6 additions & 8 deletions R/status.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#' @param wait (logical) if `TRUE` wait for the server to be ready to
#' interact with. if `FALSE` return immediately, then you'll want to call
#' this function again until you get the server URL
#' @references <https://github.com/FredHutch/proof-api#get-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.
Expand All @@ -25,13 +24,12 @@ proof_status <- function(wait = FALSE, token = NULL) {
}

fetch_status <- function(token = NULL) {
response <- GET(
make_url("cromwell-server"),
proof_header(token),
timeout(proofr_env$timeout_sec)
)
stop_for_message(response)
content(response, as = "parsed")
request(make_url("cromwell-server")) |>
proof_header(token) |>
req_timeout(proofr_env$timeout_sec) |>
req_error(body = error_body) |>
req_perform() |>
resp_body_json()
}

fetch_wait <- function(token) {
Expand Down
4 changes: 2 additions & 2 deletions R/timeout.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#'
#' @export
#' @param sec (integer/numeric) number of seconds after which
#' requests will timeout. default: 5 sec (5000 ms)
#' requests will timeout. default: 10 sec (10000 ms)
#' @references <https://httr.r-lib.org/reference/timeout.html>
#' @return nothing, side effect of setting the timeout for requests
proof_timeout <- function(sec = 5) {
proof_timeout <- function(sec = 10) {
assert(sec, c("integer", "numeric"))
proofr_env$timeout_sec <- sec
}
7 changes: 4 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

make_url <- function(...) {
proof_base <- Sys.getenv("PROOF_API_BASE_URL",
"https://proof-api.fredhutch.org")
proof_base <- Sys.getenv(
"PROOF_API_BASE_URL",
"https://proof-api.fredhutch.org"
)
file.path(proof_base, ...)
}

Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ To get started with `proofr`, see the [Getting Started vignette](https://getwild
## Notes

- There are no plans to submit this package to CRAN. Therefore, you should not depend on this package in any packages you have on CRAN.
- Base URL: The base URL for the PROOF API can be changed by setting the environment variable `PROOF_API_BASE_URL`. It can be set for an R session or for function by function use as we check that env var in each function call to the API.

## Bugs? Features?

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ To get started with `proofr`, see the [Getting Started vignette](https://getwild
## Notes

- There are no plans to submit this package to CRAN. Therefore, you should not depend on this package in any packages you have on CRAN.
- Base URL: The base URL for the PROOF API can be changed by setting the environment variable `PROOF_API_BASE_URL`. It can be set for an R session or for function by function use as we check that env var in each function call to the API.

## Bugs? Features?

Expand Down
6 changes: 6 additions & 0 deletions man/proof_authenticate.Rd

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

3 changes: 0 additions & 3 deletions man/proof_cancel.Rd

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

8 changes: 5 additions & 3 deletions man/proof_header.Rd

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

21 changes: 21 additions & 0 deletions man/proof_info.Rd

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

6 changes: 1 addition & 5 deletions man/proof_start.Rd

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

3 changes: 0 additions & 3 deletions man/proof_status.Rd

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

4 changes: 2 additions & 2 deletions man/proof_timeout.Rd

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

Loading

0 comments on commit 51a2c43

Please sign in to comment.