From a2aff63b88160308a8273be5bc672794dd1761cc Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 29 May 2024 14:54:24 -0500 Subject: [PATCH] Replace httr with httr2 Also allows us to eliminate memoise since we can rely on httr2's HTTP caching instead :) Since I was in there, I also slightly tweaked the feedback from `build_favicons()`, since I was confused that it didn't print anything on completion. --- DESCRIPTION | 3 +-- NAMESPACE | 1 - R/build-favicons.R | 50 +++++++++++++++++++------------------------- R/build-home-index.R | 27 ++++++++++++++++-------- R/build-news.R | 9 +++++--- 5 files changed, 47 insertions(+), 43 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7009713fe..33d4658ee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,10 +28,9 @@ Imports: downlit (>= 0.4.0), fontawesome, fs (>= 1.4.0), - httr (>= 1.4.2), + httr2, jsonlite, magrittr, - memoise, openssl, purrr (>= 1.0.0), ragg, diff --git a/NAMESPACE b/NAMESPACE index 697768d9e..4baedca3f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -144,5 +144,4 @@ export(template_reference) import(fs) import(rlang) importFrom(magrittr,"%>%") -importFrom(memoise,memoise) importFrom(utils,installed.packages) diff --git a/R/build-favicons.R b/R/build-favicons.R index 0200268c9..3b117e67b 100644 --- a/R/build-favicons.R +++ b/R/build-favicons.R @@ -39,11 +39,10 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) { } cli::cli_inform(c( - i = "Building favicons with {.url https://realfavicongenerator.net} ..." + i = "Building favicons with {.url https://realfavicongenerator.net}..." )) logo <- readBin(logo_path, what = "raw", n = file_info(logo_path)$size) - json_request <- list( "favicon_generation" = list( "api_key" = "87d5cd739b05c00416c4a19cd14a8bb5632ea563", @@ -65,19 +64,17 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) { ) ) ) - - resp <- httr::RETRY( - "POST", - "https://realfavicongenerator.net/api/favicon", - body = json_request, - encode = "json", - quiet = TRUE + req <- httr2::request("https://realfavicongenerator.net/api/favicon") + req <- httr2::req_body_json(req, json_request) + + withCallingHandlers( + resp <- httr2::req_perform(req), + error = function(e) { + cli::cli_abort("API request failed.", parent = e) + } ) - if (httr::http_error(resp)) { - cli::cli_abort("API request failed.", call = caller_env()) - } - content <- httr::content(resp) + content <- httr2::resp_body_json(resp) result <- content$favicon_generation_result if (!identical(result$result$status, "success")) { @@ -85,22 +82,19 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) { } tmp <- withr::local_tempfile() - result <- httr::RETRY( - "GET", - result$favicon$package_url, - httr::write_disk(tmp, overwrite = TRUE), - quiet = TRUE + req <- httr2::request(result$favicon$package_url) + resp <- httr2::req_perform(req, tmp) + + withCallingHandlers( + paths <- utils::unzip(tmp, exdir = path(pkg$src_path, "pkgdown", "favicon")), + warning = function(e) { + cli::cli_abort("Your logo file couldn't be processed and may be corrupt.", parent = e) + }, + error = function(e) { + cli::cli_abort("Your logo file couldn't be processed and may be corrupt.", parent = e) + } ) - - tryCatch({ - utils::unzip(tmp, exdir = path(pkg$src_path, "pkgdown", "favicon")) - }, - warning = function(e) { - cli::cli_abort("Your logo file couldn't be processed and may be corrupt.", parent = e) - }, - error = function(e) { - cli::cli_abort("Your logo file couldn't be processed and may be corrupt.", parent = e) - }) + cli::cli_inform(c("v" = "Added {.path {sort(path_file(paths))}}")) invisible() } diff --git a/R/build-home-index.R b/R/build-home-index.R index ae2fa32a4..64e6ae468 100644 --- a/R/build-home-index.R +++ b/R/build-home-index.R @@ -189,26 +189,35 @@ sidebar_section <- function(heading, bullets, class = make_slug(heading)) { ) } -#' @importFrom memoise memoise -NULL - -cran_link <- memoise(function(pkg) { +cran_link <- function(pkg) { if (!has_internet()) { return(NULL) } cran_url <- paste0("https://cloud.r-project.org/package=", pkg) - - if (!httr::http_error(cran_url)) { + req <- httr2::request(cran_url) + req <- httr2::req_cache(req, path = http_cache_dir(), max_age = 86400) + req <- httr2::req_error(req, function(resp) FALSE) + resp <- httr2::req_perform(req) + if (!httr2::resp_is_error(resp)) { return(list(repo = "CRAN", url = cran_url)) } # bioconductor always returns a 200 status, redirecting to /removed-packages/ bioc_url <- paste0("https://www.bioconductor.org/packages/", pkg) - req <- httr::RETRY("HEAD", bioc_url, quiet = TRUE) - if (!httr::http_error(req) && !grepl("removed-packages", req$url)) { + req <- httr2::request(bioc_url) + req <- httr2::req_cache(req, path = http_cache_dir(), max_age = 86400) + req <- httr2::req_error(req, function(resp) FALSE) + req <- httr2::req_retry(req, max_tries = 3) + resp <- httr2::req_perform(req) + + if (!httr2::resp_is_error(resp) && !grepl("removed-packages", httr2::resp_url(resp))) { return(list(repo = "Bioconductor", url = bioc_url)) } NULL -}) +} + +http_cache_dir <- function() { + dir_create(path(tools::R_user_dir("pkgdown", "cache"), "http")) +} diff --git a/R/build-news.R b/R/build-news.R index 0d08b3afe..420a45778 100644 --- a/R/build-news.R +++ b/R/build-news.R @@ -274,13 +274,16 @@ pkg_timeline <- function(package) { } url <- paste0("https://crandb.r-pkg.org/", package, "/all") + req <- httr2::request(url) + req <- httr2::req_retry(req, max_tries = 3) + req <- httr2::req_error(req, function(resp) FALSE) - resp <- httr::RETRY("GET", url, quiet = TRUE) - if (httr::http_error(resp)) { + resp <- httr2::req_perform(req) + if (httr2::resp_is_error(resp)) { return(NULL) } - content <- httr::content(resp) + content <- httr2::resp_body_json(resp) timeline <- content$timeline data.frame(