Skip to content

Commit

Permalink
Respect backoff parameter for throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Robinson committed Jul 9, 2015
1 parent 0d75a0f commit 3708582
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ stack_parse <- function(req) {
# add metadata as an attribute
attr(items, "metadata") <- j[-1]

if (!is.null(j[-1]$backoff)) {
message("Response has backoff parameter: must wait ",
j[-1]$backoff, " seconds before performing same method")
}

items
}

Expand Down Expand Up @@ -72,7 +77,13 @@ stack_GET <- function(path, site = "stackoverflow", page = 1, num_pages = 1, ...
tbl <- stack_parse(req)
tbls <- c(tbls, list(tbl))

if (!attr(tbl, "metadata")$has_more) {
metadata <- attr(tbl, "metadata")

if (!is.null(metadata$backoff)) {
Sys.sleep(metadata$backoff)
}

if (!metadata$has_more) {
# finished pagination, can quit
break
}
Expand Down

0 comments on commit 3708582

Please sign in to comment.