Facilitate Automatic Pagination for KuCoin API Endpoints (Legacy Version)
Source:R/helpers_api.R
auto_paginate_old.Rd
Manages pagination for KuCoin API endpoints asynchronously using a recursive approach, fetching pages with a user-supplied function and aggregating results via a provided function. This is an older version of auto_paginate
.
Usage
auto_paginate_old(
fetch_page,
query = list(currentPage = 1, pageSize = 50),
items_field = "items",
paginate_fields = list(currentPage = "currentPage", totalPage = "totalPage"),
aggregate_fn = function(acc) {
acc
},
max_pages = Inf,
accumulator = list(),
.__coro_env_parent__ = <environment>
)
Arguments
- fetch_page
Function fetching a page of results, returning a promise resolving to the response.
- query
Named list of query parameters for the first page. Defaults to
list(currentPage = 1, pageSize = 50)
.- items_field
Character string; field in the response containing items to aggregate. Defaults to
"items"
.- paginate_fields
Named list specifying response fields for pagination:
currentPage
: Field with the current page number.totalPage
: Field with the total number of pages. Defaults tolist(currentPage = "currentPage", totalPage = "totalPage")
.
- aggregate_fn
Function combining accumulated results into the final output. Defaults to returning the accumulator list unchanged.
- max_pages
Numeric; maximum number of pages to fetch. Defaults to
Inf
(all available pages).- accumulator
List; internal accumulator for recursive aggregation. Defaults to an empty list.
Details
Workflow Overview
Fetch Page: Calls
fetch_page
to retrieve the current page.Accumulate Results: Adds items from the page (via
items_field
) to theaccumulator
.Recursive Continuation: Recursively fetches the next page if the current page is less than the total pages and
max_pages
allows, updating the query.Aggregate Results: Applies
aggregate_fn
to the accumulator when pagination completes.
Examples
if (FALSE) { # \dontrun{
fetch_page <- coro::async(function(query) {
url <- paste0(get_base_url(), "/api/v1/example", build_query(query))
response <- httr::GET(url, httr::timeout(3))
process_kucoin_response(response, url)
})
aggregate <- function(acc) data.table::rbindlist(acc)
main_async <- coro::async(function() {
result <- await(auto_paginate_old(
fetch_page = fetch_page,
query = list(currentPage = 1, pageSize = 10),
max_pages = 3,
aggregate_fn = aggregate
))
print(result)
})
main_async()
while (!later::loop_empty()) later::run_now()
} # }