@@ -70,7 +70,9 @@ class EventStreamPostRequestBody(BaseModel):
7070
7171@app_public .websocket ("/event-stream/{uuid}" )
7272async def event_stream (websocket : WebSocket , uuid : str ):
73- """The websocket handler for the event-stream."""
73+ """The websocket handler for the event-stream.
74+ The actual location is returned to the AS when the web-socket is created
75+ using a POST to /event-stream/."""
7476
7577 # Get the DB record for this UUID...
7678 db = sqlite3 .connect (_DATABASE_PATH )
@@ -144,8 +146,7 @@ def post_es(request_body: EventStreamPostRequestBody):
144146 # Generate am new (difficult to guess) UUID for the event stream...
145147 uuid_str : str = shortuuid .uuid ()
146148 # And construct the location we'll be listening on...
147- location : str = "wss://" if _INGRESS_SECURE else "ws://"
148- location += f"{ _INGRESS_LOCATION } /event-stream/{ uuid_str } "
149+ location : str = _get_location (uuid_str )
149150
150151 # Create a new ES record...
151152 # An ID is assigned automatically -
@@ -172,6 +173,26 @@ def post_es(request_body: EventStreamPostRequestBody):
172173 }
173174
174175
176+ @app_internal .get ("/event-stream/" )
177+ def get_es ():
178+ """Returns a list of the details of all existing event-streams,
179+ their IDs, locations, and routing keys."""
180+
181+ # Get the ES record (by primary key)
182+ db = sqlite3 .connect (_DATABASE_PATH )
183+ cursor = db .cursor ()
184+ res = cursor .execute ("SELECT * FROM es" )
185+ all_es = res .fetchall ()
186+ db .close ()
187+
188+ event_streams = []
189+ for es in all_es :
190+ location : str = _get_location (es [1 ])
191+ event_streams .append ({"id" : es [0 ], "location" : location , "routing_key" : es [2 ]})
192+
193+ return {"event-streams" : event_streams }
194+
195+
175196@app_internal .delete ("/event-stream/{es_id}" )
176197def delete_es (es_id : int ):
177198 """Destroys an existing event-stream."""
@@ -200,3 +221,10 @@ def delete_es(es_id: int):
200221 _LOGGER .info ("Deleted %s" , es_id )
201222
202223 return {}
224+
225+
226+ def _get_location (uuid : str ) -> str :
227+ """Returns the location (URL) for the event stream with the given UUID."""
228+ location : str = "wss://" if _INGRESS_SECURE else "ws://"
229+ location += f"{ _INGRESS_LOCATION } /event-stream/{ uuid } "
230+ return location
0 commit comments