Skip to content

Commit

Permalink
add some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
danymukesha committed Sep 26, 2024
1 parent a6f3c6c commit 1c6328c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
11 changes: 10 additions & 1 deletion R/gets.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ get <- function(res, ..., .headers = req_headers(), rate = 15/60) { # for get th
requests <- reqs(res, ..., .headers = .headers)
requests <- purrr::map(requests, httr2::req_throttle, rate = rate)
responses <- httr2::req_perform_parallel(requests)
httr2::throttle_status()

#checking the rate limit exceptions and add delay if needed
for (i in seq_along(responses)) {
if (httr2::resp_status(responses[[i]]) == 429) {
status_code <- httr2::resp_status(responses[[i]]) # I am not sure whether the error will arrive here
if (status_code == 200) {
break
} else if (status_code == 429) {
#the `Retry-After` in the response_headers will only show up once you exceed the rate limit
retry_after <- as.numeric(httr2::resp_headers(responses[[i]])$`Retry-After`)
message(glue::glue("Rate limit reached, waiting {retry_after} seconds before retrying..."))
Sys.sleep(retry_after)
responses[[i]] <- httr2::req_perform(requests[[i]])
} else {
warning(glue("Request failed with status code {status_code}.
Moving on to the next request."))
break
}
}
return(responses)
Expand Down
16 changes: 15 additions & 1 deletion R/posts.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,38 @@
#' @param .headers An S3 list with class `ensemblr_req_hdr`. Use the helper
#' [req_headers()] to create such an object.
#' @param rate The maximum number of requests per second to allow.
#' on a request (default: 5).
#' Defaults to 15 per minute (15/60).
#'
#' @return A list of responses, one for each request made.
#'
#' @keywords internal
post <- function(res, ..., .headers = req_headers(), .body, rate = 15/60) { # for post the body could be mandatory?
if (missing(.body)) stop("The '.body' parameter is required for POST requests.")
requests <- reqs(res, ..., .headers = .headers, .body = .body)
requests <- purrr::map(requests, httr2::req_throttle, rate = rate)
responses <- purrr::map(requests, function(req) {
req |> httr2::req_method("POST") |>
httr2::req_perform()
})
httr2::throttle_status()

for (i in seq_along(responses)) {
if (httr2::resp_status(responses[[i]]) == 429) {
status_code <- httr2::resp_status(responses[[i]]) # I am not sure whether the error will arrive here
if (status_code == 200) {
break
}
else if (status_code == 429) {
#the `Retry-After` in the response_headers will only show up once you exceed the rate limit
retry_after <- as.numeric(httr2::resp_headers(responses[[i]])$`Retry-After`)
message(glue::glue("Rate limit reached, waiting {retry_after} seconds
before retrying..."))
Sys.sleep(retry_after)
responses[[i]] <- httr2::req_perform(requests[[i]])
} else {
warning(glue("Request failed with status code {status_code}.
Moving on to the next request."))
break
}
}

Expand Down
1 change: 1 addition & 0 deletions man/post.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1c6328c

Please sign in to comment.