-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_hierarchy_api.py
More file actions
230 lines (202 loc) · 6.59 KB
/
Copy pathquick_hierarchy_api.py
File metadata and controls
230 lines (202 loc) · 6.59 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""
Quick script to add hierarchy API endpoints to the existing orchestrator
"""
import asyncio
import asyncpg
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import uuid
import os
# Database connection
DATABASE_URL = "postgresql://postgres:J7hplO7vKnbUsKDAsxpe4t9C0@localhost:5434/ai_context"
app = FastAPI(title="FuzeAgent Hierarchy API", version="1.0.0")
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Pydantic models
class Organization(BaseModel):
id: str
name: str
description: Optional[str] = None
settings: dict = {}
created_at: str
updated_at: str
class OrganizationCreate(BaseModel):
name: str
description: Optional[str] = None
settings: dict = {}
class Team(BaseModel):
id: str
organization_id: str
name: str
description: Optional[str] = None
team_type: str = "general"
settings: dict = {}
created_at: str
updated_at: str
class TeamCreate(BaseModel):
organization_id: str
name: str
description: Optional[str] = None
team_type: str = "general"
settings: dict = {}
# Database connection pool
db_pool = None
async def get_db_pool():
global db_pool
if db_pool is None:
db_pool = await asyncpg.create_pool(DATABASE_URL)
return db_pool
@app.on_event("startup")
async def startup_event():
await get_db_pool()
@app.on_event("shutdown")
async def shutdown_event():
if db_pool:
await db_pool.close()
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "hierarchy-api"}
@app.get("/organizations", response_model=List[Organization])
async def get_organizations():
pool = await get_db_pool()
async with pool.acquire() as conn:
rows = await conn.fetch("""
SELECT
id::text,
name,
description,
settings,
created_at::text,
updated_at::text
FROM organizations
ORDER BY created_at DESC
""")
return [Organization(**dict(row)) for row in rows]
@app.post("/organizations", response_model=Organization)
async def create_organization(org_data: OrganizationCreate):
pool = await get_db_pool()
async with pool.acquire() as conn:
org_id = str(uuid.uuid4())
row = await conn.fetchrow("""
INSERT INTO organizations (id, name, description, settings)
VALUES ($1, $2, $3, $4)
RETURNING
id::text,
name,
description,
settings,
created_at::text,
updated_at::text
""", org_id, org_data.name, org_data.description, org_data.settings)
return Organization(**dict(row))
@app.get("/organizations/{organization_id}", response_model=Organization)
async def get_organization(organization_id: str):
pool = await get_db_pool()
async with pool.acquire() as conn:
row = await conn.fetchrow("""
SELECT
id::text,
name,
description,
settings,
created_at::text,
updated_at::text
FROM organizations
WHERE id = $1
""", organization_id)
if not row:
raise HTTPException(status_code=404, detail="Organization not found")
return Organization(**dict(row))
@app.get("/teams", response_model=List[Team])
async def get_teams(organization_id: Optional[str] = None):
pool = await get_db_pool()
async with pool.acquire() as conn:
if organization_id:
rows = await conn.fetch("""
SELECT
id::text,
organization_id::text,
name,
description,
team_type,
settings,
created_at::text,
updated_at::text
FROM teams
WHERE organization_id = $1
ORDER BY created_at DESC
""", organization_id)
else:
rows = await conn.fetch("""
SELECT
id::text,
organization_id::text,
name,
description,
team_type,
settings,
created_at::text,
updated_at::text
FROM teams
ORDER BY created_at DESC
""")
return [Team(**dict(row)) for row in rows]
@app.post("/teams", response_model=Team)
async def create_team(team_data: TeamCreate):
pool = await get_db_pool()
async with pool.acquire() as conn:
# Verify organization exists
org_exists = await conn.fetchval(
"SELECT EXISTS(SELECT 1 FROM organizations WHERE id = $1)",
team_data.organization_id
)
if not org_exists:
raise HTTPException(status_code=404, detail="Organization not found")
team_id = str(uuid.uuid4())
row = await conn.fetchrow("""
INSERT INTO teams (id, organization_id, name, description, team_type, settings)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING
id::text,
organization_id::text,
name,
description,
team_type,
settings,
created_at::text,
updated_at::text
""", team_id, team_data.organization_id, team_data.name,
team_data.description, team_data.team_type, team_data.settings)
return Team(**dict(row))
@app.get("/teams/{team_id}", response_model=Team)
async def get_team(team_id: str):
pool = await get_db_pool()
async with pool.acquire() as conn:
row = await conn.fetchrow("""
SELECT
id::text,
organization_id::text,
name,
description,
team_type,
settings,
created_at::text,
updated_at::text
FROM teams
WHERE id = $1
""", team_id)
if not row:
raise HTTPException(status_code=404, detail="Team not found")
return Team(**dict(row))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8002)