Skip to content

Commit 46490fe

Browse files
committed
migrate httr to httr2
see #2602 I copied same upload_file function from httr to utils.R since httr2::req_body_multipart requires to input made by curl::form_file.
1 parent ad57d5c commit 46490fe

File tree

6 files changed

+70
-31
lines changed

6 files changed

+70
-31
lines changed

DESCRIPTION

+2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ Imports:
2121
desc (>= 1.4.1),
2222
ellipsis (>= 0.3.2),
2323
fs (>= 1.5.2),
24+
httr2 (>= 1.1.2),
2425
lifecycle (>= 1.0.1),
2526
memoise (>= 2.0.1),
27+
mime (>= 0.13),
2628
miniUI (>= 0.1.1.1),
2729
pkgbuild (>= 1.3.1),
2830
pkgdown (>= 2.0.6),

NAMESPACE

+11
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,17 @@ import(fs)
8383
importFrom(cli,cat_bullet)
8484
importFrom(cli,cat_rule)
8585
importFrom(ellipsis,check_dots_used)
86+
importFrom(fs,file_delete)
87+
importFrom(fs,file_temp)
88+
importFrom(httr2,req_body_multipart)
89+
importFrom(httr2,req_perform)
90+
importFrom(httr2,request)
91+
importFrom(httr2,resp_body_raw)
92+
importFrom(httr2,resp_body_string)
93+
importFrom(httr2,resp_check_status)
8694
importFrom(lifecycle,deprecated)
8795
importFrom(memoise,memoise)
96+
importFrom(mime,guess_type)
8897
importFrom(pkgbuild,clean_dll)
8998
importFrom(pkgbuild,find_rtools)
9099
importFrom(pkgbuild,has_devel)
@@ -107,6 +116,8 @@ importFrom(remotes,install_svn)
107116
importFrom(remotes,install_url)
108117
importFrom(remotes,install_version)
109118
importFrom(remotes,update_packages)
119+
importFrom(rlang,check_installed)
120+
importFrom(rlang,warn)
110121
importFrom(sessioninfo,package_info)
111122
importFrom(sessioninfo,session_info)
112123
importFrom(stats,update)

R/check-mac.R

