From 13267b4e700f3b341b501427b65b7c7257c9039c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20N=C3=BCst?= Date: Wed, 13 Nov 2024 22:47:07 +0100 Subject: [PATCH] do not use own url_exists but httr's http_error trying to fix https://github.com/codecheckers/register/actions/runs/11821512304/job/32936647970 --- DESCRIPTION | 2 +- NAMESPACE | 4 +- R/configuration.R | 72 ++++-------------------------- inst/tinytest/test_codecheck_yml.R | 4 +- man/url_exists.Rd | 23 ---------- 5 files changed, 15 insertions(+), 90 deletions(-) delete mode 100644 man/url_exists.Rd diff --git a/DESCRIPTION b/DESCRIPTION index aeda1c9..3802415 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NAMESPACE b/NAMESPACE index 133feb6..de82739 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/configuration.R b/R/configuration.R index 396e78e..ddcc3f3 100644 --- a/R/configuration.R +++ b/R/configuration.R @@ -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) { @@ -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) - } - -} - diff --git a/inst/tinytest/test_codecheck_yml.R b/inst/tinytest/test_codecheck_yml.R index d22d9bd..3221ef5 100644 --- a/inst/tinytest/test_codecheck_yml.R +++ b/inst/tinytest/test_codecheck_yml.R @@ -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")) diff --git a/man/url_exists.Rd b/man/url_exists.Rd deleted file mode 100644 index bc1f58b..0000000 --- a/man/url_exists.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/configuration.R -\name{url_exists} -\alias{url_exists} -\title{From https://stackoverflow.com/a/52915256/261210 by hrbrmstr} -\usage{ -url_exists(x, non_2xx_return_value = FALSE, quiet = FALSE, ...) -} -\arguments{ -\item{x}{a single URL} - -\item{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`.} - -\item{quiet}{if not `FALSE`, then every time the `non_2xx_return_value` condition -arises a warning message will be displayed. Default is `FALSE`.} - -\item{...}{other params (`timeout()` would be a good one) passed directly -to `httr::HEAD()` and/or `httr::GET()`} -} -\description{ -From https://stackoverflow.com/a/52915256/261210 by hrbrmstr -}