Skip to content

Commit 4c20a8f

Browse files
committed
#5 add WEBMIDDENS_EXPIRY_SEC env var
1 parent 2d80add commit 4c20a8f

File tree

6 files changed

+120
-20
lines changed

6 files changed

+120
-20
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: webmiddens
22
Title: Cache Mocked 'HTTP' Requests
33
Description: Cache mocked 'HTTP' requests, leveraging 'webmockr'
44
for the 'HTTP' request matching.
5-
Version: 0.0.2.91
5+
Version: 0.0.2.94
66
Authors@R: c(
77
person("Scott", "Chamberlain", role = c("aut", "cre"),
88
email = "[email protected]",

R/midden.R

+47-9
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414
#' x <- midden$new()
1515
#' x
1616
#' x$init(path = "rainforest3")
17-
#' x$cache
1817
#' x
18+
#' x$cache
19+
#' x$expire()
20+
#' x$expire(5)
21+
#' x$expire()
22+
#' x$expire(reset = TRUE)
23+
#' x$expire()
24+
#' Sys.setenv(WEBMIDDENS_EXPIRY_SEC = 35)
25+
#' x$expire()
26+
#' x$expire(reset = TRUE)
27+
#' x$expire()
1928
#' # first request is a real HTTP request
2029
#' x$r(con$get("get", query = list(stuff = "bananas")))
2130
#' # following requests use the cached response
@@ -47,8 +56,6 @@ midden <- R6::R6Class(
4756
cache_path = NULL,
4857
#' @field verbose (logical) verbose or not
4958
verbose = FALSE,
50-
#' @field expiry (integer) expiry time (seconds)
51-
expiry = NULL,
5259

5360
#' @description Create a new `midden` object
5461
#' @param verbose (logical) get messages about whats going on.
@@ -62,7 +69,11 @@ midden <- R6::R6Class(
6269
#' @param ... ignored
6370
print = function(x, ...) {
6471
cat("<midden> ", sep = "\n")
65-
cat(paste0(" path: ", self$cache_path), sep = "\n")
72+
pth <- if (inherits(self$cache, "HoardClient"))
73+
self$cache$cache_path_get()
74+
else
75+
self$cache_path
76+
cat(paste0(" path: ", pth), sep = "\n")
6677
},
6778
#' @description an http request code block
6879
#' @param ... an http request block
@@ -79,7 +90,8 @@ midden <- R6::R6Class(
7990
res <- force(...)
8091
stub <- private$make_stub(res$method, res$url, res$content,
8192
res$request$headers, res$response_headers)
82-
checked_stub <- private$in_stored_stubs(stub, expire)
93+
exp <- private$set_expiry(expire)
94+
checked_stub <- private$in_stored_stubs(stub, exp)
8395
private$m(paste0("request found: ", checked_stub$found))
8496
private$m(paste0("request rerun: ", checked_stub$rerun))
8597
if (!checked_stub$found || (checked_stub$found && checked_stub$rerun)) {
@@ -90,7 +102,7 @@ midden <- R6::R6Class(
90102
stub <- private$make_stub(res$method, res$url,
91103
res$content, res$request$headers, res$response_headers)
92104
}
93-
private$cache_stub(stub, expire)
105+
private$cache_stub(stub, exp)
94106
}
95107
private$webmock_cleanup()
96108
return(res)
@@ -118,14 +130,30 @@ midden <- R6::R6Class(
118130
unlink(self$cache$cache_path_get(), TRUE, TRUE)
119131
},
120132
#' @description set an expiration time
121-
#' @param time (integer) seconds to expire
133+
#' @param expire (integer) seconds to expire - OR, set via the
134+
#' environment variable `WEBMIDDENS_EXPIRY_SEC`
135+
#' @param reset (logical) reset to `NULL`? default: `FALSE`
122136
#' @return NULL
123-
expire = function(time) {
124-
self$expiry <- time
137+
#' @examples
138+
#' z <- midden$new()
139+
#' z$expire(35) # set to expire all requests in 35 seconds
140+
#' # or set by env var
141+
#' Sys.setenv(WEBMIDDENS_EXPIRY_SEC = 35)
142+
expire = function(expire = NULL, reset = FALSE) {
143+
assert(reset, "logical")
144+
if (reset) {
145+
private$expiry <- NULL
146+
Sys.setenv("WEBMIDDENS_EXPIRY_SEC" = "")
147+
return(NULL)
148+
}
149+
private$set_expiry(expire)
150+
# if (!is.null(time)) private$expiry <- time
151+
# return(private$expiry)
125152
}
126153
),
127154

128155
private = list(
156+
expiry = NULL,
129157
webmock_init = function() {
130158
private$m(webmockr::enable())
131159
private$m(webmockr::webmockr_allow_net_connect())
@@ -200,6 +228,16 @@ midden <- R6::R6Class(
200228
stop("WEBMIDDENS_TURN_OFF must be logical",
201229
call. = FALSE)
202230
assert(x, "logical")
231+
},
232+
set_expiry = function(expire = NULL) {
233+
expire <-
234+
expire %||% private$expiry %||%
235+
Sys.getenv("WEBMIDDENS_EXPIRY_SEC") %||% NULL
236+
if (!is.null(expire))
237+
expire <- tryCatch(as.numeric(expire), warning = function(w) w)
238+
assert(expire, c("numeric", "integer"))
239+
private$expiry <- expire
240+
return(private$expiry)
203241
}
204242
)
205243
)

R/zzz.R

+6
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ assert <- function(x, y) {
99
}
1010
return(x)
1111
}
12+
13+
`%||%` <- function(x, y) {
14+
if (
15+
is.null(x) || length(x) == 0 || all(nchar(x) == 0) || all(is.na(x))
16+
) y else x
17+
}

man/midden.Rd

+35-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-env_vars.R

+29-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test_that("env vars with midden class", {
4141
Sys.setenv("WEBMIDDENS_TURN_OFF" = "")
4242
})
4343

44-
test_that("env vars fail as expected", {
44+
test_that("env vars fail as expected: WEBMIDDENS_TURN_OFF", {
4545
x <- midden$new()
4646
x$init(path = "forest31", type = 'tempdir')
4747
library(crul)
@@ -59,10 +59,38 @@ test_that("env vars fail as expected", {
5959
Sys.setenv("WEBMIDDENS_TURN_OFF" = "asdfasdfs")
6060
expect_error(x$r(con$get("get", query = list(fruit = "apples"))))
6161

62+
# back to an allowed value
6263
Sys.setenv("WEBMIDDENS_TURN_OFF" = "true")
6364
expect_is(x$r(con$get("get", query = list(fruit = "apples"))),
6465
"HttpResponse")
6566

6667
# reset to default
6768
Sys.setenv("WEBMIDDENS_TURN_OFF" = "")
6869
})
70+
71+
test_that("env vars fail as expected: WEBMIDDENS_EXPIRY_SEC", {
72+
x <- midden$new()
73+
x$init(path = "forest33", type = 'tempdir')
74+
library(crul)
75+
con <- crul::HttpClient$new("https://httpbin.org")
76+
77+
# env var not set
78+
Sys.setenv("WEBMIDDENS_EXPIRY_SEC" = "")
79+
expect_is(x$r(con$get("get", query = list(fruit = "apples"))),
80+
"HttpResponse")
81+
82+
# env var set to something not allowed
83+
Sys.setenv("WEBMIDDENS_EXPIRY_SEC" = "foobar")
84+
expect_error(x$r(con$get("get", query = list(fruit = "apples"))))
85+
86+
Sys.setenv("WEBMIDDENS_EXPIRY_SEC" = TRUE)
87+
expect_error(x$r(con$get("get", query = list(fruit = "apples"))))
88+
89+
# back to an allowed value
90+
Sys.setenv("WEBMIDDENS_EXPIRY_SEC" = 3)
91+
expect_is(x$r(con$get("get", query = list(fruit = "apples"))),
92+
"HttpResponse")
93+
94+
# reset to default
95+
Sys.unsetenv("WEBMIDDENS_EXPIRY_SEC")
96+
})

tests/testthat/test-midden.R

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ test_that("midden - basic structure, before calling init", {
44
mid <- midden$new()
55
expect_is(mid, "midden")
66
expect_is(mid, "R6")
7-
expect_null(mid$expiry)
87
expect_null(mid$cach_path)
98
expect_null(mid$cache)
109
expect_false(mid$verbose)
@@ -19,7 +18,6 @@ test_that("midden - structure after calling init", {
1918
mid$init(path = "rainforest10", type = 'tempdir')
2019
expect_is(mid, "midden")
2120
expect_is(mid, "R6")
22-
expect_null(mid$expiry)
2321
expect_null(mid$cach_path)
2422
expect_is(mid$cache, "HoardClient")
2523
expect_true(mid$verbose)
@@ -74,9 +72,9 @@ context('midden: expire method')
7472
test_that("midden - expire function", {
7573
library(crul)
7674
x <- midden$new()
77-
expect_null(x$expiry)
75+
expect_null(x$expire())
7876
x$expire(5)
79-
expect_equal(x$expiry, 5)
77+
expect_equal(x$expire(), 5)
8078
})
8179

8280
context('midden: fails well')

0 commit comments

Comments
 (0)