R/http-cache.R

Defines functions async_cached_http_get

the_cache <- new.env(parent = emptyenv())

async_cached_http_get <- function(url, headers = character(),
                                  options = list()) {
  hash <- cli::hash_md5(paste0("http-get-", url))
  if (hash %in% names(the_cache)) {
    async_constant(the_cache[[hash]])
  } else {
    http_get(url, headers = headers, options = options)$
      then(http_stop_for_status)$
      then(function(response) {
        json <- rawToChar(response$content)
        the_cache[[hash]] <- json
        json
      })
  }
}
r-hub/rhub documentation built on April 25, 2024, 9:23 a.m.