Skip to content

Commit

Permalink
allow httr as well
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 11, 2024
1 parent c17dc93 commit eaa8ca1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Suggests:
grDevices,
gt,
httptest2,
httr,
httr2,
interp,
ivreg,
Expand Down
47 changes: 45 additions & 2 deletions R/download_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,54 @@ download_model <- function(name,
url = "https://raw.github.com/easystats/circus/master/data/",
extension = ".rda",
verbose = TRUE) {
.download_data_github(name, url, extension, verbose)
if (check_if_installed("httr2", quietly = TRUE)) {
.download_data_httr2(name, url, extension, verbose)
} else {
.download_data_httr(name, url, extension, verbose)
}
}


# Download rda files from github, using httr
.download_data_httr <- function(name, url, extension, verbose) {
check_if_installed("httr", "to download models from the circus-repo")

url <- paste0(url, name, extension)

temp_file <- tempfile()
on.exit(unlink(temp_file))

result <- tryCatch(
{
request <- httr::GET(url)
httr::stop_for_status(request)
},
error = function(e) {
if (verbose) {
format_alert(
"Could not download model. Request failed with following error:",
e$message
)
}
NULL
}
)
if (is.null(result)) {
return(NULL)
}

writeBin(httr::content(request, type = "raw"), temp_file)

x <- load(temp_file)
model <- get(x)
rm(x)

model
}


.download_data_github <- function(name, url, extension = ".rda", verbose = TRUE) {
# Download rda files from github, using httr2
.download_data_httr2 <- function(name, url, extension = ".rda", verbose = TRUE) {
check_if_installed("httr2", "to download models from the circus-repo")

url <- paste0(url, name, extension)
Expand Down

0 comments on commit eaa8ca1

Please sign in to comment.