if (FALSE) {
# demos the two methods for the same function.
# The example is for the Google Analytics management API,
# you need to authenticate with that to run them.
# paging by using nextLink that is returned in API response
ga_segment_list1 <- function(){
# this URL will be modified by using the url_override argument in the generated function
segs <- gar_api_generator("https://www.googleapis.com/analytics/v3/management/segments",
"GET",
pars_args = list("max-results"=10),
data_parse_function = function(x) x)
gar_api_page(segs,
page_method = "url",
page_f = function(x) x$nextLink)
}
# paging by looking for the next start-index parameter
## start by creating the function that will output the correct start-index
paging_function <- function(x){
next_entry <- x$startIndex + x$itemsPerPage
# we have all results e.g. 1001 > 1000
if(next_entry > x$totalResults){
return(NULL)
}
next_entry
}
## remember to add the paging argument (start-index) to the generated function too,
## so it can be modified.
ga_segment_list2 <- function(){
segs <- gar_api_generator("https://www.googleapis.com/analytics/v3/management/segments",
"GET",
pars_args = list("start-index" = 1,
"max-results"=10),
data_parse_function = function(x) x)
gar_api_page(segs,
page_method = "param",
page_f = paging_function,
page_arg = "start-index")
}
identical(ga_segment_list1(), ga_segment_list2())
}
Run the code above in your browser using DataLab