Skip to content

Decorator for paged responses with page token #322

@santhoshramaraj

Description

@santhoshramaraj

Many server APIs interfacing with large data implement paging, where responses are returned in pages(batches) and a token is used to identify the current and next page to return.

Example, https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list

GET https://gmail.googleapis.com/gmail/v1/users/{userId}/messages

pageToken query parameter is used to retrieve a specific page of results in the list.

{
  "messages": [
    {
      object (Message)
    }
  ],
  "nextPageToken": string,
  "resultSizeEstimate": integer
}

In most cases, the user interfacing with the client wants the paged responses to be simplified to a generator hiding the token handling and batched queries to use in loops.

Describe the solution you'd like

class Gmail(Consumer):
    @paging(request_token = Query("pageToken"), init_value = None, response_token = "nextPageToken", response_data = "messages")
    @get("users/{userId}/messages")
    def get_user_messages(self, user_id: Path("userId"), ...) -> Iterator[Message]:
        """Lists the messages in the user's mailbox."""

Additional context
Several variants of paging exists based on the location of the page token and location of the underlying iterator data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions