Skip to content

Commit

Permalink
Add pause to handle rate limit when reached
Browse files Browse the repository at this point in the history
  • Loading branch information
hgoers committed May 12, 2023
1 parent 2bc90b8 commit 106016f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 14 additions & 3 deletions R/ct_perform_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,29 @@
ct_perform_request <- function(req, requests_per_second = 10 / 60, verbose = FALSE) {

if (verbose) {
cli::cli_inform(c("i" = "Performing request, which can take a few seconds, depending on the amount of data queried"))
cli::cli_inform(c("i" = "Performing request, which can take a few seconds, depending on the amount of data queried."))
}

comtrade_is_transient <- function(resp) {
httr2::resp_status(resp) == 403 &&
httr2::resp_header(resp, "Retry-After") != "0"
}

comtrade_after <- function(resp) {
time <- as.numeric(httr2::resp_header(resp, "Retry-After"))
}

resp <- req |>
httr2::req_error(body = comtrade_error_body) |>
httr2::req_throttle(rate = requests_per_second) |>
httr2::req_retry(max_tries = 5) |>
httr2::req_retry(is_transient = comtrade_is_transient,
after = comtrade_after) |>
httr2::req_perform()

if (verbose) {
cli::cli_inform(c("v" = "Got a response object from Comtrade. Use `process = F` if there is an error after this step to find issues with the response object."))
cli::cli_inform(c("v" = "Got a response object from UN Comtrade. Use `process = F` if there is an error after this step to find issues with the response object."))
}

return(resp)
}

Expand Down
2 changes: 2 additions & 0 deletions man/comtradr-package.Rd

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

0 comments on commit 106016f

Please sign in to comment.