Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intercept all httr2 exception and fail gracefully #59

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 68 additions & 58 deletions R/correctTaxo.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,67 +217,77 @@ correctTaxo <- function(genus, species = NULL, score = 0.5, useCache = FALSE, ve

# split missing taxo in chunks of 30
slices <- split(missingTaxo[, slice := ceiling(.I / BATCH_SIZE)], by = "slice", keep.by = TRUE)

# for each slice of queries
if (verbose) {
pb <- utils::txtProgressBar(style = 3)
}
queriedTaxo <- rbindlist(lapply(slices, function(slice) {

req <- httr2::request("https://tnrsapi.xyz/tnrs_api.php")
req <- httr2::req_headers(req,
'Accept' = 'application/json',
'Content-Type' = "application/json",
'charset' = "UTF-8"
)
req <- httr2::req_body_json(req, list(
opts = list(
class = jsonlite::unbox("wfo"),
mode = jsonlite::unbox("resolve"),
matches = jsonlite::unbox("best")
),
data = unname(data.frame(seq_along(slice$query),slice$query))
))

req <- httr2::req_error(req, function(response) FALSE)
qryResult <- httr2::req_perform(req)

if (httr2::resp_is_error(qryResult)) {
message("There appears to be a problem reaching the tnrs API.")
return(invisible(NULL))
}

# parse answer from tnrs
answer <- setDT(httr2::resp_body_json(qryResult, simplifyVector = TRUE))

# recode empty strings as NA
answer[, names(answer) := lapply(.SD, function(x) {
x[x==""]<-NA
x
})]

# format result
answer <- answer[, .(
submittedName = Name_submitted,
score = as.numeric(Overall_score),
matchedName = Name_matched,
from = "iplant_tnrs",
acceptedName = Accepted_name
)]

if (verbose) {
utils::setTxtProgressBar(pb, slice$slice[1] / length(slices))

pb <- if(verbose) utils::txtProgressBar(style = 3)

queriedTaxo <- tryCatch(
{
# for each slice of queries
queriedTaxo <- rbindlist(lapply(slices, function(slice) {

req <- httr2::request("https://tnrsapi.xyz/tnrs_api.php")
req <- httr2::req_headers(req,
'Accept' = 'application/json',
'Content-Type' = "application/json",
'charset' = "UTF-8"
)
req <- httr2::req_body_json(req, list(
opts = list(
class = jsonlite::unbox("wfo"),
mode = jsonlite::unbox("resolve"),
matches = jsonlite::unbox("best")
),
data = unname(data.frame(seq_along(slice$query),slice$query))
))

req <- httr2::req_timeout(req, 20)
qryResult <- httr2::req_perform(req)

# parse answer from tnrs
answer <- setDT(httr2::resp_body_json(qryResult, simplifyVector = TRUE))

# recode empty strings as NA
answer[, names(answer) := lapply(.SD, function(x) {
x[x==""]<-NA
x
})]

# format result
answer <- answer[, .(
submittedName = Name_submitted,
score = as.numeric(Overall_score),
matchedName = Name_matched,
from = "iplant_tnrs",
acceptedName = Accepted_name
)]

if (verbose) {
utils::setTxtProgressBar(pb, slice$slice[1] / length(slices))
}

Sys.sleep(WAIT_DELAY)

answer
}))
},
error = function(e) {
structure(list(), message = e$message)
},
finally = {
# close progress bar
if(!is.null(pb)) {
close(pb)
}
}

Sys.sleep(WAIT_DELAY)

answer
}))
if (verbose) {
close(pb)
}
)
}

if(nrow(missingTaxo) && is.null(nrow(queriedTaxo))) {
warning("There seem to be a problem reaching the TNRS API!\n",
attr(queriedTaxo, "message"), immediate. = TRUE, call. = FALSE)
return(invisible(NULL))
}

# build reference taxonomy from cached and queried ones
fullTaxo <- rbindlist(list(queriedTaxo, cachedTaxo), fill = TRUE)

Expand Down
Loading