+14-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#' @inheritParams check_win
99
#' @param dep_pkgs Additional custom dependencies to install prior to checking
1010
#' the package.
11+
#' @importFrom httr2 request req_body_multipart req_perform resp_check_status resp_body_string
1112
#' @family build functions
1213
#' @return The url with the check results (invisibly)
1314
#' @export
@@ -40,26 +41,25 @@ check_mac_release <- function(pkg = ".", dep_pkgs = character(), args = NULL, ma
4041

4142
url <- "https://mac.r-project.org/macbuilder/v1/submit"
4243

43-
rlang::check_installed("httr")
44-
body <- list(pkgfile = httr::upload_file(built_path))
44+
rlang::check_installed("httr2")
45+
body <- list(pkgfile = upload_file(built_path))
4546

47+
# upload_file function implemented in utils.R
48+
4649
if (length(dep_built_paths) > 0) {
47-
uploads <- lapply(dep_built_paths, httr::upload_file)
50+
uploads <- lapply(dep_built_paths, upload_file)
4851
names(uploads) <- rep("depfiles", length(uploads))
4952
body <- append(body, uploads)
5053
}
5154

52-
res <- httr::POST(url,
53-
body = body,
54-
headers = list(
55-
"Content-Type" = "multipart/form-data"
56-
),
57-
encode = "multipart"
58-
)
55+
req <- httr2::request(url)
56+
req <- httr2::req_body_multipart(req, !!!body)
57+
res <- httr2::req_perform(req)
5958

60-
httr::stop_for_status(res, task = "Uploading package")
59+
httr2::resp_check_status(res, info = "Uploading package")
6160

62-
response_url <- httr::content(res)$url
61+
res_body <- httr2::resp_body_string(res)
62+
response_url <- regmatches(res_body, regexpr("https://mac\\.R-project\\.org/macbuilder/results/[0-9a-zA-Z\\-]+/", res_body))
6363

6464
if (!quiet) {
6565
time <- strftime(Sys.time() + 10 * 60, "%I:%M %p")
@@ -72,3 +72,5 @@ check_mac_release <- function(pkg = ".", dep_pkgs = character(), args = NULL, ma
7272

7373
invisible(response_url)
7474
}
75+
76+

R/release.R

+21-12
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,27 @@ upload_cran <- function(pkg, built_path, call = parent.frame()) {
277277

278278
# Initial upload ---------
279279
cli::cli_inform(c(i = "Uploading package & comments"))
280-
rlang::check_installed("httr")
280+
rlang::check_installed("httr2")
281281
body <- list(
282282
pkg_id = "",
283283
name = maint$name,
284284
email = maint$email,
285-
uploaded_file = httr::upload_file(built_path, "application/x-gzip"),
285+
uploaded_file = upload_file(built_path, "application/x-gzip"),
286286
comment = comments,
287287
upload = "Upload package"
288288
)
289-
r <- httr::POST(cran_submission_url, body = body)
289+
290+
req <- httr2::request(cran_submission_url)
291+
req <- httr2::req_body_multipart(req, !!!body)
292+
r <- httr2::req_perform(req)
290293

291294
# If a 404 likely CRAN is closed for maintenance, try to get the message
292-
if (httr::status_code(r) == 404) {
295+
if (httr2::resp_status(r) == 404) {
293296
msg <- ""
294-
try({
295-
r2 <- httr::GET(sub("index2", "index", cran_submission_url))
296-
msg <- extract_cran_msg(httr::content(r2, "text"))
297+
try({
298+
req2 <- httr2::request(sub("index2", "index", cran_submission_url))
299+
r2 <- httr2::req_perform(req2)
300+
msg <- extract_cran_msg(httr2::resp_body_string(r2))
297301
})
298302
cli::cli_abort(
299303
c(
@@ -304,8 +308,8 @@ upload_cran <- function(pkg, built_path, call = parent.frame()) {
304308
)
305309
}
306310

307-
httr::stop_for_status(r)
308-
new_url <- httr::parse_url(r$url)
311+
httr2::resp_check_status(r)
312+
new_url <- httr2::url_parse(r$url)
309313

310314
# Confirmation -----------
311315
cli::cli_inform(c(i = "Confirming submission"))
@@ -316,9 +320,14 @@ upload_cran <- function(pkg, built_path, call = parent.frame()) {
316320
policy_check = "1/",
317321
submit = "Submit package"
318322
)
319-
r <- httr::POST(cran_submission_url, body = body)
320-
httr::stop_for_status(r)
321-
new_url <- httr::parse_url(r$url)
323+
324+
req <- httr2::request(cran_submission_url)
325+
req <- httr2::req_body_multipart(req, !!!body)
326+
r <- httr2::req_perform(req)
327+
328+
httr2::resp_check_status(r)
329+
330+
new_url <- httr2::url_parse(r$url)
322331
if (new_url$query$submit == "1") {
323332
cli::cli_inform(c(
324333
"v" = "Package submission successful",

R/run-source.R

+13-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#' @param url url
1414
#' @param ... other options passed to [source()]
1515
#' @param sha1 The (prefix of the) SHA-1 hash of the file at the remote URL.
16+
#' @importFrom fs file_temp file_delete
17+
#' @importFrom rlang check_installed warn
18+
#' @importFrom httr2 request req_perform resp_check_status resp_body_raw
19+
#' @importFrom ellipsis check_dots_used
1620
#' @export
1721
#' @seealso [source_gist()]
1822
#' @examples
@@ -31,18 +35,20 @@
3135
source_url <- function(url, ..., sha1 = NULL) {
3236
stopifnot(is.character(url), length(url) == 1)
3337
rlang::check_installed("digest")
34-
rlang::check_installed("httr")
38+
rlang::check_installed("httr2")
3539

36-
temp_file <- file_temp()
37-
on.exit(file_delete(temp_file), add = TRUE)
40+
temp_file <- fs::file_temp()
41+
on.exit(fs::file_delete(temp_file), add = TRUE)
3842

39-
request <- httr::GET(url)
40-
httr::stop_for_status(request)
41-
writeBin(httr::content(request, type = "raw"), temp_file)
43+
resp <- httr2::req_perform(httr2::request(url))
44+
45+
httr2::resp_check_status(resp)
46+
47+
writeBin(httr2::resp_body_raw(resp), temp_file)
4248

4349
check_sha1(temp_file, sha1)
4450

45-
check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))
51+
ellipsis::check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))
4652
source(temp_file, ...)
4753
}
4854

R/utils.R

+9
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ is_testing <- function() {
4545
is_rstudio_running <- function() {
4646
!is_testing() && rstudioapi::isAvailable()
4747
}
48+
49+
#' @importFrom mime guess_type
50+
upload_file <- function(path, type = NULL) {
51+
stopifnot(is.character(path), length(path) == 1, file.exists(path))
52+
if (is.null(type)) {
53+
type <- mime::guess_type(path)
54+
}
55+
curl::form_file(path, type)
56+
}

0 commit comments

Comments
 (0)