Skip to content

Commit

Permalink
Accounts for 503 Error (#206)
Browse files Browse the repository at this point in the history
* closes #204
  • Loading branch information
trangdata authored Feb 13, 2024
1 parent 52aed87 commit 097eb82
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions R/oa_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -625,28 +625,27 @@ oa_random <- function(entity = oa_entities(),
api_request <- function(query_url, ua, query = query, api_key = oa_apikey()) {
res <- httr::GET(query_url, ua, query = query, httr::add_headers(api_key = api_key))

if (httr::status_code(res) == 200) {
if (httr::http_type(res) != "application/json") {
stop("API did not return json", call. = FALSE)
}

data <- jsonlite::fromJSON(
httr::content(res, as = "text", encoding = "utf-8"),
simplifyVector = FALSE
)

return(data)
}

if (httr::status_code(res) == 429) {
message("HTTP status 429 Too Many Requests")
return(list())
}

parsed <- jsonlite::fromJSON(
httr::content(res, "text", encoding = "UTF-8"),
simplifyVector = FALSE
)
m <- httr::content(res, "text", encoding = "UTF-8")

if (httr::status_code(res) == 503) {
mssg <- regmatches(m, regexpr("(?<=<title>).*?(?=<\\/title>)", m, perl = TRUE))
message(mssg, ". Please try setting `per_page = 25` in your function call!")
return(list())
}

parsed <- jsonlite::fromJSON(m, simplifyVector = FALSE)

if (httr::status_code(res) == 200) {
if (httr::http_type(res) != "application/json") {
stop("API did not return json", call. = FALSE)
}
return(parsed)
}

if (httr::http_error(res)) {
stop(
Expand Down

0 comments on commit 097eb82

Please sign in to comment.