knitr::opts_chunk$set( comment = "#>", collapse = TRUE, warning = FALSE )
simple caching of HTTP requests/responses, hooking into webmockr (https://github.com/ropensci/webmockr) for the HTTP request matching
A midden is a debris pile constructed by a woodrat/pack rat (https://en.wikipedia.org/wiki/Pack_rat#Midden)
vcr
is meant really for testing, or script use. i don't think it fits
well into a use case where another pkg wants to cache responsesmemoise
seems close-ish but doesn't fit needs, e.g., no expiry, not specific
to HTTP requests, etc.webmockr
to match requests (works with crul
; soon httr
)2019-03-08 00:00:00
and it's
2019-03-07 23:00:00
, then 1 hr from now the cache will expire, and a new real HTTP
request will need to be made (i.e., the cache will be deleted whenever the next
HTTP request is made)right now we only support crul
, but httr
support should arrive soon
remotes::install_github("sckott/webmiddens")
library(webmiddens) library(crul)
Let's say you have some function http_request()
that does an HTTP request that
you re-use in various parts of your project or package
http_request <- function(...) { x <- crul::HttpClient$new("https://httpbin.org", opts = list(...)) x$get("get") }
And you have a function some_fxn()
that uses http_request()
to do the HTTP
request, then proces the results to a data.frame or list, etc. This is a super
common pattern in a project or R package that deals with web resources.
some_fxn <- function(...) { res <- http_request(...) jsonlite::fromJSON(res$parse("UTF-8")) }
Without webmiddens
the HTTP request happens as usual and all is good
some_fxn()
Now, with webmiddens
run wm_configuration()
first to set the path where HTTP requests will be cached
wm_configuration("foo1")
bb <- midden_current() bb$destroy()
first request is a real HTTP request
res1 <- use_midden(some_fxn()) res1
second request uses the cached response from the first request
res2 <- use_midden(some_fxn()) res2
x <- midden$new() x # no path # Run $init() to set the path x$init(path = "forest") x
The cache
slot has a hoardr
object which you can use to fiddle with
files, see ?hoardr::hoard
x$cache
Use expire()
to set the expire time (in seconds). You can set it through
passing to expire()
or through the environment variable WEBMIDDENS_EXPIRY_SEC
x$expire() x$expire(5) x$expire() x$expire(reset = TRUE) x$expire() Sys.setenv(WEBMIDDENS_EXPIRY_SEC = 35) x$expire() x$expire(reset = TRUE) x$expire()
FIXME: The below not working right now - figure out why
wm_enable() con <- crul::HttpClient$new("https://httpbin.org") # first request is a real HTTP request x$r(con$get("get", query = list(stuff = "bananas"))) # following requests use the cached response x$r(con$get("get", query = list(stuff = "bananas")))
verbose output
x <- midden$new(verbose = TRUE) x$init(path = "rainforest") x$r(con$get("get", query = list(stuff = "bananas")))
set expiration time
x <- midden$new() x$init(path = "grass") x$expire(3) x
Delete all the files in your "midden" (the folder with cached files)
x$cleanup()
Delete the "midden" (the folder with cached files)
x$destroy()
webmiddens
in R doing citation(package = 'webmiddens')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.