HTMLResponse, JSONResponse, PlainTextResponse, RedirectResponse, ) from fastapi.templating import Jinja2Templates from fastapi_mako import FastAPIMako from fastapi.security import HTTPBasic, HTTPBasicCredentials import secrets router = APIRouter() MAX_QUEUE_SIZE = 3 router.counter = 0 router.__name__ = "First router" router.fake_datebase = {} start = date.today() objects = { 1: {"field_a": "a", "field_b": "b"}, 2: {"field_a": "a", "field_b": "b"}, 3: {"field_a": "a", "field_b": "b"}, # .... # } router.secret_key = "very constatn and random secret, best 64+ characters" router.access_tokens = [] security = HTTPBasic()
class Shipper(BaseModel): company_name: str class CategoryInput(BaseModel): name: str class CategoryOutput(BaseModel): id: int name: str dbrouter = APIRouter() dbrouter.__name__ = "DataBase app!" templates = Jinja2Templates(directory="templates") db_path = os.path.abspath(os.getcwd()) + "/db/northwind.db" print(db_path) @dbrouter.get("/", response_class=HTMLResponse) def welcome_jinja(request: Request): return templates.TemplateResponse( "welcome.html.j2", {"request": request}, ) @dbrouter.on_event("startup")
from fastapi import APIRouter, Depends, HTTPException, status, Response, Request from dblib.database import get_db from dblib import crud, schemas, models from typing import List from pydantic import PositiveInt from sqlalchemy.orm import Session ormrouter = APIRouter() ormrouter.__name__ = "ORM app!" @ormrouter.get("/shippers/{shipper_id}", response_model=schemas.Shipper) async def get_shipper(shipper_id: PositiveInt, db: Session = Depends(get_db)): db_shipper = crud.get_shipper(db, shipper_id) if db_shipper is None: raise HTTPException(status_code=404, detail="Shipper not found") return db_shipper @ormrouter.get("/shippers", response_model=List[schemas.Shipper]) async def get_shippers(db: Session = Depends(get_db)): return crud.get_shippers(db) @ormrouter.get("/suppliers", response_model=List[schemas.Supplier]) async def get_suppliers(response: Response, db: Session = Depends(get_db)): response.status_code = status.HTTP_200_OK return crud.get_suppliers(db) @ormrouter.get("/suppliers/{supplier_id}", response_model=schemas.SupplierFull)