webmiddens

knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  warning = FALSE
)

Project Status: Active – The project has reached a stable, usable state and is being actively developed. R-CMD-check codecov

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)

the need

brainstorming

http libraries

right now we only support crul, but httr support should arrive soon

installation

remotes::install_github("sckott/webmiddens")

use_midden()

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

the midden class

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()

Meta



ropensci/webmiddens documentation built on Dec. 11, 2020, 9:03 p.m.