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
- Sorting
- Sorting
- SortingOption
- SortingConfig
- Middlewares
- CamelCaseQueryParamsMiddleware
- OpenAPI
- translate_query_params_snake_to_camel
- Pagination
- 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("/foo")
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
# /foo?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
]
Extra
import typing as tp
from fastapi_utk import Sorting, SortingOption
# Use Sorting class to specify global pagination configuration
sorting = Sorting()
# If for some routes you need non-default configuration, set it right in depends
@router.get("/foo")
def foo(
sort_by: tp.Annotated[
list[SortingOption],
sorting.Depends(
choices=["bar", "baz_baz", "pop"], # allowed sorting keys
default=["-bar"], # default sorting
delimiter=",", # sorting keys delimiter
url_query_param_name="sort" # query param name to set sorting
)
],
) -> ...:
pritn(sort_by)
# [
# SortingOption(field="baz", is_desc=False),
# SortingOption(field="baz_baz", is_desc=True),
# ]
# /foo?sort=baz,-barBaz
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.2.tar.gz
(21.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_utk-1.0.2.tar.gz.
File metadata
- Download URL: fastapi_utk-1.0.2.tar.gz
- Upload date:
- Size: 21.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.29
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ef5ff07cc5f8bed436c1c790d651a9681f9d1527c337eb5c5654a85221ded20
|
|
| MD5 |
11fe3810ea590e4fd097c1657ba00b6f
|
|
| BLAKE2b-256 |
0c63eeeddde9877c59e8a3ea77dcd82e010df8eaae7ee9978bfac916a1b56279
|
File details
Details for the file fastapi_utk-1.0.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_utk-1.0.2-py3-none-any.whl
- Upload date:
- Size: 23.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.29
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13e9c53ef17c58d0139ab1265065a90cb2f10dc07a5d537739092e4a02529bae
|
|
| MD5 |
7fba79db82aba591cd66d36c7b616c8f
|
|
| BLAKE2b-256 |
6bdafba0c0fd330762e288f5fc1ecc840ac24ddf85c37f66534a3fb4d10daed2
|