Skip to content

Paginate

async_boto.utils.paginate

T module-attribute

T = TypeVar('T', bound=BaseModel)

paginate async

paginate(client, method, request, pagination_response_key='LastEvaluatedKey', pagination_query_key=None)

Generic paginator function for DynamoDB client methods.

PARAMETER DESCRIPTION
client

The DynamoDB client instance.

TYPE: Any

method

The name of the client method to call.

TYPE: str

request

The request model instance.

TYPE: BaseModel

pagination_response_key

The key in the response that indicates the pagination token, by default "LastEvaluatedKey".

TYPE: str DEFAULT: 'LastEvaluatedKey'

pagination_query_key

The key in the request that indicates the pagination token, by default None. If not given, it will be the same as pagination_response_key.

TYPE: str DEFAULT: None

YIELDS DESCRIPTION
T

The response model instance for each page.

Source code in async_boto/utils/paginate.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
async def paginate(
    client: Any,
    method: str,
    request: BaseModel,
    pagination_response_key: str = "LastEvaluatedKey",
    pagination_query_key: str = None,
) -> AsyncGenerator[T, None]:
    """
    Generic paginator function for DynamoDB client methods.

    Parameters
    ----------
    client : Any
        The DynamoDB client instance.
    method : str
        The name of the client method to call.
    request : BaseModel
        The request model instance.
    pagination_response_key : str, optional
        The key in the response that indicates the pagination token,
        by default "LastEvaluatedKey".
    pagination_query_key : str, optional
        The key in the request that indicates the pagination token, by default None.
        If not given, it will be the same as pagination_response_key.

    Yields
    ------
    T
        The response model instance for each page.
    """
    pagination_query_key = pagination_query_key or pagination_response_key

    while True:
        response = await getattr(client, method)(request)
        response_data = response.model_dump()
        yield response

        if (
            pagination_response_key in response_data
            and response_data[pagination_response_key]
        ):
            setattr(
                request, pagination_query_key, response_data[pagination_response_key]
            )
        else:
            break