FastAPI Class Views and utilities
Project description
fastapi-views
FastAPI Class Views and utilities
Documentation: https://asynq-io.github.io/fastapi-views/
Repository: https://github.com/asynq-io/fastapi-views
Installation
pip install fastapi-views
Optional dependencies
Avaliable extensions: uvloop, prometheus, uvicorn, opentelemetry, cli
pip install 'fastapi-views[all]'
Usage
from typing import ClassVar, Optional
from uuid import UUID
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi_views import ViewRouter, configure_app
from fastapi_views.views.viewsets import AsyncAPIViewSet
class UpdateItemSchema(BaseModel):
name: str
price: int
class ItemSchema(BaseModel):
id: UUID
name: str
price: int
class MyViewSet(AsyncAPIViewSet):
api_component_name = "Item"
response_schema = ItemSchema
items: ClassVar[dict[UUID, ItemSchema]] = {}
async def list(self) -> list[ItemSchema]:
return list(self.items.values())
async def create(self, item: ItemSchema) -> ItemSchema:
self.items[item.id] = item
return item
async def retrieve(self, id: UUID) -> Optional[ItemSchema]:
return self.items.get(id)
async def update(self, id: UUID, item: UpdateItemSchema) -> ItemSchema:
self.items[id] = ItemSchema(id=id, name=item.name, price=item.price)
return self.items[id]
async def destroy(self, id: UUID) -> None:
self.items.pop(id, None)
# Generics viewsets generate crud automatically using repository pattern
# for more complete example check out examples/generics
class ItemGenericViewSet(AsyncGenericViewSet):
api_component_name = "Item"
primary_key = ItemId
response_schema = Item
create_schema = UpdateItemSchema
update_schema = UpdateItemSchema
partial_update_schema = UpdateItemSchema
filter = None
repository = ItemRepository() # to implement
router = ViewRouter(prefix="/items")
router.register_view(MyViewSet)
app = FastAPI(title="My API")
app.include_router(router)
configure_app(app)
Features
- Class Based Views
- APIViews
- ViewSets
- Generic Views & ViewSets
- Both async and sync function support
- No dependencies on ORM
- OpenAPI operation id simplification
- 'Smart' and fast serialization using Pydantic v2
- Http Problem Details implementation (both models & exception classes)
- Automatic prometheus metrics exporter
- Optional Opentelemetry instrumentation with
correlation_idin error responses - CLI for generating OpenAPI documentation file
- Pagination types & schemas
- Advanced drf-like filters
- Server Side Events
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastapi_views-1.5.1.tar.gz
(21.6 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_views-1.5.1.tar.gz.
File metadata
- Download URL: fastapi_views-1.5.1.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1bca4a163a0d1f205822bbdedc1aeac5e9e8f22d58e18cd44043e3c0fde7e6a
|
|
| MD5 |
bb7365a1853ad5efb6382c0266682366
|
|
| BLAKE2b-256 |
570a462e9079abf7e57dbbd0ee17f4759fb8fc648cf1106e8b6c08bc1943073c
|
File details
Details for the file fastapi_views-1.5.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_views-1.5.1-py3-none-any.whl
- Upload date:
- Size: 30.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb39fbcbcc8ecd8392ea700976596e4038d64a39d23aac92e693abd4ff42e1ef
|
|
| MD5 |
bcbd4c05f65ca768b7b5ee84dc1587bf
|
|
| BLAKE2b-256 |
c74564fbd4d5cc44c71d917abd7cffa899aac88b1db8ceeb0479bb21099e20d5
|