Skip to contents

Handles pagination for KuCoin API endpoints asynchronously by iteratively fetching pages with a user-supplied function and aggregating results using a provided aggregation function.

Usage

auto_paginate(
  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,
  .__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 to list(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).

Value

Promise resolving to the aggregated result as defined by aggregate_fn.

Details

Workflow Overview

  1. Fetch Page: Calls fetch_page with current query parameters to retrieve a page.

  2. Accumulate Results: Adds items from the page (via items_field) to an accumulator list.

  3. Determine Continuation: Continues if the current page is less than the total pages and max_pages hasn’t been reached.

  4. Aggregate Results: Applies aggregate_fn to the accumulator once all pages are fetched.

API Endpoint

Not applicable (helper function for paginated endpoints).

Usage

Utilised to simplify retrieval of multi-page data from KuCoin API responses, aggregating results into a user-defined format.

Official Documentation

Not directly tied to a specific endpoint; see KuCoin API pagination guidelines.

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(
    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()
} # }