r-lib / httr2

Make HTTP requests and process their responses. A modern reimagining of httr.
https://httr2.r-lib.org
Other
235 stars 56 forks source link

Example for `req_cache()` usage in package #513

Closed m-muecke closed 1 week ago

m-muecke commented 1 month ago

req_cache() already has an example with using tempdir() as the path, would be great to have an example for using it with tools::R_user_dir("pgk", "cache") like done in the pkgdown package and others

hadley commented 3 weeks ago

What sort of example are you looking for?

m-muecke commented 3 weeks ago

Currently the docs mention using unlink(dir(cache_path, full.names = TRUE) or tempdir() for the path arg. Would be great to have a sentence or example for using it inside a package with tools::R_user_dir. Perhaps something similar to what I've found in the pkgdown package:

req_pkgdown_cache <- function(req) {
  cache_path <- dir_create(path(tools::R_user_dir("pkgdown", "cache"), "http"))
  httr2::req_cache(
    req,
    path = cache_path,
    max_age = 86400 # 1 day
  )
}