Utilities for building REST APIs with FastAPI
Project description
FastAPI REST Utils
A collection of utilities for building REST APIs with FastAPI, providing viewset-based API development with SQLAlchemy integration.
🚀 Features
- ViewSet Base Classes: Abstract base classes for building RESTful viewsets with full CRUD operations
- SQLAlchemy Integration: Built-in support for SQLAlchemy models with async/await patterns
- Router Utilities: Extended APIRouter with viewset registration capabilities
- Dependency Injection: Common dependency injection patterns for FastAPI
- Type Safety: Full type hints and Pydantic integration
📦 Installation
pip install fastapi-rest-utils
🏃♂️ Quick Start
from fastapi import FastAPI, Depends
from fastapi_rest_utils.viewsets.sqlalchemy import ModelViewSet
from fastapi_rest_utils.router import RestRouter
from fastapi_rest_utils.deps import db_dep_injector
from pydantic import BaseModel
# Define schemas
class ProductBase(BaseModel):
name: str
price: float
class ProductResponse(ProductBase):
id: int
# Define SQLAlchemy model
class Product(Base):
__tablename__ = "products"
id = Column(Integer, primary_key=True)
name = Column(String)
price = Column(Float)
# Create viewset
class ProductViewSet(ModelViewSet):
model = Product
schema_config = {
"list": {"response": list[ProductResponse]},
"retrieve": {"response": ProductResponse},
"create": {"payload": ProductBase, "response": ProductResponse},
"update": {"payload": ProductBase, "response": ProductResponse},
}
# Register with router
app = FastAPI()
router = RestRouter()
router.register_viewset(
viewset=ProductViewSet,
prefix="/products",
tags=["products"],
dependencies=[Depends(db_dep_injector(get_async_session))]
)
app.include_router(router)
📚 Core Concepts
ViewSets
ViewSets combine multiple views (list, retrieve, create, update, delete) into a single class and automatically generate routes.
Available Views
- ListView: GET requests to list all objects
- RetrieveView: GET requests to retrieve a single object
- CreateView: POST requests to create new objects
- UpdateView: PUT requests to update objects
- DeleteView: DELETE requests to remove objects
Schema Configuration
schema_config = {
"list": {"response": list[ProductResponse]},
"retrieve": {"response": ProductResponse},
"create": {"payload": ProductCreate, "response": ProductResponse},
"update": {"payload": ProductUpdate, "response": ProductResponse},
}
Router Registration
router = RestRouter()
router.register_viewset(
viewset=ProductViewSet,
prefix="/products",
tags=["products"],
dependencies=[Depends(db_dep_injector(get_async_session))]
)
🔧 API Reference
ModelViewSet
Complete CRUD viewset for SQLAlchemy models.
Required Attributes:
model: The SQLAlchemy model classschema_config: Dictionary defining request/response schemasdependency: List of callable dependencies
Overridable Methods:
get_objects(request, *args, **kwargs): Customize list query logicget_object(request, id, *args, **kwargs): Customize single object retrievalcreate_object(request, payload, *args, **kwargs): Customize object creationupdate_object(request, id, payload, *args, **kwargs): Customize object updatesdelete_object(request, id, *args, **kwargs): Customize object deletion
Dependency Utilities
# Inject database session into request.state.db
dependencies=[Depends(db_dep_injector(get_async_session))]
# Inject authenticated user into request.state.user
dependencies=[Depends(auth_dep_injector(current_active_user))]
🧪 Testing
pytest
📋 Requirements
- Python 3.8+
- FastAPI >= 0.100.0
- Pydantic >= 2.0.0
- SQLAlchemy >= 2.0.0
🔮 Future Plans
- Pagination: Cursor-based and offset-based pagination
- Filtration: Query parameter filtering and complex filter expressions
- Ordering: Multi-field sorting with configurable defaults
- Bulk Operations: Batch create, update, and delete operations
- Field Selection: Allow clients to specify which fields to include/exclude
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
📄 License
MIT License - see the LICENSE file for details.
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
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_rest_utils-0.1.1.tar.gz.
File metadata
- Download URL: fastapi_rest_utils-0.1.1.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d88c3fdb35cb506051e9c58b95be6ac8e22465c6b453c3928007daccd3b7d29
|
|
| MD5 |
81a4c4237cb142dc4e54372dffc1f30b
|
|
| BLAKE2b-256 |
a12cd98673a05d43b3af72cdb3ff48c2f0acc1228e9a234e469f8e9676f90566
|
Provenance
The following attestation bundles were made for fastapi_rest_utils-0.1.1.tar.gz:
Publisher:
publish.yml on Mdanic75/fastapi_rest_utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_rest_utils-0.1.1.tar.gz -
Subject digest:
7d88c3fdb35cb506051e9c58b95be6ac8e22465c6b453c3928007daccd3b7d29 - Sigstore transparency entry: 348850452
- Sigstore integration time:
-
Permalink:
Mdanic75/fastapi_rest_utils@1784ebc360c1171017a9c43cd78c9e8b488819c7 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Mdanic75
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1784ebc360c1171017a9c43cd78c9e8b488819c7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_rest_utils-0.1.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_rest_utils-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83acc64dcb4ee59dc1720abd98c1503f52e09d57ade8c0daed1b5f5354890531
|
|
| MD5 |
a7cdf1330e14b5b8c01137ec20bf650d
|
|
| BLAKE2b-256 |
bff477072d9c8896dbb30130e5cc752fe9c9c8fcb1707c1aa587ca7f1da4d9d0
|
Provenance
The following attestation bundles were made for fastapi_rest_utils-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on Mdanic75/fastapi_rest_utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_rest_utils-0.1.1-py3-none-any.whl -
Subject digest:
83acc64dcb4ee59dc1720abd98c1503f52e09d57ade8c0daed1b5f5354890531 - Sigstore transparency entry: 348850476
- Sigstore integration time:
-
Permalink:
Mdanic75/fastapi_rest_utils@1784ebc360c1171017a9c43cd78c9e8b488819c7 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Mdanic75
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1784ebc360c1171017a9c43cd78c9e8b488819c7 -
Trigger Event:
push
-
Statement type: