NAME

HTTP::API::Core::Pagination - Iterate through HTTP API response pages

SYNOPSIS

my $pager = $api->paginate(
    '/users',
    mode      => 'page',
    page_size => 100,
    max_pages => 50,
);

my $items = $pager->all;

DESCRIPTION

Provides lazy, synchronous pagination for next_url, page, cursor, and offset APIs. Requests are issued only when more items are needed. The default mode is next_url. See HTTP::API::Core for the client interface.

METHODS

new

my $pager = HTTP::API::Core::Pagination->new(
    client => $api,
    path   => '/users',
    mode   => 'next_url',
    items  => 'data.items',
    next   => 'links.next',
);

Constructs a paginator without fetching a page. Normally use the client's paginate method. client and path are required.

items, next, has_more, and total accept dotted paths or code references. A code reference receives the decoded body; with response_aware_extractors enabled, it also receives the response object. The items extractor must return an array reference. An empty items path selects the whole decoded body.

query and request are hash references. Query options are applied when constructing page requests; subsequent next_url continuations are followed as returned. Request options are passed to the client on every page. Absolute HTTP(S) continuation URLs are restricted to the client's configured origin by default; allow_cross_origin explicitly opts out of that check.

page_size, when supplied, must be a positive integer. Page numbering starts at start_page (default 1); offset numbering starts at start_offset (default 0) and advances by the actual returned item count. For offset mode, an explicit total takes precedence over has_more. max_pages optionally limits the number of successfully validated pages. The default is unlimited. If another page is needed after that limit, an exception is raised before another request.

next

my $item = $pager->next;

Returns the next item, or undef at exhaustion. A decoded JSON null item also returns undef, so this interface cannot distinguish null from exhaustion. Use all if null items must be preserved.

all

my $items = $pager->all;
my @items = $pager->all;

Consumes the remaining items. Returns an array reference in scalar context or a list in list context. JSON null items are preserved as undef elements. Items already consumed through next are not returned again.

ERRORS

Malformed item or continuation metadata, repeated continuations and page limit violations raise exceptions. A page is committed only after its response and continuation have been validated; a failed page does not advance the iterator or consume its page allowance. A subsequent call may retry that page. Earlier successful pages remain consumed.

Continuation errors do not include the rejected URL or cursor value, which could contain sensitive data.

SEE ALSO

HTTP::API::Core