Skip to content

Commit 9a7e505

Browse files
authored
Merge pull request #217 from cmu-delphi/ds/api-keys
refactor+docs: set_api_key
2 parents b4b709e + 6fabeca commit 9a7e505

File tree

9 files changed

+61
-78
lines changed

9 files changed

+61
-78
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Type: Package
33
Title: Client for Delphi's 'Epidata' API
44
Version: 1.1.0.9000
55
Date: 2023-09-11
6-
Authors@R:
6+
Authors@R:
77
c(
88
person("Logan", "Brooks", email = "lcbrooks@andrew.cmu.edu", role = c("aut")),
99
person("Dmitry", "Shemetov", email = "dshemeto@andrew.cmu.edu", role = c("aut")),
@@ -37,6 +37,7 @@ Imports:
3737
openssl,
3838
readr,
3939
tibble,
40+
usethis,
4041
xml2
4142
RoxygenNote: 7.2.3
4243
Suggests:
@@ -50,7 +51,7 @@ Suggests:
5051
VignetteBuilder: knitr
5152
Language: en-US
5253
Config/testthat/edition: 3
53-
Collate:
54+
Collate:
5455
'auth.R'
5556
'avail_endpoints.R'
5657
'cache.R'

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export(pvt_norostat)
4444
export(pvt_quidel)
4545
export(pvt_sensors)
4646
export(pvt_twitter)
47-
export(set_api_key)
47+
export(save_api_key)
4848
export(set_cache)
4949
import(cachem)
5050
import(glue)

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# epidatr 1.1.0
2+
23
- renamed the mostly internal `get_auth_key` to `get_api_key`
3-
- added `set_api_key` to more easily set the option
4+
- added `save_api_key` to more easily set the option
45
- various CRAN submission related doc-fixes
56
- fixed some errors from passing "" as a key
67

R/auth.R

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,28 @@
1010
#' please consider [registering for an API
1111
#' key](https://api.delphi.cmu.edu/epidata/admin/registration_form).
1212
#'
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.
13+
#' API keys are strings read from the environment variable `DELPHI_EPIDATA_KEY`.
14+
#' We recommend setting your key with `save_api_key()`, which will modify an
15+
#' applicable `.Renviron` file, which will be read in automatically when you
16+
#' start future R sessions (see [`?Startup`][base::Startup] for details on
17+
#' `.Renviron` files). Alternatively, you can modify the environment variable at
18+
#' the command line before/while launching R, or inside an R session with
19+
#' [`Sys.setenv()`], but these will not persist across sessions.
2720
#'
2821
#' Once an API key is set, it is automatically used for all requests made by
2922
#' functions in this package.
3023
#'
3124
#' @return For `get_api_key()`, returns the current API key as a string, or
3225
#' `""` if none is set.
3326
#'
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>
27+
#' @references
28+
#' - [Delphi Epidata API Keys
29+
#' documentation](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html).
30+
#' - [Delphi Epidata API Registration
31+
#' Form](https://api.delphi.cmu.edu/epidata/admin/registration_form).
4032
#'
41-
#' Delphi Epidata API Registration Form.
42-
#' <https://api.delphi.cmu.edu/epidata/admin/registration_form>
4333
#' @export
4434
get_api_key <- function() {
45-
key <- getOption("delphi.epidata.key", default = "")
46-
if (key != "") {
47-
return(key)
48-
}
49-
5035
key <- Sys.getenv("DELPHI_EPIDATA_KEY", unset = "")
5136
if (key != "") {
5237
return(key)
@@ -55,7 +40,7 @@ get_api_key <- function() {
5540
cli::cli_warn(
5641
c(
5742
"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."
43+
"i" = "See {.help save_api_key} for details on obtaining and setting API keys."
5944
),
6045
.frequency = "regularly",
6146
.frequency_id = "delphi.epidata.key"
@@ -64,8 +49,29 @@ get_api_key <- function() {
6449
}
6550

6651
#' @rdname get_api_key
67-
#' @param key API key to use for future requests
6852
#' @export
69-
set_api_key <- function(key) {
70-
options(delphi.epidata.key = key)
53+
save_api_key <- function() {
54+
cli::cli_inform(
55+
c(
56+
"i" = "This function will open your {.code .Renviron} file in a text editor. You will need to
57+
write {.code DELPHI_EPIDATA_KEY=yourkeyhere} (without quotes) in the file and save it. If the editor
58+
does not open, you will need to edit the file manually.",
59+
"i" = "Press enter to continue."
60+
)
61+
)
62+
readline()
63+
64+
if (file.exists(usethis::proj_path(".Renviron"))) {
65+
usethis::edit_r_environ(scope = "project")
66+
67+
cat("\n\n")
68+
cli::cli_inform(
69+
c(
70+
"i" = "Your project {.code .Renviron} file has been updated. Make sure not to share this
71+
file (add it to .gitignore or equivalents)."
72+
)
73+
)
74+
} else {
75+
usethis::edit_r_environ(scope = "user")
76+
}
7177
}

README.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ API](https://cmu-delphi.github.io/delphi-epidata/). It provides a simple R inter
2828

2929
## Usage
3030

31-
You can find detailed docs here:
31+
You can find detailed docs here:
3232

3333
```{r}
3434
library(epidatr)
@@ -86,7 +86,7 @@ your key, register for a pseudo-anonymous account
8686
[here](https://api.delphi.cmu.edu/epidata/admin/registration_form) and see more
8787
discussion on the [general API
8888
website](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html). See the
89-
`set_api_key()` function documentation for details on how to use your API key.
89+
`save_api_key()` function documentation for details on how to use your API key.
9090

9191
Note that the private endpoints (i.e. those prefixed with `pvt_`) require a
9292
separate key that needs to be passed as an argument. These endpoints require

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ generate your key, register for a pseudo-anonymous account
115115
[here](https://api.delphi.cmu.edu/epidata/admin/registration_form) and
116116
see more discussion on the [general API
117117
website](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html).
118-
See the `set_api_key()` function documentation for details on how to use
119-
your API key.
118+
See the `save_api_key()` function documentation for details on how to
119+
use your API key.
120120

121121
Note that the private endpoints (i.e. those prefixed with `pvt_`)
122122
require a separate key that needs to be passed as an argument. These

man/get_api_key.Rd

Lines changed: 12 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-auth.R

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
test_that("get_api_key", {
22
withr::with_envvar(c(DELPHI_EPIDATA_KEY = "epidata_key_from_envvar"), {
3-
withr::with_options(list(delphi.epidata.key = "epidata_key_from_options"), {
4-
# option takes precedence over environment variable
5-
expect_identical(get_api_key(), "epidata_key_from_options")
6-
})
7-
})
8-
withr::with_envvar(c(DELPHI_EPIDATA_KEY = NA_character_), {
9-
withr::with_options(list(delphi.epidata.key = "epidata_key_from_options"), {
10-
# option by itself works:
11-
expect_identical(get_api_key(), "epidata_key_from_options")
12-
})
3+
expect_identical(get_api_key(), "epidata_key_from_envvar")
134
})
145
})

vignettes/epidatr.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ regions.
2323
We recommend you register for an API key. While most endpoints are available
2424
without one, there are [limits on API usage for anonymous
2525
users](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html), including
26-
a rate limit. See `set_api_key()` for details on how to obtain an API key and
26+
a rate limit. See `save_api_key()` for details on how to obtain an API key and
2727
set this package to use it.
2828

2929
## Basic Usage

0 commit comments

Comments
 (0)