Skip to content

Commit 6db8789

Browse files
authored
Merge pull request #176 from cmu-delphi/docs/options
Better document API keys
2 parents 18de016 + 94024b1 commit 6db8789

File tree

13 files changed

+147
-67
lines changed

13 files changed

+147
-67
lines changed

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export(disable_cache)
1515
export(epirange)
1616
export(fetch)
1717
export(fetch_args_list)
18-
export(get_auth_key)
18+
export(get_api_key)
1919
export(pub_covid_hosp_facility)
2020
export(pub_covid_hosp_facility_lookup)
2121
export(pub_covid_hosp_state_timeseries)
@@ -44,6 +44,7 @@ export(pvt_norostat)
4444
export(pvt_quidel)
4545
export(pvt_sensors)
4646
export(pvt_twitter)
47+
export(set_api_key)
4748
export(set_cache)
4849
import(cachem)
4950
import(glue)

R/auth.R

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,71 @@
1-
#' Getting the API key
1+
#' Get and set API keys
22
#'
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.
65
#'
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).
812
#'
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>
943
#' @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 = "")
1246
if (key != "") {
1347
return(key)
1448
}
1549

16-
key <- getOption("delphi.epidata.key", default = "")
50+
key <- Sys.getenv("DELPHI_EPIDATA_KEY", unset = "")
1751
if (key != "") {
1852
return(key)
1953
}
2054

2155
cli::cli_warn(
2256
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."
3159
),
3260
.frequency = "regularly",
3361
.frequency_id = "delphi.epidata.key"
3462
)
3563
return("")
3664
}
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+
}

R/epidatr-package.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#' @section Package options:
2+
#'
3+
#' The `delphi.epidata.key` option specifies the API key to be used when making
4+
#' requests to the Epidata API.
5+
#'
16
#' @keywords internal
27
#' @include cache.R
38
"_PACKAGE"

R/request.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ do_request <- function(url, params, timeout_seconds = 30) {
2929
query = params,
3030
terminate_on = c(400, 401, 403, 405, 414, 500),
3131
http_headers,
32-
httr::authenticate("epidata", get_auth_key()),
32+
httr::authenticate("epidata", get_api_key()),
3333
httr::timeout(timeout_seconds)
3434
)
3535
if (res$status_code == 414) {
@@ -39,7 +39,7 @@ do_request <- function(url, params, timeout_seconds = 30) {
3939
encode = "form",
4040
terminate_on = c(400, 401, 403, 405, 414, 500),
4141
http_headers,
42-
httr::authenticate("epidata", get_auth_key())
42+
httr::authenticate("epidata", get_api_key())
4343
)
4444
}
4545
res

README.Rmd

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ The Delphi API requires a (free) API key for full functionality. To generate
7979
your key, register for a pseudo-anonymous account
8080
[here](https://api.delphi.cmu.edu/epidata/admin/registration_form) and see more
8181
discussion on the [general API
82-
website](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html). The
83-
`epidatr` client will automatically look for this key in the R option
84-
`delphi.epidata.key` or in the environment variable
85-
`DELPHI_EPIDATA_KEY`. We recommend storing your key in `.Renviron` file, which R
86-
will read by default.
87-
88-
Note that for the time being, the private endpoints (i.e. those prefixed with
89-
`pvt`) will require a separate key that needs to be passed as an argument.
82+
website](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html). See the
83+
`set_api_key()` function documentation for details on how to use your API key.
84+
85+
Note that the private endpoints (i.e. those prefixed with `pvt_`) require a
86+
separate key that needs to be passed as an argument. These endpoints require
87+
specific data use agreements to access.
9088

9189
[mit-image]: https://img.shields.io/badge/License-MIT-yellow.svg
9290
[mit-url]: https://opensource.org/license/mit/

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ provides real-time access to epidemiological surveillance data for
1717
influenza, COVID-19, and other diseases for the USA at various
1818
geographical resolutions, both from official government sources such as
1919
the [Center for Disease Control
20-
(CDC)](https://www.cdc.gov/datastatistics/index.html), public sources such as [Google
21-
Trends](https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/google-symptoms.html),
20+
(CDC)](https://www.cdc.gov/datastatistics/index.html) and [Google
21+
Trends](https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/google-symptoms.html)
2222
and private partners such as
2323
[Facebook](https://delphi.cmu.edu/blog/2020/08/26/covid-19-symptom-surveys-through-facebook/)
2424
and [Change Healthcare](https://www.changehealthcare.com/). It is built
@@ -104,14 +104,12 @@ generate your key, register for a pseudo-anonymous account
104104
[here](https://api.delphi.cmu.edu/epidata/admin/registration_form) and
105105
see more discussion on the [general API
106106
website](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html).
107-
The `epidatr` client will automatically look for this key in the R
108-
option `delphi.epidata.key` or in the environment variable
109-
`DELPHI_EPIDATA_KEY`. We recommend storing your key in `.Renviron` file,
110-
which R will read by default.
111-
112-
Note that for the time being, the private endpoints (i.e. those prefixed
113-
with `pvt`) will require a separate key that needs to be passed as an
114-
argument.
107+
See the `set_api_key()` function documentation for details on how to use
108+
your API key.
109+
110+
Note that the private endpoints (i.e. those prefixed with `pvt_`)
111+
require a separate key that needs to be passed as an argument. These
112+
endpoints require specific data use agreements to access.
115113

116114
## Get updates
117115

_pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ reference:
3636
- title: Configuration and utilities
3737
desc: Set API keys and handle API data types
3838
- contents:
39-
- get_auth_key
39+
- get_api_key
4040
- avail_endpoints
4141
- epirange
4242
- timeset

man/epidatr-package.Rd

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

man/get_api_key.Rd

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

man/get_auth_key.Rd

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)