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 gspot-fastapi-auth
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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_simple_class_view-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 073d3187ae55f2d66aca30c2451b6bd99134dfbc4d5e46fc96a1e7821e769904 |
|
MD5 | ca3383d19c530cde03d1f719e0f76a92 |
|
BLAKE2b-256 | 770e81252951f9abc138e002e102b03520f12b05da00d2bcbae85d4b81b66f3b |
Close
Hashes for fastapi_simple_class_view-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce2e0f5f6f2bd158c200825da427ae9932d8dc3b6d6173456426d9468fe124d2 |
|
MD5 | a203ee243cbf5ca9e52d8d951c5e27ab |
|
BLAKE2b-256 | e60dffe162bc238bf9fd010ee309c8faa19d8d34f150e7ffaeaef99c156a5359 |