Skip to main content

FastAPI Class Views and utilities

Project description

fastapi-views

Tests Build License Mypy Ruff Pydantic v2 security: bandit Python Format PyPi

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_id in error responses
  • CLI for generating OpenAPI documentation file
  • Pagination types & schemas
  • Advanced drf-like filters
  • Server Side Events

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.0.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_views-1.5.0-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_views-1.5.0.tar.gz.

File metadata

  • Download URL: fastapi_views-1.5.0.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for fastapi_views-1.5.0.tar.gz
Algorithm Hash digest
SHA256 70a75d0cf0559819119520502d73a1e89d1e32e58dd9fb3f5209eaa298a021c4
MD5 ef00ac8b264ca5b29a086fcb13caf59f
BLAKE2b-256 9aba2a7adec539f0eef74dc0b5a2939291cb1cd5579f7d033df1376925cfa0e3

See more details on using hashes here.

File details

Details for the file fastapi_views-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_views-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ace3ad3d12b80eede15306671690ac32c60a3769df5fc9bce1a12a0a8717347b
MD5 65f04c7290bf0114f93a5df2815870eb
BLAKE2b-256 77d6f0be9d1dadda0c61c5236a09b819498f0b8248f83baa4d971e41e6b27b02

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page