Added more websocket endpoints

Can now subscribe to:
- spool/filament/vendor level events, not specific to any specific ID
- root level events, captures all events sent
This commit is contained in:
Donkie
2023-10-15 20:36:48 +02:00
parent 03be75abca
commit 3e2bbed17e
4 changed files with 114 additions and 7 deletions

View File

@@ -110,9 +110,17 @@ class FilamentUpdateParameters(FilamentParameters):
@router.get(
"",
name="Find filaments",
description="Get a list of filaments that matches the search query.",
description=(
"Get a list of filaments that matches the search query. "
"A websocket is served on the same path to listen for updates to any filament, or added or deleted filaments. "
"See the HTTP Response code 299 for the content of the websocket messages."
),
response_model_exclude_none=True,
response_model=list[Filament],
responses={
200: {"model": list[Filament]},
404: {"model": Message},
299: {"model": FilamentEvent, "description": "Websocket message"},
},
)
async def find(
*,
@@ -231,6 +239,24 @@ async def find(
)
@router.websocket(
"",
name="Listen to filament changes",
)
async def notify_any(
websocket: WebSocket,
) -> None:
await websocket.accept()
websocket_manager.connect(("filament",), websocket)
try:
while True:
await asyncio.sleep(0.5)
if await websocket.receive_text():
await websocket.send_json({"status": "healthy"})
except WebSocketDisconnect:
websocket_manager.disconnect(("filament",), websocket)
@router.get(
"/{filament_id}",
name="Get filament",