14
14
# ' x <- midden$new()
15
15
# ' x
16
16
# ' x$init(path = "rainforest3")
17
- # ' x$cache
18
17
# ' 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()
19
28
# ' # first request is a real HTTP request
20
29
# ' x$r(con$get("get", query = list(stuff = "bananas")))
21
30
# ' # following requests use the cached response
@@ -47,8 +56,6 @@ midden <- R6::R6Class(
47
56
cache_path = NULL ,
48
57
# ' @field verbose (logical) verbose or not
49
58
verbose = FALSE ,
50
- # ' @field expiry (integer) expiry time (seconds)
51
- expiry = NULL ,
52
59
53
60
# ' @description Create a new `midden` object
54
61
# ' @param verbose (logical) get messages about whats going on.
@@ -62,7 +69,11 @@ midden <- R6::R6Class(
62
69
# ' @param ... ignored
63
70
print = function (x , ... ) {
64
71
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 " )
66
77
},
67
78
# ' @description an http request code block
68
79
# ' @param ... an http request block
@@ -79,7 +90,8 @@ midden <- R6::R6Class(
79
90
res <- force(... )
80
91
stub <- private $ make_stub(res $ method , res $ url , res $ content ,
81
92
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 )
83
95
private $ m(paste0(" request found: " , checked_stub $ found ))
84
96
private $ m(paste0(" request rerun: " , checked_stub $ rerun ))
85
97
if (! checked_stub $ found || (checked_stub $ found && checked_stub $ rerun )) {
@@ -90,7 +102,7 @@ midden <- R6::R6Class(
90
102
stub <- private $ make_stub(res $ method , res $ url ,
91
103
res $ content , res $ request $ headers , res $ response_headers )
92
104
}
93
- private $ cache_stub(stub , expire )
105
+ private $ cache_stub(stub , exp )
94
106
}
95
107
private $ webmock_cleanup()
96
108
return (res )
@@ -118,14 +130,30 @@ midden <- R6::R6Class(
118
130
unlink(self $ cache $ cache_path_get(), TRUE , TRUE )
119
131
},
120
132
# ' @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`
122
136
# ' @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)
125
152
}
126
153
),
127
154
128
155
private = list (
156
+ expiry = NULL ,
129
157
webmock_init = function () {
130
158
private $ m(webmockr :: enable())
131
159
private $ m(webmockr :: webmockr_allow_net_connect())
@@ -200,6 +228,16 @@ midden <- R6::R6Class(
200
228
stop(" WEBMIDDENS_TURN_OFF must be logical" ,
201
229
call. = FALSE )
202
230
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 )
203
241
}
204
242
)
205
243
)
0 commit comments