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

Uploaded Source

Built Distribution

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

fastapi_simple_class_view-0.0.3-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_simple_class_view-0.0.3.tar.gz.

File metadata

File hashes

Hashes for fastapi_simple_class_view-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b903c57949eb234707da7f33553ed26c42ee5d6637deea3e713c33a59eb73439
MD5 420a756fbe16baaecfdd33d3026ac79e
BLAKE2b-256 c172efbddea6a97c6f10aa4d1d2e226aae5731fea5bba7f65b741bfc0076019c

See more details on using hashes here.

File details

Details for the file fastapi_simple_class_view-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_simple_class_view-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 35565a81e81b0a816cd6b6a5f6ae3c6f759654e4b8075b407e7de9b1250a5f2b
MD5 be769b4245aa2af513ac77fbd0777355
BLAKE2b-256 bbe66ce39833dd88b71b105a621adf0e54d42051c27e0c10035470bdbfb86c1d

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