nested schema fixes
This commit is contained in:
@@ -190,19 +190,19 @@ components:
|
|||||||
x-label: "Complications"
|
x-label: "Complications"
|
||||||
required: [noteType, surgeryType]
|
required: [noteType, surgeryType]
|
||||||
|
|
||||||
Call:
|
Action:
|
||||||
type: object
|
type: object
|
||||||
x-primary-key: _received_at
|
x-primary-key: _received_at
|
||||||
x-display-format: "{sound}"
|
x-display-format: "{action}"
|
||||||
x-list-columns: [sound]
|
x-list-columns: [action]
|
||||||
properties:
|
properties:
|
||||||
sound:
|
action:
|
||||||
type: string
|
type: string
|
||||||
enum: [woof, meow, coo]
|
enum: [wagging_tail, blep, stretching, showing_belly, barking, panting, purring, kneading, head_tilt, chirping, wing_flap]
|
||||||
x-label: "Sound"
|
x-label: "Action"
|
||||||
x-order: 1
|
x-order: 1
|
||||||
x-filterable: false
|
x-filterable: false
|
||||||
required: [sound]
|
required: [action]
|
||||||
|
|
||||||
Parent:
|
Parent:
|
||||||
type: object
|
type: object
|
||||||
@@ -1058,10 +1058,10 @@ paths:
|
|||||||
$ref: '#/components/responses/ValidationError'
|
$ref: '#/components/responses/ValidationError'
|
||||||
'500':
|
'500':
|
||||||
$ref: '#/components/responses/InternalServerError'
|
$ref: '#/components/responses/InternalServerError'
|
||||||
/pets/{id}/calls:
|
/pets/{id}/actions:
|
||||||
get:
|
get:
|
||||||
summary: Stream animal sounds via SSE, scoped to a pet's species
|
summary: Stream animal actions via SSE, scoped to a pet's species
|
||||||
operationId: stream_calls
|
operationId: stream_actions
|
||||||
x-sse: true
|
x-sse: true
|
||||||
parameters:
|
parameters:
|
||||||
- name: id
|
- name: id
|
||||||
@@ -1070,11 +1070,11 @@ paths:
|
|||||||
schema: {type: integer}
|
schema: {type: integer}
|
||||||
responses:
|
responses:
|
||||||
'200':
|
'200':
|
||||||
description: SSE stream of random animal sounds
|
description: SSE stream of random animal actions (behaviors)
|
||||||
content:
|
content:
|
||||||
text/event-stream:
|
text/event-stream:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/Call'
|
$ref: '#/components/schemas/Action'
|
||||||
|
|
||||||
/appointments:
|
/appointments:
|
||||||
get:
|
get:
|
||||||
|
|||||||
@@ -369,8 +369,8 @@ def delete_appointment(id: int, response: Response):
|
|||||||
response.status_code = 204
|
response.status_code = 204
|
||||||
|
|
||||||
|
|
||||||
async def stream_calls(id: int):
|
async def stream_actions(id: int):
|
||||||
"""Stream animal sounds via SSE, scoped to a pet's species."""
|
"""Stream animal actions via SSE, scoped to a pet's species."""
|
||||||
try:
|
try:
|
||||||
pet = _get_pet(id)
|
pet = _get_pet(id)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|||||||
@@ -2,16 +2,20 @@ import asyncio
|
|||||||
import random
|
import random
|
||||||
import json
|
import json
|
||||||
|
|
||||||
_sounds_by_species = {"dog": ["woof"], "cat": ["meow"], "bird": ["coo"]}
|
_behaviors_by_species = {
|
||||||
|
"dog": ["wagging_tail", "stretching", "showing_belly", "barking", "panting"],
|
||||||
|
"cat": ["blep", "stretching", "showing_belly", "purring", "kneading"],
|
||||||
|
"bird": ["head_tilt", "stretching", "chirping", "wing_flap"],
|
||||||
|
}
|
||||||
_subscribers: dict[int, list[asyncio.Queue]] = {}
|
_subscribers: dict[int, list[asyncio.Queue]] = {}
|
||||||
_worker_tasks: dict[int, asyncio.Task] = {}
|
_worker_tasks: dict[int, asyncio.Task] = {}
|
||||||
|
|
||||||
|
|
||||||
async def _sound_worker(pet_id: int, species: str):
|
async def _behavior_worker(pet_id: int, species: str):
|
||||||
sounds = _sounds_by_species.get(species, ["woof"])
|
behaviors = _behaviors_by_species.get(species, ["wagging_tail"])
|
||||||
while True:
|
while True:
|
||||||
sound = random.choice(sounds)
|
action = random.choice(behaviors)
|
||||||
data = json.dumps({"sound": sound})
|
data = json.dumps({"action": action})
|
||||||
queues = _subscribers.get(pet_id, [])
|
queues = _subscribers.get(pet_id, [])
|
||||||
for q in queues:
|
for q in queues:
|
||||||
await q.put(data)
|
await q.put(data)
|
||||||
@@ -22,7 +26,7 @@ def _ensure_worker(pet_id: int, species: str):
|
|||||||
if pet_id not in _worker_tasks or _worker_tasks[pet_id].done():
|
if pet_id not in _worker_tasks or _worker_tasks[pet_id].done():
|
||||||
_subscribers.setdefault(pet_id, [])
|
_subscribers.setdefault(pet_id, [])
|
||||||
_worker_tasks[pet_id] = asyncio.create_task(
|
_worker_tasks[pet_id] = asyncio.create_task(
|
||||||
_sound_worker(pet_id, species)
|
_behavior_worker(pet_id, species)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user