Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MansMeg committed Jan 28, 2024
1 parent bc39c8a commit efee629
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
16 changes: 10 additions & 6 deletions R/pxweb.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
#'
#' @export
pxweb <- function(url) {
if(!curl::has_internet()){
message(no_internet_msg())
return(NULL)
}
if (is.pxweb(url)) {
return(url)
}
checkmate::assert_string(url)
url_parsed <- parse_url_or_fail(x = url)
if(!has_internet(url_parsed$hostname)){
message(no_internet_msg(url_parsed$hostname))
return(NULL)

Check warning on line 39 in R/pxweb.R

View check run for this annotation

Codecov / codecov/patch

R/pxweb.R#L38-L39

Added lines #L38 - L39 were not covered by tests
}

obj <- list(
url = url_parsed,
Expand Down Expand Up @@ -124,6 +124,10 @@ pxweb_tempdir <- function(to = "apis") {
}
}

no_internet_msg <- function(){
return("No internet access.")
no_internet_msg <- function(host){
return(paste0("No internet access to '", host, "'."))

Check warning on line 128 in R/pxweb.R

View check run for this annotation

Codecov / codecov/patch

R/pxweb.R#L128

Added line #L128 was not covered by tests
}

has_internet <- function(host){
!is.null(curl::nslookup(host, error = FALSE))
}
11 changes: 7 additions & 4 deletions R/pxweb_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ pxweb_get_data <- function(url, query, verbose = TRUE, column.name.type = "text"
#' both \code{httr::POST} and \code{httr::GET}.
#' @export
pxweb_advanced_get <- function(url, query = NULL, verbose = TRUE, log_http_calls = FALSE, pxmdo = NULL, ...) {
if(!curl::has_internet()){
message(no_internet_msg())
return(NULL)
}
checkmate::assert_flag(log_http_calls)
checkmate::assert_class(pxmdo, classes = "pxweb_metadata", null.ok = TRUE)
if (log_http_calls) {
pxweb_http_log_on()
}
if(!is.pxweb(url)){
url_parsed <- parse_url_or_fail(x = url)
if(!has_internet(url_parsed$hostname)){
message(no_internet_msg(url_parsed$hostname))
return(NULL)

Check warning on line 107 in R/pxweb_get.R

View check run for this annotation

Codecov / codecov/patch

R/pxweb_get.R#L106-L107

Added lines #L106 - L107 were not covered by tests
}
}

px <- pxweb(url)
if (!is.null(query)) {
Expand Down
19 changes: 15 additions & 4 deletions R/pxweb_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
#' ## x <- pxweb_interactive(x = "https://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/")
#'
pxweb_interactive <- function(x = NULL) {
if(!curl::has_internet()){
message(no_internet_msg())
return(NULL)
# Check internet access if x is an url
if(!is.null(x)){
url_parsed <- parse_url(x)
if(parsed_url_has_hostname(url_parsed)){
if(!has_internet(url_parsed$hostname)){
message(no_internet_msg(url_parsed$hostname))
return(NULL)

Check warning on line 32 in R/pxweb_interactive.R

View check run for this annotation

Codecov / codecov/patch

R/pxweb_interactive.R#L27-L32

Added lines #L27 - L32 were not covered by tests
}
}
}

# Setup structure
pxe <- pxweb_explorer(x)

Expand Down Expand Up @@ -149,7 +156,11 @@ pxweb_explorer.pxweb <- function(x) {
#' @rdname pxweb_explorer
#' @keywords internal
pxweb_explorer.pxweb_api_catalogue_entry <- function(x) {
pxe <- list(pxweb = pxweb(build_pxweb_url(x)))
suppressMessages(pxe <- list(pxweb = pxweb(build_pxweb_url(x))))
if(is.null(pxe$pxweb)) {
stop(no_internet_msg(parse_url(x$url)$hostname),
call. = FALSE)

Check warning on line 162 in R/pxweb_interactive.R

View check run for this annotation

Codecov / codecov/patch

R/pxweb_interactive.R#L161-L162

Added lines #L161 - L162 were not covered by tests
}
tot_pos <- pxweb_api_path(pxe$pxweb, as_vector = TRUE)
pxe$root <- character(0)
pxe$position <- character(0)
Expand Down
19 changes: 15 additions & 4 deletions R/pxweb_parse_url_or_fail.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
#'
#' @keywords internal
parse_url_or_fail <- function(x) {
pu <- parse_url(x)
if (!parsed_url_has_hostname(x)) {
stop("Cannot parse url (hostname). Please check url for potential errors (?parse_url): '", x, "'", call. = FALSE)
}
pu
}

parse_url <- function(x) {
checkmate::assert_string(x)
x <- pxweb_fix_url(x)
parsed_url <- httr::parse_url(x)
if (is.null(parsed_url$hostname)) {
stop("Cannot parse url (hostname). Please check url for potential errors (?parse_url): '", x, "'", call. = FALSE)
httr::parse_url(x)
}

parsed_url_has_hostname <- function(x){
if(!checkmate::test_class(x, "url")){
x <- parse_url(x)
}
parsed_url
!is.null(x$hostname)
}

0 comments on commit efee629

Please sign in to comment.