paginateWithCursor

Fetches and aggregates all pages of a paginated API resource using a cursor.

This function repeatedly calls an endpoint, using the x-aptos-cursor header to request the next page. This operation is atomic: it either successfully retrieves all pages or fails entirely if any step encounters an error.

Usage

// Define the data class for the items being paginated
@Serializable
data class MyEvent(val sequence_number: String, val data: MyEventData)

suspend fun fetchAllEvents() {
val options = RequestOptions.AptosRequestOptions(...)

// Make the call, specifying the list item's type
val result: Result<List<MyEvent>, AptosException> = aptos.paginateWithCursor(options)

// Handle the result
when (result) {
is Ok -> println("Successfully fetched ${result.value.size} events in total.")
is Err -> println("Failed to fetch events: ${result.error.message}")
}
}

Return

A Result object:

  • Ok<List<T>>: On success, contains a single, flat list of all items T from all pages combined.

  • Err<AptosException>: On failure, contains an error from the first failed step.

Parameters

T

The data class type for the individual items within the paginated list.

options

The initial request configuration. The function will modify the params for subsequent page requests.