FastAPI إطار عمل حديث لبناء APIs سريعة مع Python.
from fastapi import FastAPI
import irisnative
app = FastAPI(title="IRIS Academy API")
def get_db():
return irisnative.createConnection(
'localhost', 52773, 'USER', '_SYSTEM', 'SYS'
)
@app.get("/employees")
def list_employees(dept: str = None):
conn = get_db()
query = "SELECT ID, Name, Department FROM Employee"
if dept:
query += f" WHERE Department = '{dept}'"
cursor = conn.cursor()
cursor.execute(query)
return [{"id": r[0], "name": r[1], "dept": r[2]} for r in cursor]
@app.post("/employees")
def create_employee(name: str, dept: str, salary: float):
conn = get_db()
cursor = conn.cursor()
cursor.execute(
"INSERT INTO Employee (Name, Department, Salary) VALUES (?, ?, ?)",
[name, dept, salary]
)
conn.commit()
return {"status": "created"}