Skip to main content

Class based routing for FastAPI

Project description

Class based routing for FastAPI


Package version


Documentation: https://XDeepZeroX.github.io/class-based-fastapi

Source Code: https://github.com/XDeepZeroX/class-based-fastapi


FastAPI is a modern, fast web framework for building APIs with Python 3.6+.


Features

Write Fast API Controllers (Classes) that can inherit route information from it's parent.

  • This also allows to create a path prefix from a template and add api version information in the template.
  • You don't need to duplicate the code, you can inherit it.
  • To generate OpenAPI documentation, you do not need to explicitly specify the type of the return value, use Generics !

Do the same with API methods as before, only more convenient.

See the docs for more details and examples.

Requirements

This package is intended for use with any recent version of FastAPI (depending on pydantic>=1.8.2), and Python 3.6+.

Installation

pip install class-based-fastapi

Example

import uuid
from typing import List, Generic, TypeVar  # 0. Import

import sqlalchemy
import uvicorn
from class_based_fastapi import Routable, get, put, post, delete
from fastapi import FastAPI, Depends
from sqlalchemy import select
from sqlmodel import Session, create_engine

from database import run_async_upgrade
from models.models import Category, CategoryPUT, Book, BookPUT

app = FastAPI(debug=True)

engine = create_engine('postgresql://postgres:123456@localhost:5432/fastapi_example', echo=True)


@app.on_event("startup")
def on_startup():
    print("Start migration")
    run_async_upgrade()
    print("DB success upgrade !")


def get_session() -> Session:
    with Session(engine) as conn:
        yield conn


T = TypeVar('T')  # 1. Create generic type
TPut = TypeVar('TPut')  # 1. Create generic type


class BaseAPI(Routable, Generic[T, TPut]):  # 2. Create generic base API controller
    conn: Session = Depends(get_session)

    def __init__(self):
        self._type_db_model = self._get_type_generic(T)

    def _get_type_generic(self, tvar: TypeVar):
        return next(filter(lambda x: x['name'] == tvar.__name__, self.__class__.__generic_attribute__))['type']

    @get("")
    def get_list_categories(self) -> List[T]:  # 3. Specifying  generic types
        items = self.conn.execute(select(self._type_db_model)).scalars().all()
        return items

    @post("")
    def add_category(self, model: T) -> T:
        self.conn.add(model)
        self.conn.commit()
        return model

    @delete("{guid}")
    def delete_category(self, guid: str) -> bool:
        self.conn.execute(
            sqlalchemy.delete(self._type_db_model).filter(self._type_db_model.guid == uuid.UUID(guid))
        )
        self.conn.commit()
        return True

    @put("{guid}")
    def update_category(self, guid: str, model: TPut) -> T:  # 3. Specifying  generic types
        model_db = self.conn.execute(
            select(self._type_db_model).filter(self._type_db_model.guid == uuid.UUID(guid))
        ).scalar()
        # Update fields
        for name, val in model.dict(exclude_unset=True).items():
            setattr(model_db, name, val)
        self.conn.commit()
        self.conn.refresh(model_db)
        return model_db


# Categories
class CategoryAPI(BaseAPI[Category, CategoryPUT]):  # 4. Inheriting the base controller
    NAME_MODULE = Category.__name__


# Books
class BookAPI(BaseAPI[Book, BookPUT]):  # 4. Inheriting the base controller
    NAME_MODULE = Book.__name__


app.include_router(CategoryAPI.routes())  # 5. Include routes
app.include_router(BookAPI.routes())  # 5. Include routes

if __name__ == "__main__":
    uvicorn.run('main:app', host="localhost", port=8001, reload=True, debug=True)

Class base API OpenAPI Docs

Next steps >>>

License

This project is licensed under the terms of the MIT license.

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

class-based-fastapi-1.0.3.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

class_based_fastapi-1.0.3-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file class-based-fastapi-1.0.3.tar.gz.

File metadata

  • Download URL: class-based-fastapi-1.0.3.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.0

File hashes

Hashes for class-based-fastapi-1.0.3.tar.gz
Algorithm Hash digest
SHA256 5ef482cff324942bbb676db7d95b069d91042bf57ccf9138cd9e0c128044ebce
MD5 ee643801a572970867ed8334cb3773c6
BLAKE2b-256 66c5c1fc50b568e843d6c7a6c900fbec7d83f3d26988690d8ab294d5f597f723

See more details on using hashes here.

File details

Details for the file class_based_fastapi-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for class_based_fastapi-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 55b60927518a8cb834ea65718cc2fc69fab76450ba478c8c03b9ee0a5e04b9a9
MD5 a289ff31490fe4ff99282bae93c7428f
BLAKE2b-256 0adf7a1417cc016f97c443fc973e976e5d3e7d08e1c3cdb4ad613c522b31a25c

See more details on using hashes here.

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