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

proofr v0.3.0 Updates #31

Merged
merged 29 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b97e9b9
Merge pull request #17 from getwilds/main
tefirman May 31, 2024
e98ba31
rework internals to use httr2 and dev version of webmockr that suppor…
sckott Jul 12, 2024
7171223
update docs
sckott Aug 6, 2024
d8afed7
Merge branch 'main' into httr2-webmockr
sckott Aug 6, 2024
80ce107
create proof_info() function to call /info endpoint
dtenenba May 19, 2024
84a2172
add man page
dtenenba May 19, 2024
011ddff
add unit test for info endpoint
dtenenba May 19, 2024
04d3c9b
Removing logged_in_user and api_endpoint from info function
tefirman Aug 8, 2024
8a9cc17
merge from dev
sckott Aug 9, 2024
ce8f9c3
no longer need dev version of webmockr; tweak url formatting in desc …
sckott Aug 9, 2024
0d8f6ea
swap info fxn over to use httr2
sckott Aug 9, 2024
f64a28c
R 4.1.0 or greater for native pipe
sckott Aug 15, 2024
5de3e90
Merge pull request #25 from getwilds/httr2-webmockr
sckott Aug 15, 2024
974a122
improve docs for proof_authenticate, pushing users towards more secur…
sckott Aug 26, 2024
5a32a97
fix #13 remove links to proof api repo since its private - levaing re…
sckott Aug 27, 2024
e8212c3
fix #22 fix install line for rcromwell
sckott Aug 27, 2024
9074372
fix #24 document ability to set api base url via an en var in pkg lev…
sckott Aug 27, 2024
58419fa
fix #23 add advice to vignette on storing proof token in .Renviron file
sckott Aug 27, 2024
6ef2c03
Merge pull request #29 from getwilds/rcromwell-install-fix
tefirman Sep 6, 2024
6b82286
Merge pull request #30 from getwilds/env-vars
tefirman Sep 6, 2024
4724f63
Merge pull request #28 from getwilds/remove-proof-api-links
tefirman Sep 6, 2024
11337b9
Merge branch 'dev' into better-auth-docs
tefirman Sep 6, 2024
153ec51
Merge pull request #27 from getwilds/better-auth-docs
tefirman Sep 6, 2024
5175edb
Fixing linting warnings identified by GitHub Action
tefirman Sep 12, 2024
15bed3a
Merge pull request #32 from getwilds/lint-fix
tefirman Sep 12, 2024
8c2adca
Shortening comments to get rid of linting error
tefirman Sep 12, 2024
a55cdb9
update info man file
sckott Sep 12, 2024
e480242
styling fixes for utils.R and a test file
sckott Sep 12, 2024
12a49b6
change end of pipe sequence in auth fxn to avoid rhs issues in indexi…
sckott Sep 12, 2024
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
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