Skip to content

Commit 23e37a5

Browse files
authored
Merge pull request #78 from ropensci-review-tools/httr2
httr2
2 parents b0bbc22 + 87fbdbc commit 23e37a5

File tree

8 files changed

+81
-208
lines changed

8 files changed

+81
-208
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: roreviewapi
22
Title: Plumber API to report package structure and function
3-
Version: 0.1.1.021
3+
Version: 0.1.1.030
44
Authors@R:
55
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-2172-5265"))
@@ -20,7 +20,7 @@ Imports:
2020
ghql,
2121
gitcreds,
2222
here,
23-
httr,
23+
httr2,
2424
jsonlite,
2525
logger,
2626
magrittr,

R/dependencies.R

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,6 @@ install_sys_deps <- function (path, os, os_release) {
7777

7878
install_scripts <- sysreqs_rspm (desc_file, os, os_release)
7979

80-
if (is.integer (install_scripts)) {
81-
# use rhub (see #22)
82-
install_scripts <- NULL
83-
sysreqs <- sysreqs_rhub (desc_file)
84-
if (length (sysreqs) > 0L) {
85-
install_scripts <- paste0 (
86-
"apt-get install -y ",
87-
sysreqs
88-
)
89-
}
90-
}
91-
9280
if (length (install_scripts) > 0L) {
9381
tmp <- lapply (install_scripts, system) # nolint
9482
}
@@ -111,49 +99,19 @@ sysreqs_rspm <- function (desc_file, os, os_release) {
11199
os,
112100
os_release
113101
)
114-
res <- httr::POST (url = u, body = httr::upload_file (desc_file))
115-
116-
if (res$status != 200L) {
117-
return (res$status)
118-
}
102+
req <- httr2::request (u)
103+
req <- httr2::req_headers (req, "Content-Type" = "application/json")
104+
req <- httr2::req_body_file (req, path = desc_file)
105+
resp <- httr2::req_perform (req)
106+
httr2::resp_check_status (resp)
119107

120-
res <- httr::content (res, type = "text", encoding = "UTF-8")
121-
res <- jsonlite::fromJSON (res, simplifyDataFrame = TRUE)$dependencies
108+
body <- httr2::resp_body_json (resp, simplifyVector = TRUE)
122109

123-
install_scripts <- unique (unlist (res$install_scripts))
110+
install_scripts <- unique (unlist (body$dependencies$install_scripts))
124111

125112
return (install_scripts)
126113
}
127114

