|
| 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 | +} |
0 commit comments