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 my_app import router
from my_app.response_models import User
from fastapi_utk import Paginated, Pagination, Paginator
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(["age", "name"], default=["-age"])],
) -> list[User]:
print(sort_by)
# [
# SortingOption(field="baz", is_desc=False),
# SortingOption(field="baz_baz", is_desc=True),
# ]
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, for example `my.api/path?sort=bar,-pop`
default=["-bar"], # default sorting return (if not set by request params)
delimiter=",", # sorting keys delimiter
url_query_param_name="sort" # query param name to set sorting
)
],
) -> ...:
...
# /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.3.tar.gz
(22.2 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.3.tar.gz.
File metadata
- Download URL: fastapi_utk-1.0.3.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.29
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
985716bd8236e9351a8883ead7d4705317fad79e0866266d3796951e936ac4aa
|
|
| MD5 |
125eb2adb346a7bdfaa9d9c4ae8f40b6
|
|
| BLAKE2b-256 |
b868f31bd7ad7d74c3ea224d2df855be7740acf5a3d837b0528310218b1f664f
|
File details
Details for the file fastapi_utk-1.0.3-py3-none-any.whl.
File metadata
- Download URL: fastapi_utk-1.0.3-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 |
d0ad1c76d3b3db9b6f45c70a57b7d7bbcbc2c1fb1b01b8454f22a8800543530e
|
|
| MD5 |
449c3e9f3b2e709f568b6606e71ec6a0
|
|
| BLAKE2b-256 |
50f0e886db9d08ef910035f7469217327f41aca7bd24079a0c677ffa7cf8a345
|