Skip to content

Commit

Permalink
add offset parameter to .quick_fetch() calls to address rladies#55
Browse files Browse the repository at this point in the history
  • Loading branch information
benubah committed Jul 4, 2019
1 parent b0dc11a commit 602ab59
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
.quick_fetch <- function(api_url,
api_key = NULL,
event_status = NULL,
offset = 0,
...) {

# list of parameters
parameters <- list(key = api_key, # your api_key
status = event_status, # you need to add the status
# otherwise it will get only the upcoming event
offset = offset,
... # other parameters
)

Expand Down Expand Up @@ -44,6 +46,7 @@
res <- .quick_fetch(api_url = api_url,
api_key = api_key,
event_status = event_status,
offset = 0,
...)

# Total number of records matching the query
Expand All @@ -52,22 +55,22 @@
records <- res$result
cat(paste("Downloading", total_records, "record(s)..."))

# If you have not yet retrieved all records, calculate the # of remaining calls required
extra_calls <- ifelse(
(length(records) < total_records) & !is.null(res$headers$link),
floor(total_records/length(records)),
0)
if (extra_calls > 0) {
if((length(records) < total_records) & !is.null(res$headers$link)){

# calculate number of offsets for records above 200
offsetn <- ceiling(total_records/length(records))
all_records <- list(records)
for (i in seq(extra_calls)) {
# Keep making API requests with an increasing offset value until you get all the records
# TO DO: clean this strsplit up or replace with regex

next_url <- strsplit(strsplit(res$headers$link, split = "<")[[1]][2], split = ">")[[1]][1]
res <- .quick_fetch(next_url, api_key, event_status)
for(i in 1:(offsetn - 1)) {
res <- .quick_fetch(api_url = api_url,
api_key = api_key,
event_status = event_status,
offset = i,
...)
all_records[[i + 1]] <- res$result
}
records <- unlist(all_records, recursive = FALSE)

}

return(records)
Expand Down

0 comments on commit 602ab59

Please sign in to comment.