~hrbrmstr/urlscan

ref: 4d8ceac4f9e24a72e4f366ae192dc24b617e8eb9 urlscan/R/api-key.R -rw-r--r-- 1.1 KiB
4d8ceac4boB Rudis v0.2.0 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#' Get or set URLSCAN_API_KEY value
#'
#' The API wrapper functions in this package all rely on a urlscan API
#' key residing in the environment variable `URLSCAN_API_KEY`. The
#' easiest way to accomplish this is to set it in the `.Renviron` file in your
#' home directory.
#'
#' @md
#' @param force Force setting a new urlscan API key for the current environment?
#' @return atomic character vector containing the urlscan API key
#' @references <https://urlscan.io/about-api/>
#' @export
urlscan_api_key <- function(force = FALSE) {
  env <- Sys.getenv("URLSCAN_API_KEY")
  if (!identical(env, "") && !force) return(env)

  if (!interactive()) {
    stop(
      "Please set env var URLSCAN_API_KEY to your urlscan API key",
      call. = FALSE
    )
  }

  message("Couldn't find env var URLSCAN_API_KEY See ?urlscan_api_key for more details.")
  message("Please enter your urlscan API key and press enter:")
  pat <- readline(": ")

  if (identical(pat, "")) {
    stop("urlscan API key entry failed", call. = FALSE)
  }

  message("Updating URLSCAN_API_KEY env var to PAT")
  Sys.setenv(urlscan_API_KEY = pat)

  pat
}