Skip to content

Commit 0b24c18

Browse files
committed
first draft of the package
1 parent d9a7f26 commit 0b24c18

22 files changed

+397
-0
lines changed

.Rbuildignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
^LICENSE\.md$
2+
^_pkgdown\.yml$
3+
^docs$
4+
^pkgdown$
5+
^README\.Rmd$
6+
^\.github$
7+
^Makefile$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docs

DESCRIPTION

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Package: proofr
2+
Title: Client for the PROOF API
3+
Version: 0.0.0.91
4+
Authors@R:
5+
person("Scott", "Chamberlain", , "[email protected]", role = c("aut", "cre"),
6+
comment = c(ORCID = "0000-0003-1444-9135"))
7+
Description: Client for the PROOF API.
8+
License: MIT + file LICENSE
9+
Encoding: UTF-8
10+
Roxygen: list(markdown = TRUE)
11+
RoxygenNote: 7.2.3
12+
Imports:
13+
httr
14+
Suggests:
15+
testthat (>= 3.0.0),
16+
withr
17+
Config/testthat/edition: 3

Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
PACKAGE := $(shell grep '^Package:' DESCRIPTION | sed -E 's/^Package:[[:space:]]+//')
2+
RSCRIPT = Rscript --no-init-file
3+
FILE_TARGET := "R/${FILE}.R"
4+
5+
install: doc build
6+
R CMD INSTALL . && rm *.tar.gz
7+
8+
build:
9+
R CMD build .
10+
11+
doc:
12+
${RSCRIPT} -e "devtools::document()"
13+
14+
eg:
15+
${RSCRIPT} -e "devtools::run_examples(run_dontrun = TRUE)"
16+
17+
check: build
18+
_R_CHECK_CRAN_INCOMING_=FALSE R CMD CHECK --as-cran --no-manual `ls -1tr ${PACKAGE}*gz | tail -n1`
19+
@rm -f `ls -1tr ${PACKAGE}*gz | tail -n1`
20+
@rm -rf ${PACKAGE}.Rcheck
21+
22+
vign_getting_started:
23+
cd vignettes;\
24+
${RSCRIPT} -e "Sys.setenv(NOT_CRAN='true'); knitr::knit('${PACKAGE}.Rmd.og', output = '${PACKAGE}.Rmd')";\
25+
cd ..
26+
27+
test:
28+
${RSCRIPT} -e "devtools::test()"
29+
30+
readme:
31+
${RSCRIPT} -e "knitr::knit('README.Rmd')"
32+
33+
lint_package:
34+
${RSCRIPT} -e "lintr::lint_package()"
35+
36+
# use: `make style_file FILE=stuff.R`
37+
# ("R/" is prepended); accepts 1 file only
38+
style_file:
39+
${RSCRIPT} -e 'styler::style_file(${FILE_TARGET})'
40+
41+
style_package:
42+
${RSCRIPT} -e "styler::style_pkg()"

NAMESPACE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(proof_authenticate)
4+
export(proof_job_cancel)
5+
export(proof_job_start)
6+
export(proof_job_status)
7+
importFrom(httr,DELETE)
8+
importFrom(httr,GET)
9+
importFrom(httr,POST)
10+
importFrom(httr,add_headers)
11+
importFrom(httr,content)
12+
importFrom(httr,stop_for_status)

R/auth.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
find_token <- function(token = NULL) {
2+
if (!is.null(token)) {
3+
return(token)
4+
}
5+
token <- Sys.getenv("PROOF_TOKEN")
6+
if (identical(token, "")) {
7+
stop("token not found - see ?proof_authenticate")
8+
}
9+
token
10+
}
11+
12+
#' Get header for PROOF API calls
13+
#'
14+
#' Utility method to get header for PROOF API calls
15+
#'
16+
#' @keywords internal
17+
#' @param token PROOF API token
18+
#' @return A `request` S3 class with the HTTP header that can be passed
19+
#' to `httr::GET()`, `httr::POST()`, etc.
20+
proof_header <- function(token = NULL) {
21+
add_headers(Authorization = paste0("Bearer ", find_token(token)))
22+
}
23+
24+
#' Authenticate with PROOF API
25+
#'
26+
#' Authenticates with HutchNet credentials, returns PROOF API token
27+
#'
28+
#' @export
29+
#' @param username (character) HutchNet username
30+
#' @param password (character) HutchNet password
31+
#' @return A single token (character) for bearer authentication with
32+
#' the PROOF API
33+
#' @examples
34+
#' # Sys.getenv("PROOF_TOKEN")
35+
#' # x <- proof_authenticate()
36+
#' # Sys.getenv("PROOF_TOKEN")
37+
proof_authenticate <- function(username, password) {
38+
response <- POST(make_url("authenticate"), body = list(
39+
username = username,
40+
password = password
41+
), encode = "json")
42+
stop_for_status(response)
43+
parsed <- content(response, as = "parsed")
44+
token <- parsed$token
45+
Sys.setenv(PROOF_TOKEN = token)
46+
token
47+
}

R/cancel.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#' Delete PROOF Cromwell server
2+
#'
3+
#' @export
4+
#' @references <https://github.com/FredHutch/proof-api#delete-cromwell-server>
5+
#' @details Does not return PROOF/Cromwell server URL, for that you have to
6+
#' periodically call [proof_job_status()], or wait for the email from the
7+
#' PROOF API
8+
#' @return A list with fields `job_id` and `info`
9+
proof_job_cancel <- function() {
10+
response <- DELETE(make_url("cromwell-server"), proof_header())
11+
# FIXME: better error handling - surface error messages
12+
stop_for_status(response)
13+
content(response, as = "parsed")
14+
}

R/job_status.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#' Get PROOF API job status - is job running, what's its URL...
2+
#'
3+
#' @export
4+
#' @references <https://github.com/FredHutch/proof-api#get-cromwell-server>
5+
#' @return A list with slots:
6+
#' - `canJobStart`
7+
#' - `jobStatus`
8+
#' - `cromwellUrl`
9+
proof_job_status <- function() {
10+
response <- GET(make_url("cromwell-server"), proof_header())
11+
stop_for_status(response)
12+
content(response, as = "parsed")
13+
}

R/proofr-package.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
#' @importFrom httr GET POST DELETE add_headers content stop_for_status
6+
## usethis namespace: end
7+
NULL

R/start.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' Start PROOF Cromwell server
2+
#'
3+
#' @export
4+
#' @param pi_name (character) PI name in the form last_f; only needed if user
5+
#' is in more than one SLURM account
6+
#' @references <https://github.com/FredHutch/proof-api#post-cromwell-server>
7+
#' @details Does not return PROOF/Cromwell server URL, for that you have to
8+
#' periodically call [proof_job_status()], or wait for the email from the
9+
#' PROOF API
10+
#' @return A list with fields `job_id` and `info`
11+
proof_job_start <- function(pi_name = NULL) {
12+
response <- POST(
13+
make_url("cromwell-server"),
14+
proof_header(),
15+
body = list(pi_name = pi_name),
16+
encode = "json"
17+
)
18+
# FIXME: better error handling - surface error messages
19+
stop_for_status(response)
20+
content(response, as = "parsed")
21+
}

0 commit comments

Comments
 (0)