-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_sync.py
More file actions
92 lines (70 loc) · 2.12 KB
/
Copy pathmain_sync.py
File metadata and controls
92 lines (70 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from typing import Generator, Annotated
import asyncio
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import IntegrityError
from sqlmodel import select, func
from sqlmodel import Session
from models import Hero, Heroes
DATABASE_URL_DIALECT = (
"postgresql+psycopg://postgres:mysecretpassword@127.0.0.1:5433/postgres"
)
app = FastAPI(
title="Test FASTAPI SQLALCHEMY SQLMODEL",
description="TBD",
version="0.1.0",
)
engine = create_engine(
DATABASE_URL_DIALECT,
)
def get_session() -> Generator[Session, None, None]:
session = sessionmaker(
bind=engine,
class_=Session,
expire_on_commit=False,
)
with session() as sess:
yield sess
SessionDep = Annotated[Session, Depends(get_session)]
@app.post("/heroes/")
def create_hero(hero: Hero, session: SessionDep):
session.add(hero)
try:
session.commit()
return hero
except IntegrityError:
session.rollback()
raise HTTPException(409, "Hero already exists")
@app.get("/heroes/{hero_id}/")
def read_hero(hero_id: int, session: SessionDep):
hero = session.get(Hero, hero_id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
return hero
@app.get("/heroes/")
def read_heroes(session: SessionDep):
count = session.exec(select(func.count()).select_from(Hero))
res = session.exec(select(Hero))
return Heroes(data=res.all(), count=count.one())
@app.get("/pool-status/")
def pool_status():
pool = engine.pool
return {
"size": pool.size(),
"checked_in": pool.checkedin(),
"checked_out": pool.checkedout(),
"overflow": pool.overflow(),
}
@app.post("/heroes/minimal/")
def create_hero_minimal():
return {"id": 1, "name": "test", "country": "test"}
@app.get("/asyncio-stats/")
def asyncio_stats():
loop = asyncio.get_event_loop()
tasks = asyncio.all_tasks(loop)
return {
"total_tasks": len(tasks),
"loop_running": loop.is_running(),
"loop_debug": loop.get_debug(),
}