Skip to content

Commit

Permalink
Implement timeout
Browse files Browse the repository at this point in the history
Fixes #312
  • Loading branch information
raffaem committed Feb 3, 2025
1 parent 81aafb6 commit a9e69ff
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions R/oa_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ oa_fetch <- function(entity = if (is.null(identifier)) NULL else id_type(shorten
count_only = FALSE,
mailto = oa_email(),
api_key = oa_apikey(),
verbose = FALSE) {
verbose = FALSE,
timeout = 30) {
output <- match.arg(output)
entity <- match.arg(entity, oa_entities())

Expand Down Expand Up @@ -142,7 +143,8 @@ oa_fetch <- function(entity = if (is.null(identifier)) NULL else id_type(shorten
mailto = mailto,
api_key = api_key,
parse = output != "raw",
verbose = verbose
verbose = verbose,
timeout = timeout
)
}

Expand Down Expand Up @@ -197,6 +199,10 @@ oa_fetch <- function(entity = if (is.null(identifier)) NULL else id_type(shorten
#' If FALSE, returns the raw JSON response as string.
#' @param verbose Logical.
#' If TRUE, print information about the querying process. Defaults to TRUE.
#' @param timeout Numeric.
#' Maximum number of seconds to wait.
#' An error will be thrown if the request does not complete in the time limit.
#' Defaults to 30.
#'
#' @return a data.frame or a list of bibliographic records.
#'
Expand Down Expand Up @@ -312,7 +318,8 @@ oa_request <- function(query_url,
mailto = oa_email(),
api_key = oa_apikey(),
parse = TRUE,
verbose = FALSE) {
verbose = FALSE,
timeout = 30) {

ua <- "https://github.com/ropensci/openalexR/"

Expand All @@ -335,7 +342,7 @@ oa_request <- function(query_url,
}

# first, download info about n. of items returned by the query
res <- api_request(query_url, ua, query = query_ls, api_key = api_key, parse = FALSE)
res <- api_request(query_url, ua, query = query_ls, api_key = api_key, parse = FALSE, timeout = timeout)
res_parsed <- jsonlite::fromJSON(res, simplifyVector = FALSE)
res_meta <- res_parsed$meta
if (parse) {
Expand Down Expand Up @@ -364,7 +371,7 @@ oa_request <- function(query_url,
if (verbose) cat("=")
Sys.sleep(1 / 10)
query_ls[[paging]] <- next_page
res <- api_request(query_url, ua, query = query_ls)
res <- api_request(query_url, ua, query = query_ls, timeout = timeout)
data <- c(data, res[[result_name]])
i <- i + 1
next_page <- get_next_page("cursor", i, res)
Expand Down Expand Up @@ -412,10 +419,10 @@ oa_request <- function(query_url,
query_ls[[paging]] <- next_page

if (parse) {
res <- api_request(query_url, ua, query = query_ls, parse = TRUE)
res <- api_request(query_url, ua, query = query_ls, parse = TRUE, timeout = timeout)
if (!is.null(res[[result_name]])) data[[i]] <- res[[result_name]]
} else {
raw <- api_request(query_url, ua, query = query_ls, parse = FALSE)
raw <- api_request(query_url, ua, query = query_ls, parse = FALSE, timeout = timeout)
data[[i]] <- raw
}
}
Expand Down Expand Up @@ -708,9 +715,10 @@ oa_random <- function(entity = oa_entities(),
final_res
}

api_request <- function(query_url, ua, query, api_key = oa_apikey(), parse = TRUE) {
api_request <- function(query_url, ua, query, api_key = oa_apikey(), parse = TRUE, timeout=30) {
my_req_url_query <- \(x,y) do.call(\(...) httr2::req_url_query(x,...), y)
res <- httr2::request(query_url) |>
httr2::req_timeout(timeout) |>
httr2::req_user_agent(ua) |>
my_req_url_query(query) |>
httr2::req_headers(api_key = api_key) |>
Expand Down

0 comments on commit a9e69ff

Please sign in to comment.