Skip to main content

FastAPI Ultimate Toolkit

Project description

FastAPI Ultimate Toolkit

The most useful tools for any FastAPI project

Installation

pip install fastapi-utk

Features

  • Api
    • Pagination
      • Pagination
      • PaginationConfig
      • Paginator
      • Paginated
  • Utils
    • NotSet

Use cases

>>> CLICK THE LINK TO SEE EXAMPLE PROJECT <<<

Pagination

Example

import typing as tp
from fastapi_utk import Paginated, Pagination, Paginator

from my_app import router
from my_app.response_models import User

pagination = Pagination()


@router.get("/users")
def get_users(
        paginator: tp.Annotated[Paginator, pagination.Depends()],
) -> Paginated[User]:
    total, users = get_users_from_db(..., limit=paginator.limit, offset=paginator.offset)

    return paginator(
        [
            User(
                id=user.id,
                age=user.age,
                name=user.name,
            )
            for user in users
        ],
        total=total,
    )

Response

Schema

Extra

import typing as tp
from fastapi_utk import Paginated, Pagination, Paginator

# Use Pagination class to specify global pagination configuration
pagination = Pagination()


# If for some routes you need non-default configuration, set it right in depends
@router.get("foos")
def foo(
        paginator: tp.Annotated[
            Paginator,
            pagination.Depends(
                default_page=1,  # default page number if query param is not set
                default_page_size=10,  # default page size if query param is not set
                max_page_size=100,  # maximum page size
                url_page_query_param_name="fooPage",  # query param name to set a page number
                url_page_size_query_param_name="fooPageSize",
                # query param name to set page size, set `None` to disable this option
            )
        ]
) -> Paginated[MyModel]:  # use Paginated[...] to warp collection response 
    # ...

    return paginator(..., total=...)  # total is used to calculate amount of pages

# /users?fooPage=1&fooPageSize=100

Sorting

Example

import typing as tp
from fastapi_utk import Sorting, SortingOption

from my_app import router
from my_app.response_models import User

sorting = Sorting()


@router.get("/users")
def get_users(
        sort_by: tp.Annotated[
            list[SortingOption],
            sorting.Depends(
                choices=["age", "name"],
                default=["-age"]
            )
        ],
) -> list[User]:
    total, users = get_users_from_db(..., _sort_by=sort_by)

    return [
        User(
            id=user.id,
            age=user.age,
            name=user.name,
        )
        for user in users
    ]

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_utk-1.0.1.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_utk-1.0.1-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_utk-1.0.1.tar.gz.

File metadata

  • Download URL: fastapi_utk-1.0.1.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.29

File hashes

Hashes for fastapi_utk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9b33a944ef3016cdcf63576a6402637f6a5c886d90749b316db4e2b3b71eda3c
MD5 5b8107fc8c3086b324a4107ee6a72e3d
BLAKE2b-256 aacb0ce73f25eb417c275b71f4c31136dce2db67dcea8c3525e5bff2f5e9348a

See more details on using hashes here.

File details

Details for the file fastapi_utk-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_utk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 604019e61320608df338cf92260872538f79d2b96b029496302c0cda575917a2
MD5 66103962ca7c502cb1f8bb5c1407c225
BLAKE2b-256 27d0941529a1d1c961d36de799d64f2f2ddb3af48ba9a7e8968a80479680255f

See more details on using hashes here.

Supported by

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