
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
class-based-fastapi
Advanced tools
Class based routing for FastAPI
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+.
Write Fast API Controllers (Classes) that can inherit route information from it's parent.
Do the same with API methods as before, only more convenient.
See the docs for more details and examples.
This package is intended for use with any recent version of FastAPI (depending on pydantic>=1.8.2
), and Python 3.6+.
pip install class-based-fastapi
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)
This project is licensed under the terms of the MIT license.
FAQs
Class based routing for FastAPI
We found that class-based-fastapi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.