sse sub resource

This commit is contained in:
2026-07-12 00:51:23 +05:30
parent 4a7a76e330
commit f1a7a556fd
5 changed files with 59 additions and 60 deletions

View File

@@ -43,6 +43,7 @@ from data import (
create_pet as _create_pet,
update_pet as _update_pet,
delete_pet as _delete_pet,
get_pet as _get_pet,
list_appointments as _list_appointments,
get_appointment as _get_appointment,
create_appointment as _create_appointment,
@@ -368,9 +369,14 @@ def delete_appointment(id: int, response: Response):
response.status_code = 204
async def stream_calls():
"""Stream random animal sounds via SSE."""
q = await subscribe()
async def stream_calls(id: int):
"""Stream animal sounds via SSE, scoped to a pet's species."""
try:
pet = _get_pet(id)
except KeyError:
raise HTTPException(status_code=404, detail="Pet not found")
species = pet.species
q = await subscribe(id, species)
async def event_generator():
try:
@@ -378,6 +384,6 @@ async def stream_calls():
data = await q.get()
yield f"data: {data}\n\n"
finally:
unsubscribe(q)
unsubscribe(id, q)
return StreamingResponse(event_generator(), media_type="text/event-stream")