Skip to content

Commit 0eeaa4e

Browse files
committed
feat: Added vcr to Suggests and in unit tests for openFDA, as well as use in openFDA() roxygen docs
1 parent 45f92ce commit 0eeaa4e

File tree

11 files changed

+5778
-182
lines changed

11 files changed

+5778
-182
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto
2+
tests/fixtures/**/* -diff

DESCRIPTION

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ License: GPL (>= 3)
1515
URL: https://github.com/simpar1471/openFDA,
1616
https://simpar1471.github.io/openFDA/
1717
BugReports: https://github.com/simpar1471/openFDA/issues
18-
Depends:
18+
Depends:
1919
R (>= 4.1.0)
20-
Imports:
20+
Imports:
2121
cli,
2222
httr2,
2323
checkmate,
@@ -27,11 +27,12 @@ Imports:
2727
prettyunits,
2828
keyring
2929
Config/Needs/website: rmarkdown
30-
Suggests:
30+
Suggests:
3131
knitr,
3232
rmarkdown,
33+
stringr,
3334
testthat (>= 3.0.0),
34-
stringr
35+
vcr (>= 0.6.0)
3536
Config/testthat/edition: 3
3637
Config/testthat/start-first: options
3738
Encoding: UTF-8

R/api_key.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#' the number of requests on a per-IP address basis, as well as on a per-key
1212
#' basis).
1313
#' @examples
14+
#' \dontshow{
15+
#' try(prior_api_key <- get_api_key(), silent = TRUE)
16+
#' }
1417
#' # Set your openFDA API key with `set_api_key()`
1518
#' api_key <- "example_api_key"
1619
#' set_api_key(api_key)
@@ -20,6 +23,9 @@
2023
#'
2124
#' # An error will be thrown you try to set your API key to an empty string.
2225
#' try(set_api_key(""))
26+
#' \dontshow{
27+
#' try(set_api_key(api_key = prior_api_key), silent = TRUE)
28+
#' }
2329
#' @section Setting an openFDA API key:
2430
#' API keys are the sort of thing that should be stored securely. For that
2531
#' reason, `set_api_key()` and `get_api_key()` utilise the

R/openFDA.R

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,28 +134,27 @@
134134
#' [httr2](https://httr2.r-lib.org/) response object without printing a
135135
#' warning.
136136
#' @examples
137-
#' if (httr2::secret_has_key("OPENFDA_KEY")) {
138-
#' set_api_key(httr2::secret_decrypt(
139-
#' "TEaDtqdFMq9_Montij5p9IY6T57IyqkbF8IYFVOpk-ttxotFUNdJSxgccAnkq4nQhplaf-r3deQ",
140-
#' "OPENFDA_KEY"
141-
#' ))
142-
#'
143-
#' resp <- openFDA(search = "openfda.manufacturer_name:gilead*",
144-
#' limit = 2,
145-
#' skip = 10,
146-
#' paging = "never",
147-
#' paging_verbosity = "quiet")
148-
#'
149-
#' # The function returns an `httr2` object
150-
#' print(resp)
137+
#' \dontshow{
138+
#' vcr::insert_example_cassette("openFDA_examples", package = "openFDA")
151139
#' }
140+
#' resp <- openFDA(search = "openfda.manufacturer_name:gilead*",
141+
#' limit = 2,
142+
#' skip = 10,
143+
#' paging = "never",
144+
#' paging_verbosity = "quiet")
145+
#'
146+
#' # The function returns an `httr2` object
147+
#' print(resp)
152148
#'
153149
#' # Bad inputs will cause informative errors - here, a bad API key is supplied
154150
#' try(
155151
#' openFDA(search = "openfda.manufacturer_name:gilead*",
156152
#' api_key = "BAD_API_KEY",
157153
#' limit = 1)
158154
#' )
155+
#' \dontshow{
156+
#' vcr::eject_cassette()
157+
#' }
159158
#' @references
160159
#' Kass-Hout TA, Xu Z, Mohebbi M, Nelsen H, Baker A, LEvine J, Johansen E,
161160
#' Bright RA. **OpenFDA: an innovative platform providing access to a wealth of
@@ -379,7 +378,7 @@ openFDA_err_400_msg <- function(resp) {
379378
#' @rdname openFDA_http_errors
380379
#' @noRd
381380
openFDA_err_403_msg <- function(resp) {
382-
api_key <- httr2::url_parse(resp$url)$query$api_key
381+
api_key <- rlang::env_get(rlang::caller_env(7), nm = "api_key")
383382
c("The openFDA API returned a 403 error.",
384383
"!" = "This usually means that an invalid {.var api_key} was used.",
385384
"i" = cli::format_inline("Your API key was {.val {api_key}}."))

0 commit comments

Comments
 (0)