Skip to contents

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 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).

accumulator

List; internal accumulator for recursive aggregation. Defaults to an empty list.

Value

Promise resolving to the aggregated result as defined by aggregate_fn.

Details

Workflow Overview

  1. Fetch Page: Calls fetch_page to retrieve the current page.

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

  3. Recursive Continuation: Recursively fetches the next page if the current page is less than the total pages and max_pages allows, updating the query.

  4. Aggregate Results: Applies aggregate_fn to the accumulator when pagination completes.

API Endpoint

Not applicable (helper function for paginated endpoints).

Usage

Utilised as a legacy alternative to auto_paginate for retrieving multi-page data from KuCoin API responses.

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