Skip to content

Commit 05da039

Browse files
author
Alan Christie
committed
feat: Now supports GET at /event-stream/
1 parent 1ba036a commit 05da039

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,21 @@ and delete event streams using the internal API. Here we're using
6363
`jq` and `cut` to process the response body to simplify the subsequent **DELETE**
6464
request: -
6565

66+
To create (**POST**) an event stream, run the following:
67+
6668
ES_LOC=$(http post localhost:8081/event-stream/ routing_key=0123456789 -b | jq -r ".location")
6769
echo $ES_LOC
6870
ES_ID=$(echo $ES_LOC | cut -d/ -f5)
71+
echo $ES_ID
72+
73+
To **DELETE** the event stream, run the following:
6974

7075
http delete localhost:8081/event-stream/$ES_ID -b
7176

77+
To list (**GET**) all the existing event streams, run the following:
78+
79+
http localhost:8081/event-stream/ -b
80+
7281
---
7382

7483
[black]: https://black.readthedocs.io/en/stable

app/app.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ class EventStreamPostRequestBody(BaseModel):
7070

7171
@app_public.websocket("/event-stream/{uuid}")
7272
async 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}")
176197
def 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

Comments
 (0)