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.4.0.tar.gz (21.3 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.4.0-py3-none-any.whl (30.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fastapi_views-1.4.0.tar.gz
Algorithm Hash digest
SHA256 761e366d8be12ec4be6598e22bff5cac73c223b56b0f6bc6fdab0331c896e8db
MD5 a7b2f69833b87344a21060e4251ef235
BLAKE2b-256 b626c41bd8c21070fc7826387c2e9eba1dbe350402428a2ab4f8eb5c63489d68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_views-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a13c498a3be3e23bfa48efda526d9624f6c2cbc192ed355c2f7dd1ecc9367f10
MD5 ec0626c054b71246b898880fcbff91ba
BLAKE2b-256 9beb2189f49b4642c26b809baf82356f403e0dd9586c6b0bcd3151e6b79f00f1

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