128-
#' Get system requirements from rhub.io
129-
#'
130-
#' @param desc_file Path to DESC file
131-
#' @return Character vector of system requirements, but not in full form of
132-
#' install scripts.
133-
#' @noRd
134-
sysreqs_rhub <- function (desc_file) {
135-
136-
d <- data.frame (read.dcf (desc_file))
137-
138-
sr <- gsub ("\\s", "%20", d$SystemRequirements)
139-
140-
if (length (sr) == 0L) {
141-
return (NULL)
142-
}
143-
144-
rhub <- sprintf ("http://sysreqs.r-hub.io/map/%s", sr)
145-
146-
res <- httr::GET (url = rhub)
147-
res <- httr::content (res, type = "text", encoding = "UTF-8")
148-
req <- jsonlite::fromJSON (res, simplifyDataFrame = TRUE)
149-
150-
req <- lapply (req, function (i) i$platforms$DEB)
151-
req <- unique (unname (unlist (req)))
152-
req <- req [which (!is.na (req))]
153-
154-
return (req)
155-
}
156-
157115
upgradeable_pkgs <- function (path, repos) {
158116

159117
deps <- remotes::dev_package_deps (

R/gh-issue.R

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -228,43 +228,12 @@ check_html_variable <- function (x, variable) {
228228
#' Get GitHub token
229229
#'
230230
#' (Same as in `pkgcheck`)
231-
#' @param token_name Optional name of token to use
232-
#' @return The value of the GitHub access token extracted from environment
233-
#' variables.
231+
#' @return The value of the GitHub access token
234232
#' @family github
235233
#' @noRd
236-
get_gh_token <- function (token_name = "") {
234+
get_gh_token <- function () {
237235

238-
e <- Sys.getenv ()
239-
if (token_name != "") {
240-
241-
toks <- unique (e [grep (token_name, names (e))])
242-
243-
} else {
244-
245-
toks <- e [grep ("GITHUB", names (e))]
246-
if (length (unique (toks)) > 1) {
247-
toks <- toks [grep ("TOKEN|PAT", names (toks))]
248-
}
249-
# GitHub runners have "GITHUB_PATH" and "GITHUB_EVENT_PATH"
250-
if (length (unique (toks)) > 1) {
251-
toks <- toks [grep ("TOKEN$|PAT$", names (toks))]
252-
}
253-
}
254-
255-
if (length (unique (toks)) > 1) {
256-
257-
stop (
258-
"There are ",
259-
length (unique (toks)),
260-
" possible tokens named [",
261-
paste0 (names (toks), collapse = ", "),
262-
"]; please ensure one distinct ",
263-
"token named 'GITHUB_TOKEN' or similar."
264-
)
265-
}
266-
267-
return (unique (toks))
236+
gitcreds::gitcreds_get ()$password
268237
}
269238

270239
issue_cmt_qry <- function (gh_cli, org, repo, issue_num) {

R/utils.R

Lines changed: 16 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ symbol_crs <- function () {
2424
#' @noRd
2525
get_github_user <- function () {
2626

27-
gh_tok <- gitcreds::gitcreds_get ()$password
27+
gh_tok <- get_gh_token ()
2828

2929
# Check corresponding user name:
3030
u <- "https://api.github.com/user"
31-
h <- httr::add_headers (Authorization = paste ("Bearer", gh_tok, sep = " "))
32-
x <- httr::content (httr::GET (u, h), "text")
33-
34-
# Then extract user:
35-
x <- strsplit (x, "\\n") [[1]]
36-
login <- grep ("\\\"login\\\"\\:", x, value = TRUE)
37-
if (length (login) > 0L) {
38-
login <- gsub ("\\\"|,$", "", strsplit (login, "\\:\\s+") [[1]] [2])
39-
} else {
40-
login <- ""
31+
req <- httr2::request (u)
32+
req <- httr2::req_headers (
33+
req,
34+
"Authorization" = paste0 ("Bearer ", gh_tok),
35+
"Content-Type" = "application/json"
36+
)
37+
38+
resp <- httr2::req_perform (req)
39+
httr2::resp_check_status (resp)
40+
41+
x <- httr2::resp_body_json (resp)
42+
login <- ""
43+
if ("login" %in% names (x)) {
44+
login <- x$login
4145
}
4246

4347
return (login)
@@ -117,79 +121,4 @@ get_subdir_from_url <- function (repourl) {
117121
return (subdir)
118122
}
119123

120-
#' Bob Rudis's URL checker function
121-
#'
122-
#' This is used here to check the URLs in the `check_issue_template` function.
123-
#'
124-
#' @param x a single URL
125-
#' @param non_2xx_return_value what to do if the site exists but the HTTP status
126-
#' code is not in the `2xx` range. Default is to return `FALSE`.
127-
#' @param quiet if not `FALSE`, then every time the `non_2xx_return_value`
128-
#' condition arises a warning message will be displayed. Default is `FALSE`.
129-
#' @param ... other params (`timeout()` would be a good one) passed directly to
130-
#' `httr::HEAD()` and/or `httr::GET()`
131-
#' @return 'TRUE' is 'x' is a valid URL.
132-
#'
133-
#' @note
134-
#' https://stackoverflow.com/questions/52911812/check-if-url-exists-in-r
135-
#' @noRd
136-
url_exists <- function (x, non_2xx_return_value = FALSE, quiet = FALSE, ...) {
137-
138-
# you don't need thse two functions if you're already using `purrr`
139-
# but `purrr` is a heavyweight compiled pacakge that introduces
140-
# many other "tidyverse" dependencies and this doesnt.
141-
142-
capture_error <- function (code, otherwise = NULL, quiet = TRUE) {
143-
tryCatch (
144-
list (result = code, error = NULL),
145-
error = function (e) {
146-
if (!quiet) {
147-
message ("Error: ", e$message)
148-
}
149-
150-
list (result = otherwise, error = e)
151-
},
152-
interrupt = function (e) {
153-
stop ("Terminated by user", call. = FALSE)
154-
}
155-
)
156-
}
157-
158-
safely <- function (.f, otherwise = NULL, quiet = TRUE) {
159-
function (...) capture_error (.f (...), otherwise, quiet)
160-
}
161-
162-
sHEAD <- safely (httr::HEAD) # nolint
163-
sGET <- safely (httr::GET) # nolint
164-
165-
# Try HEAD first since it's lightweight
166-
res <- sHEAD (x, ...)
167-
168-
if (is.null (res$result) ||
169-
((httr::status_code (res$result) %/% 200) != 1)) {
170-
171-
res <- sGET (x, ...)
172-
173-
if (is.null (res$result)) {
174-
return (FALSE)
175-
} # or whatever you want to return on "hard" errors
176-
177-
if (((httr::status_code (res$result) %/% 200) != 1)) {
178-
if (!quiet) {
179-
warning (paste0 (
180-
"Requests for [",
181-
x,
182-
"] responded but without an HTTP status ",
183-
"code in the 200-299 range"
184-
))
185-
}
186-
return (non_2xx_return_value)
187-
}
188-
189-
return (TRUE)
190-
191-
} else {
192-
return (TRUE)
193-
}
194-
195-
}
124+
url_exists <- getFromNamespace ("url_exists", "pkgcheck")

codemeta.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
"codeRepository": "https://github.com/ropensci-review-tools/roreviewapi",
99
"issueTracker": "https://github.com/ropensci-review-tools/roreviewapi/issues",
1010
"license": "https://spdx.org/licenses/GPL-3.0",
11-
"version": "0.1.1.021",
11+
"version": "0.1.1.030",
1212
"programmingLanguage": {
1313
"@type": "ComputerLanguage",
1414
"name": "R",
1515
"url": "https://r-project.org"
1616
},
17-
"runtimePlatform": "R version 4.4.1 (2024-06-14)",
17+
"runtimePlatform": "R version 4.5.1 (2025-06-13)",
1818
"author": [
1919
{
2020
"@type": "Person",
@@ -225,15 +225,15 @@
225225
},
226226
"10": {
227227
"@type": "SoftwareApplication",
228-
"identifier": "httr",
229-
"name": "httr",
228+
"identifier": "httr2",
229+
"name": "httr2",
230230
"provider": {
231231
"@id": "https://cran.r-project.org",
232232
"@type": "Organization",
233233
"name": "Comprehensive R Archive Network (CRAN)",
234234
"url": "https://cran.r-project.org"
235235
},
236-
"sameAs": "https://CRAN.R-project.org/package=httr"
236+
"sameAs": "https://CRAN.R-project.org/package=httr2"
237237
},
238238
"11": {
239239
"@type": "SoftwareApplication",
@@ -350,7 +350,7 @@
350350
},
351351
"SystemRequirements": "gh, dos2unix"
352352
},
353-
"fileSize": "128.087KB",
353+
"fileSize": "127.539KB",
354354
"readme": "https://github.com/ropensci-review-tools/roreviewapi/blob/main/README.md",
355355
"contIntegration": "https://github.com/ropensci-review-tools/roreviewapi/actions?query=workflow%3AR-CMD-check",
356356
"developmentStatus": "https://www.repostatus.org/#active"

makefile

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
LFILE = README
1+
RFILE = README
22

3-
all: knith #open
3+
all: help
44

5-
knith: $(LFILE).Rmd
6-
echo "rmarkdown::render('$(LFILE).Rmd',output_file='$(LFILE).html')" | R --no-save -q
5+
doc: ## Update package documentation with `roxygen2`
6+
Rscript -e "roxygen2::roxygenise()"; \
77

8-
knitr: $(LFILE).Rmd
9-
echo "rmarkdown::render('$(LFILE).Rmd', output_format = rmarkdown::md_document (variant = 'gfm'))" | R --no-save -q
8+
knith: $(RFILE).Rmd ## Render README as HTML
9+
echo "rmarkdown::render('$(RFILE).Rmd',output_file='$(RFILE).html')" | R --no-save -q
1010

11-
open: $(LFILE).html
12-
xdg-open $(LFILE).html &
11+
knitr: $(RFILE).Rmd ## Render README as markdown
12+
echo "rmarkdown::render('$(RFILE).Rmd',output_file='$(RFILE).md')" | R --no-save -q
1313

14-
clean:
15-
rm -rf *.html *.png README_cache
14+
allcon: ## Run 'allcontributors::add_contributors'
15+
Rscript -e 'allcontributors::add_contributors()'
16+
17+
check: ## Run `rcmdcheck`
18+
Rscript -e 'rcmdcheck::rcmdcheck()'
19+
20+
test: ## Run test suite
21+
Rscript -e 'testthat::test_local()'
22+
23+
pkgcheck: ## Run `pkgcheck` and print results to screen.
24+
Rscript -e 'library(pkgcheck); checks <- pkgcheck(); print(checks); summary (checks)'
25+
26+
clean: ## Clean all junk files, including all pkgdown docs
27+
rm -rf *.html *.png README_cache docs/
28+
29+
help: ## Show this help
30+
@printf "Usage:\033[36m make [target]\033[0m\n"
31+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

tests/testthat/test-utils.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
test_all <- identical (Sys.getenv ("MPADGE_LOCAL"), "true") ||
2-
identical (Sys.getenv ("GITHUB_JOB"), "test-coverage")
1+
test_all <- identical (Sys.getenv ("MPADGE_LOCAL"), "true")
2+
# identical (Sys.getenv ("GITHUB_JOB"), "test-coverage")
33
testthat::skip_if (!test_all)
44

5+
# The 'get_github_user()' is not authorized on gh runners
6+
57
test_that ("utils symbols", {
68
expect_equal (symbol_tck (), ":heavy_check_mark:")
79
expect_equal (symbol_crs (), ":heavy_multiplication_x:")
@@ -26,9 +28,5 @@ test_that ("utils gh branch", {
2628
repourl <- "https://github.com/ropensci/software-review/tree/other"
2729
b <- get_branch_from_url (repourl)
2830
expect_equal (b, "other")
29-
expect_warning (
30-
chk <- url_exists (repourl),
31-
"Requests for \\["
32-
)
33-
expect_false (chk)
31+
expect_false (url_exists (repourl))
3432
})

0 commit comments

Comments
 (0)