Skip to content

Commit

Permalink
Replace httr with httr2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hadley committed May 29, 2024
1 parent 67d4110 commit a2aff63
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,4 @@ export(template_reference)
import(fs)
import(rlang)
importFrom(magrittr,"%>%")
importFrom(memoise,memoise)
importFrom(utils,installed.packages)
50 changes: 22 additions & 28 deletions R/build-favicons.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -65,42 +64,37 @@ 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")) {
cli::cli_abort("API request failed.", .internal = TRUE)
}

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()
}
Expand Down
27 changes: 18 additions & 9 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
9 changes: 6 additions & 3 deletions R/build-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a2aff63

Please sign in to comment.