-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (31 loc) · 1.07 KB
/
server.py
File metadata and controls
36 lines (31 loc) · 1.07 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
# server.py
from fastapi import FastAPI, HTTPException, Header
from pydantic import BaseModel
import rhino_health as rh
import uuid
app = FastAPI()
sessions = {}
class LoginRequest(BaseModel):
username: str
password: str
@app.post("/login")
def login(req: LoginRequest):
try:
session = rh.login(username=req.username, password=req.password, rhino_api_url=rh.lib.constants.ApiEnvironment.DEMO_DEV_URL)
session_id = str(uuid.uuid4())
sessions[session_id] = session
return {"session_id": session_id}
except Exception as e:
raise HTTPException(status_code=401, detail=str(e))
class ProjectRequest(BaseModel):
session_id: str
@app.post("/projects")
def get_projects(req: ProjectRequest):
session = sessions.get(req.session_id)
if not session:
raise HTTPException(status_code=401, detail="Invalid or expired session_id")
try:
projects = session.project.get_projects()
return {"projects": [p.dict() for p in projects]}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))