Skip to main content

A module containing helpers for paginating responses with FastAPI

Project description

FastAPI Pagination Utilities

This Python package contains utilities to aid in paginating responses from FastAPI applications.

Use

Install fastapi-pagination-utilities using pip:

pip install fastapi-pagination-utilities

The module can then be used as fastapi_pagination:

from fastapi import FastAPI
from fastapi_pagination import (
    CURSOR_QUERY, PAGE_SIZE_QUERY, PaginationDetails, PaginatedResults
)
from pydantic import BaseModel

app = FastAPI()

class Widget(BaseModel):
    name: str

@app.get(
    '/widgets',
    summary="List widgets",
    description="List all the widgers which are available."
    response_model=PaginatedResults[Widget]
)
def list_widgets(
        request: Request,
        cursor: Optional[str] = CURSOR_QUERY,
        page_size: Optional[int] = PAGE_SIZE_QUERY):

    # Get pagination.
    pagination = PaginationDetails(cursor, page_size)

    # list_widgets() should take a page size and offset and return a list of
    # results.
    results, has_more = list_widgets(pagination.offset, pagination.page_size)

    return PaginatedResults(
        next=(
            str(request.url.include_query_params(cursor=pagination.next_cursor()))
            if has_more else None
        ),
        previous=(
            str(request.url.include_query_params(cursor=pagination.previous_cursor()))
            if pagination.previous_cursor() else None
        ),
        results=results
    )

Developer quickstart

This project contains a dockerized testing environment which wraps tox.

Tests can be run using the ./test.sh command:

# Run all PyTest tests and Flake8 checks
$ ./test.sh

# Run just PyTest
$ ./test.sh -e py3

# Run a single test file within PyTest
$ ./test.sh -e py3 -- tests/test_identifiers.py

# Run a single test file within PyTest with verbose logging
$ ./test.sh -e py3 -- tests/test_identifiers.py -vvv

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-pagination-utilities-0.9.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file fastapi-pagination-utilities-0.9.1.tar.gz.

File metadata

File hashes

Hashes for fastapi-pagination-utilities-0.9.1.tar.gz
Algorithm Hash digest
SHA256 bc00ded2b05f5e0f6baf71a35b15623514443e4e5d5f9c5d3c8782f2d8c0f46f
MD5 c9c1fea7942b74cacaba6c2826a94ee7
BLAKE2b-256 c53fbaefc355fafd85f5ea074e62881faca10146d81dd80fcf7fed718eb17b11

See more details on using hashes here.

File details

Details for the file fastapi_pagination_utilities-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_pagination_utilities-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 946ef9a70794a2529815fae14e2667ff6d93da3d5944ef0766cdbd1ef82990eb
MD5 5ebba97d34574b0ba272d14237809740
BLAKE2b-256 d08560ec60c3e1886d7af3147e6210b40361377a5e829205929872d2c9074e8e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page