Skip to content

Commit 546acde

Browse files
authored
New interal yields_successful_request() (#26)
1 parent 05f4b5f commit 546acde

4 files changed

+45
-1
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Imports:
1919
cli,
2020
dplyr,
2121
glue,
22+
httr2,
2223
rlang,
2324
tibble,
2425
utils

R/document_universe.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ document_universe_impl <- function(x, url_template = NULL) {
99
out,
1010
topic = ifelse(
1111
.data$type == "help",
12-
paste0("<a href=", glue::glue(url_template), ">", .data$topic, "</a>"),
12+
to_href(url = .data$topic, template = glue::glue(url_template)),
1313
.data$topic
1414
)
1515
)
@@ -18,6 +18,9 @@ document_universe_impl <- function(x, url_template = NULL) {
1818
out
1919
}
2020

21+
to_href <- function(url, template) {
22+
paste0("<a href=", template, ">", url, "</a>")
23+
}
2124

2225
#' Create a data frame with documentation metadata of one or more packages
2326
#'

R/yields_successful_request.R

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#' Does a URL yield a successful response?
2+
#'
3+
#' @param url A string.
4+
#'
5+
#' @return Logical.
6+
#'
7+
#' @examples
8+
#' yields_successful_request("bad")
9+
#' yields_successful_request("https://www.google.com/")
10+
#' yields_successful_request("https://www.google.com/404")
11+
#' @noRd
12+
yields_successful_request <- function(url) {
13+
unlist(lapply(url, yields_successful_request_impl))
14+
}
15+
16+
yields_successful_request_impl <- function(url) {
17+
tryCatch(is_succesful_request(url), error = function(e) FALSE)
18+
}
19+
20+
is_succesful_request <- function(url) {
21+
req <- httr2::req_error(httr2::request(url), is_error = \(resp) FALSE)
22+
resp <- httr2::req_perform(req)
23+
identical(httr2::resp_status(resp), 200L)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
test_that("with a bad url returns FALSE", {
2+
url <- "bad"
3+
expect_equal(yields_successful_request(url), FALSE)
4+
})
5+
6+
test_that("with a 404 url returns FALSE", {
7+
skip_if_offline()
8+
url <- "https://www.google.com/not/found"
9+
expect_equal(yields_successful_request(url), FALSE)
10+
})
11+
12+
test_that("with a good url returns FALSE", {
13+
skip_if_offline()
14+
url <- "https://www.google.com/"
15+
expect_equal(yields_successful_request(url), TRUE)
16+
})

0 commit comments

Comments
 (0)