Skip to content

Commit

Permalink
do not use own url_exists but httr's http_error
Browse files Browse the repository at this point in the history
  • Loading branch information
nuest committed Nov 13, 2024
1 parent 0e608d1 commit 13267b4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 90 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: codecheck
Title: Helper Functions for CODECHECK Project
Version: 0.11.3
Version: 0.11.4
Authors@R:
c(person(given = "Stephen",
family = "Eglen",
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ importFrom(R.cache,addMemoization)
importFrom(R.cache,getCacheRootPath)
importFrom(gh,gh)
importFrom(httr,GET)
importFrom(httr,HEAD)
importFrom(httr,content)
importFrom(httr,status_code)
importFrom(httr,http_error)
importFrom(httr,http_status)
importFrom(knitr,kable)
importFrom(osfr,osf_download)
importFrom(osfr,osf_ls_files)
Expand Down
72 changes: 9 additions & 63 deletions R/configuration.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ get_codecheck_yml <- function(x) {
#'
#' @author Daniel Nüst
#' @importFrom rorcid check_dois
#' @importFrom httr http_error http_status GET
#'
#' @export
validate_codecheck_yml <- function(configuration) {
Expand Down Expand Up @@ -218,73 +219,18 @@ validate_codecheck_yml <- function(configuration) {
if(assertthat::has_name(codecheck_yml, "repository")) {
if(is.vector(codecheck_yml$repository)) {
for(r in codecheck_yml$repository) {
assertthat::assert_that(url_exists(r, quiet = TRUE) == TRUE,
msg = paste0(r, " - URL does not exist"))
assertthat::assert_that(httr::http_error(r) == FALSE,
msg = paste0(r, " - URL returns error: ",
toString(httr::http_status(httr::GET(r))))
)
}
} else {
assertthat::assert_that(url_exists(codecheck_yml$repository, quiet = TRUE) == TRUE,
msg = paste0(codecheck_yml$repository, " - URL does not exist"))
assertthat::assert_that(httr::http_error(codecheck_yml$repository) == FALSE,
msg = paste0(codecheck_yml$repository, " - URL returns error: ",
toString(httr::http_status(httr::GET(r))))
)
}
}

return(TRUE)
}

#' From https://stackoverflow.com/a/52915256/261210 by hrbrmstr
#'
#' @param x a single URL
#' @param non_2xx_return_value what to do if the site exists but the
#' HTTP status code is not in the `2xx` range. Default is to return `FALSE`.
#' @param quiet if not `FALSE`, then every time the `non_2xx_return_value` condition
#' arises a warning message will be displayed. Default is `FALSE`.
#' @param ... other params (`timeout()` would be a good one) passed directly
#' to `httr::HEAD()` and/or `httr::GET()`
#'
#' @importFrom httr HEAD GET status_code
url_exists <- function(x, non_2xx_return_value = FALSE, quiet = FALSE,...) {

capture_error <- function(code, otherwise = NULL, quiet = TRUE) {
tryCatch(
list(result = code, error = NULL),
error = function(e) {
if (!quiet)
message("Error: ", e$message)

list(result = otherwise, error = e)
},
interrupt = function(e) {
stop("Terminated by user", call. = FALSE)
}
)
}

safely <- function(.f, otherwise = NULL, quiet = TRUE) {
function(...) capture_error(.f(...), otherwise, quiet)
}

sHEAD <- safely(httr::HEAD)
sGET <- safely(httr::GET)

# Try HEAD first since it's lightweight
res <- sHEAD(x, ...)

if (is.null(res$result) ||
((httr::status_code(res$result) %/% 200) != 1)) {

res <- sGET(x, ...)

if (is.null(res$result)) return(NA) # or whatever you want to return on "hard" errors

if (((httr::status_code(res$result) %/% 200) != 1)) {
if (!quiet) warning(sprintf("Requests for [%s] responded but without an HTTP status code in the 200-299 range", x))
return(non_2xx_return_value)
}

return(TRUE)

} else {
return(TRUE)
}

}

4 changes: 3 additions & 1 deletion inst/tinytest/test_codecheck_yml.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ expect_error(validate_codecheck_yml("yaml/codechecker_name_missing/codecheck.yml

# repository/ies ----
expect_error(validate_codecheck_yml("yaml/repository_url_invalid/codecheck.yml"),
pattern = "URL does not exist")
pattern = "URL returns error")
expect_error(validate_codecheck_yml("yaml/repository_url_invalid/codecheck-with-list.yml"),
pattern = "URL returns error")
expect_error(validate_codecheck_yml("yaml/repository_url_invalid/codecheck-with-list.yml"),
pattern = "does_not_exist")
expect_silent(validate_codecheck_yml("yaml/repository_url_invalid/codecheck-valid.yml"))
Expand Down
23 changes: 0 additions & 23 deletions man/url_exists.Rd

This file was deleted.

0 comments on commit 13267b4

Please sign in to comment.