web middens class

Public fields

cache

cache_path

(character) the cache path

verbose

(logical) verbose or not

Methods

Public methods


Method new()

Create a new midden object

Usage

midden$new(verbose = FALSE)

Arguments

verbose

(logical) get messages about whats going on. default: FALSE

Returns

A new midden object


Method print()

print method for midden objects

Usage

midden$print(x, ...)

Arguments

x

self

...

ignored


Method r()

execute an http request code block

Usage

midden$r(...)

Arguments

...

any function that makes an http request

Returns

http response


Method init()

initialize the class with a path for where to cache data

Usage

midden$init(
  path = NULL,
  type = "user_cache_dir",
  prefix = "R",
  full_path = NULL
)

Arguments

path

(character) the path to be appended to the cache path set by type

type

(character) the type of cache, see rappdirs

prefix

(character) prefix to the path value. Default: "R"

full_path

(character) instead of using path, type, and prefix just set the full path with this parameter

Returns

NULL


Method destroy()

remove all cached files in the midden, including directory

Usage

midden$destroy()

Returns

NULL


Method cleanup()

remove all cached files in the midden, but leave the directory

Usage

midden$cleanup()

Returns

NULL


Method expire()

set an expiration time

Usage

midden$expire(expire = NULL, reset = FALSE)

Arguments

expire

(integer) seconds to expire - OR, set via the environment variable WEBMIDDENS_EXPIRY_SEC

reset

(logical) reset to NULL? default: FALSE

Returns

NULL

Examples

z <- midden$new()
z$expire(35) # set to expire all requests in 35 seconds
# or set by env var
Sys.setenv(WEBMIDDENS_EXPIRY_SEC = 35)


Method cache_response()

cache response

Usage

midden$cache_response(x, exp)

Arguments

x

a crul HttpResponse

exp

(integer) seconds to expire


Method clone()

The objects of this class are cloneable with this method.

Usage

midden$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

if (FALSE) { library(crul) # without middens con <- crul::HttpClient$new("https://httpbin.org") con2 <- crul::HttpClient$new("https://google.com") con$get("get", query = list(stuff = "bananas")) con2$get(query = list(q = "stuff")) # with middens x <- midden$new() x x$init(path = "rainforest3") x x$cache 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() # 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 # real request x$r(con$get("get", query = list(grass = "tall"))) ## before expiry, get mocked response x$r(con$get("get", query = list(grass = "tall"))) Sys.sleep(5) ## after expiry, get real response x$r(con$get("get", query = list(grass = "tall"))) } ## ------------------------------------------------ ## Method `midden$expire` ## ------------------------------------------------ z <- midden$new() z$expire(35) # set to expire all requests in 35 seconds
#> [1] 35
# or set by env var Sys.setenv(WEBMIDDENS_EXPIRY_SEC = 35)