|
1 | | -#' Getting the API key |
| 1 | +#' Get and set API keys |
2 | 2 | #' |
3 | | -#' @description |
4 | | -#' Get the API key from the environment variable `DELPHI_EPIDATA_KEY` or |
5 | | -#' `getOption("delphi.epidata.key")`. |
| 3 | +#' Get and set the API key used to make requests to the Epidata API. Without a |
| 4 | +#' key, requests may be subject to rate limits and other limitations. |
6 | 5 | #' |
7 | | -#' @return The API key as a string or "". |
| 6 | +#' We recommend you register for an API key. While most endpoints are available |
| 7 | +#' without one, there are [limits on API usage for anonymous |
| 8 | +#' users](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html), |
| 9 | +#' including a rate limit. If you regularly request large amounts of data, |
| 10 | +#' please consider [registering for an API |
| 11 | +#' key](https://api.delphi.cmu.edu/epidata/admin/registration_form). |
8 | 12 | #' |
| 13 | +#' API keys are strings and can be set in two ways. If the environment variable |
| 14 | +#' `DELPHI_EPIDATA_KEY` is set, it will be used automatically. Environment |
| 15 | +#' variables can be set either in the shell or by editing your `.Renviron` file, |
| 16 | +#' which will ensure the setting applies to all R sessions. See `?Startup` for a |
| 17 | +#' description of `Renviron` files and where they can be placed. |
| 18 | +#' |
| 19 | +#' Alternately, the API key can be set from within a session by using |
| 20 | +#' `set_api_key()`, which sets the R option `delphi.epidata.key` using |
| 21 | +#' `options()`. If this option is set, it is used in preference to the |
| 22 | +#' environment variable, so you may change keys within an R session. R options |
| 23 | +#' are not preserved between sessions, so `set_api_key()` must be run every time |
| 24 | +#' you open R. Alternately, you can have R set the option at startup by adding |
| 25 | +#' it to your `.Rprofile`; see `?Startup` for a description of `Rprofile` files |
| 26 | +#' and where they can be placed. |
| 27 | +#' |
| 28 | +#' Once an API key is set, it is automatically used for all requests made by |
| 29 | +#' functions in this package. |
| 30 | +#' |
| 31 | +#' @return For `get_api_key()`, returns the current API key as a string, or |
| 32 | +#' `""` if none is set. |
| 33 | +#' |
| 34 | +#' @seealso [usethis::edit_r_environ()] to automatically edit the `.Renviron` |
| 35 | +#' file; [usethis::edit_r_profile()] to automatically edit the `.Rprofile` |
| 36 | +#' file |
| 37 | +#' |
| 38 | +#' @references Delphi Epidata API Keys documentation. |
| 39 | +#' <https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html> |
| 40 | +#' |
| 41 | +#' Delphi Epidata API Registration Form. |
| 42 | +#' <https://api.delphi.cmu.edu/epidata/admin/registration_form> |
9 | 43 | #' @export |
10 | | -get_auth_key <- function() { |
11 | | - key <- Sys.getenv("DELPHI_EPIDATA_KEY", unset = "") |
| 44 | +get_api_key <- function() { |
| 45 | + key <- getOption("delphi.epidata.key", default = "") |
12 | 46 | if (key != "") { |
13 | 47 | return(key) |
14 | 48 | } |
15 | 49 |
|
16 | | - key <- getOption("delphi.epidata.key", default = "") |
| 50 | + key <- Sys.getenv("DELPHI_EPIDATA_KEY", unset = "") |
17 | 51 | if (key != "") { |
18 | 52 | return(key) |
19 | 53 | } |
20 | 54 |
|
21 | 55 | cli::cli_warn( |
22 | 56 | c( |
23 | | - "No API key found. You will be limited to non-complex queries and encounter rate limits if you proceed. |
24 | | - To avoid this, you can get your key by registering |
25 | | - at https://api.delphi.cmu.edu/epidata/admin/registration_form and then:", |
26 | | - "i" = "set the environment variable DELPHI_EPIDATA_KEY", |
27 | | - "i" = "set the option 'delphi.epidata.key'", |
28 | | - "", |
29 | | - "To save your key for later sessions (and hide it from your code), you can edit your .Renviron file with:", |
30 | | - "i" = "usethis::edit_r_environ()" |
| 57 | + "No API key found. You will be limited to non-complex queries and encounter rate limits if you proceed.", |
| 58 | + "i" = "See {.help set_api_key} for details on obtaining and setting API keys." |
31 | 59 | ), |
32 | 60 | .frequency = "regularly", |
33 | 61 | .frequency_id = "delphi.epidata.key" |
34 | 62 | ) |
35 | 63 | return("") |
36 | 64 | } |
| 65 | + |
| 66 | +#' @rdname get_api_key |
| 67 | +#' @param key API key to use for future requests |
| 68 | +#' @export |
| 69 | +set_api_key <- function(key) { |
| 70 | + options(delphi.epidata.key = key) |
| 71 | +} |
0 commit comments