This commit is contained in:
Waylon Walker 2023-06-27 16:19:32 -05:00
parent 3490305e39
commit d747299c95
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
8 changed files with 64 additions and 49 deletions

View file

@ -8,13 +8,13 @@ hero_router = APIRouter()
@hero_router.on_event("startup")
def on_startup() -> None:
async def on_startup() -> None:
# SQLModel.metadata.create_all(get_config().database.engine)
...
@hero_router.get("/hero/{hero_id}")
def get_hero(
async def get_hero(
*,
session: Session = Depends(get_session),
hero_id: int,
@ -27,7 +27,7 @@ def get_hero(
@hero_router.post("/hero/")
def post_hero(
async def post_hero(
*,
session: Session = Depends(get_session),
hero: HeroCreate,
@ -42,7 +42,7 @@ def post_hero(
@hero_router.patch("/hero/")
def patch_hero(
async def patch_hero(
*,
session: Session = Depends(get_session),
hero: HeroUpdate,
@ -61,7 +61,7 @@ def patch_hero(
@hero_router.delete("/hero/{hero_id}")
def delete_hero(
async def delete_hero(
*,
session: Session = Depends(get_session),
hero_id: int,
@ -77,11 +77,11 @@ def delete_hero(
@hero_router.get("/heros/")
def get_heros(
async def get_heros(
*,
session: Session = Depends(get_session),
) -> Heros:
"get all heros"
statement = select(Hero)
heros = session.exec(statement).all()
heros = session.execute(statement).all()
return Heros(__root__=heros)

View file

@ -29,6 +29,8 @@ def run(
help="show the log messages",
),
):
import uvicorn
uvicorn.run(**get_config().api_server.dict())

View file

@ -54,7 +54,7 @@ def list() -> Union[Hero, List[Hero]]:
def clear() -> Union[Hero, List[Hero]]:
"list many heros"
heros = Heros.list()
for hero in heros.heros:
for hero in heros.__root__:
HeroDelete.delete(id=hero.id)
return hero

View file

@ -4,6 +4,7 @@ from typing import TYPE_CHECKING
from fastapi import Depends
from pydantic import BaseModel, BaseSettings, validator
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import Session
from learn_sql_model.standard_config import load
@ -18,6 +19,7 @@ class ApiServer(BaseModel):
reload: bool = True
log_level: str = "info"
host: str = "0.0.0.0"
workers: int = 1
class ApiClient(BaseModel):
@ -42,7 +44,16 @@ class Database:
@property
def engine(self) -> "Engine":
return create_engine(self.config.database_url)
try:
return self._engine
except AttributeError:
self._engine = create_engine(
self.config.database_url,
connect_args={"check_same_thread": False},
pool_recycle=3600,
pool_pre_ping=True,
)
return self._engine
@property
def session(self) -> "Session":
@ -87,11 +98,20 @@ def get_config(overrides: dict = {}) -> Config:
return config
config = get_config()
engine = create_engine(
config.database_url,
connect_args={"check_same_thread": False},
pool_recycle=3600,
pool_pre_ping=True,
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_session() -> "Session":
config = get_config()
engine = create_engine(config.database_url)
with Session(engine) as session:
yield session
yield SessionLocal()
async def reset_db_state(config: Config = None) -> None:

View file

@ -1,13 +1,11 @@
from typing import Dict, Optional
from typing import Dict
import httpx
from pydantic import BaseModel
from sqlmodel import Field, SQLModel
from learn_sql_model.config import config
from learn_sql_model.optional import optional
from learn_sql_model.models.pet import Pet
class HeroBase(SQLModel, table=False):
@ -75,7 +73,7 @@ class HeroUpdate(HeroBase):
def update(self) -> Hero:
r = httpx.patch(
f"{config.api_client.url}/hero/",
json=self.dict(),
json=self.dict(exclude_none=True),
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")