https://github.com/asynq-io/fastapi-views
FastAPI Class Views and utilities
https://github.com/asynq-io/fastapi-views
api asyncio fastapi python rest
Last synced: 2 months ago
JSON representation
FastAPI Class Views and utilities
- Host: GitHub
- URL: https://github.com/asynq-io/fastapi-views
- Owner: asynq-io
- License: apache-2.0
- Created: 2024-07-16T13:00:06.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-09-30T03:39:36.000Z (9 months ago)
- Last Synced: 2024-10-03T10:47:26.641Z (9 months ago)
- Topics: api, asyncio, fastapi, python, rest
- Language: Python
- Homepage: https://asynq-io.github.io/fastapi-views/
- Size: 975 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastapi-views




[](https://github.com/charliermarsh/ruff)
[](https://docs.pydantic.dev/latest/contributing/#badges)
[](https://github.com/PyCQA/bandit)


*FastAPI Class Views and utilities*
---
Documentation: https://asynq-io.github.io/fastapi-views/Repository: https://github.com/asynq-io/fastapi-views
---
## Installation
```shell
pip install fastapi-views
```## Usage
```python
from typing import ClassVar, Optional
from uuid import UUIDfrom fastapi import FastAPI
from pydantic import BaseModelfrom fastapi_views import ViewRouter, configure_app
from fastapi_views.views.viewsets import AsyncAPIViewSetclass UpdateItemSchema(BaseModel):
name: str
price: intclass ItemSchema(BaseModel):
id: UUID
name: str
price: intclass 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 itemasync 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)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
- 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_id` in error responses
- CLI for generating OpenAPI documentation file
- Pagination types & schemas