From 11aeb30ed55e9213f12602e18d94c3dd4d69978d Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Thu, 30 Nov 2023 10:53:37 -0500 Subject: [PATCH] feat: Use `{httr2}` to download assets to avoid timeout and give a progress bar (#39) --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/assets.R | 24 ++++++++++++++---------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cba6032..377a592 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,6 +20,7 @@ Imports: archive, brio, fs, + httr2 (>= 1.0.0), jsonlite, progress, rappdirs, @@ -27,7 +28,6 @@ Imports: tools Suggests: plumber, - httr, spelling, testthat (>= 3.0.0) Config/Needs/website: tidyverse/tidytemplate diff --git a/NEWS.md b/NEWS.md index 078d6d1..093a58c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # shinylive (development version) +* Use `{httr2}` to download assets from GitHub releases. (@dgkf #30, #39) + # shinylive 0.1.0 * Initial CRAN submission. diff --git a/R/assets.R b/R/assets.R index f1f35bd..77e87e5 100644 --- a/R/assets.R +++ b/R/assets.R @@ -21,10 +21,14 @@ assets_download <- function( version = assets_version(), ..., # Note that this is the cache directory, which is the parent of the assets - # directory. The tarball will have the assets directory as the top-level subdir. + # directory. The tarball will have the assets directory as the top-level + # subdir. dir = assets_cache_dir(), url = assets_bundle_url(version)) { - tmp_targz <- tempfile(paste0("shinylive-", gsub(".", "_", version, fixed = TRUE), "-"), fileext = ".tar.gz") + tmp_targz <- tempfile( + paste0("shinylive-", gsub(".", "_", version, fixed = TRUE), "-"), + fileext = ".tar.gz" + ) on.exit( { @@ -36,12 +40,10 @@ assets_download <- function( ) message("Downloading shinylive assets v", version, "...") - - # temporarily increase download timeout for ?utils::download.file guidelines - opts <- options(timeout = 250 * 60 * 2) # expect minimum 0.5 MB/s - on.exit(options(opts)) - - utils::download.file(url, destfile = tmp_targz, method = "auto") + req <- httr2::request(url) + req <- httr2::req_progress(req) + httr2::req_perform(req, path = tmp_targz) + message("") # Newline after progress bar message("Unzipping to ", dir, "/") fs::dir_create(dir) @@ -372,6 +374,8 @@ assets_version <- function() { check_assets_url <- function( version = assets_version(), url = assets_bundle_url(version)) { - req <- httr::HEAD(url) - req$status_code == 200 + req <- httr2::request(url) + req <- httr2::req_method(req, "HEAD") + resp <- httr2::req_perform(req) + resp$status_code == 200 }