Skip to main content

Class based view for FastAPI

Project description

Class based view in FastApi

This package gives you convenient access for writing and maintaining class based controllers in FastAPI framework

Install package

pip install fastapi-simple-class-view

Quickstart

Typical app structure

An example of a typical application structure, you can redefine it at your discretion

.
├── app
│   ├── __init__.py
│   ├── permissions.py
│   ├── scheme.py
│   ├── service.py
│   ├── urls.py
│   └── view.py

view.py

Simple writing of CRUD operation for user model
It includes:

  • permissions - Dict (checking for rights to a specific endpoint)
  • py_model - Dict (the specific Pydantic schema to be used in the response or request)
  • service - service class object (the service that will be used to make requests to the database)
  • slug_field_type - Type (the type of slug field, used for the correct operation of the swagger)
from fastapi_simple_class_view.base import APIView
from fastapi_simple_class_view.mixins import GenericView
from collections import defaultdict

from .permissions import is_superuser, is_customer
from .scheme import UserSchema, UserCreateUpdate
from .service import user_service


class UsersView(GenericView, APIView):
    py_model = defaultdict(lambda: UserSchema, {
        'create': UserCreateUpdate,
        'update': UserCreateUpdate,
    })
    permissions = defaultdict(lambda: is_superuser, {
        'list': is_customer,
    })
    service = user_service
    slug_field_type = int

urls.py

from example.app.view import UsersView
from fastapi_simple_class_view.controller import APIController

app_router = APIController()

app_router.controller_register('/users/', UsersView())

service.py

Service that uses the sqlalchemy model

from example.database import UsersModel, Database
from fastapi_simple_class_view.base import BaseService


class UserService(BaseService):
    model = UsersModel


user_service = UserService(Database().session)

Result

Compact recording will allow you to automatically create crud operations with the model

alt text

Detailed view of the remaining application files

permissions.py

Simple example is the creation of permits based on class HttpBearer

from dataclasses import dataclass
from fastapi import Depends
from fastapi.security import HTTPBearer


@dataclass
class User:
    username: str
    is_superuser: bool


def is_superuser(creds=Depends(HTTPBearer())):
    """
    Check SuperUser by credentials
    :param creds:
    :return: UserModel
    """
    return User('admin', True)

scheme.py

Pydantic Scheme

from pydantic import BaseModel


class UserSchema(BaseModel):
    id: int
    username: str

SqlAlchemy model example

class UsersModel(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(VARCHAR(50))
    first_name = Column(VARCHAR(100))
    last_name = Column(VARCHAR(100))

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

fastapi_simple_class_view-0.0.2.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distribution

fastapi_simple_class_view-0.0.2-py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 3

Supported by

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