Skip to content

Commit 4ed685a

Browse files
author
Alan Christie
committed
feat: Internal API now has the concept of a 'routing_key'
1 parent 8440748 commit 4ed685a

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

.pylintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[messages control]
2+
3+
# Disable some distracting checks.
4+
# See http://pylint.pycqa.org/en/latest/user_guide/message-control.html
5+
disable = too-few-public-methods,

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ current health of your clone with: -
4141

4242
pre-commit run --all-files
4343

44+
## Local execution
45+
You can build and run the service using `docker compose`: -
46+
47+
docker compose up --build --detach
48+
49+
And then interact with it using `http`, where you should be able to create
50+
and delete event streams using the internal API. Here we're using
51+
`jq` to process the response body to simplify the subsequent **DELETE** request: -
52+
53+
ES_ID=$(http post localhost:8081/event-stream/ routing_key=0123456789 -b | jq -r ".id")
54+
echo $ES_ID
55+
56+
http delete localhost:8081/event-stream/$ES_ID -b
57+
4458
---
4559

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

app/app.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
"""The entrypoint for the Squonk2 FastAPI WebSocket service."""
22

33
import uuid
4+
from typing import Dict
45

56
from fastapi import FastAPI, HTTPException, WebSocket, status
7+
from pydantic import BaseModel
68

79
# Public (event-stream) and internal (REST) services
810
app_public = FastAPI()
911
app_internal = FastAPI()
1012

11-
_ES_UUID_SET: set = set()
13+
# A map of event stream IDs (a UUID with a "es-" prefix) to their routing keys.
14+
_ES_UUID_MAP: Dict = {}
15+
16+
17+
# We use pydantic to declare the model (request payloads) for the internal REST API.
18+
# The public API is a WebSocket API and does not require a model.
19+
class EventStreamPostRequestBody(BaseModel):
20+
"""/event-stream/ POST request body."""
21+
22+
routing_key: str
1223

1324

1425
# Endpoints for the 'public-facing' event-stream web-socket API ------------------------
@@ -17,9 +28,9 @@
1728
@app_public.websocket("/event-stream/{es_id}")
1829
async def event_stream(websocket: WebSocket, es_id: str):
1930
"""The websocket handler for the event-stream."""
20-
if not es_id in _ES_UUID_SET:
31+
if not es_id in _ES_UUID_MAP:
2132
raise HTTPException(
22-
status_code=status.HTTP_400_BAD_REQUEST,
33+
status_code=status.HTTP_404_NOT_FOUND,
2334
detail=f"EventStream {es_id} is not known",
2435
)
2536
await websocket.accept()
@@ -32,20 +43,24 @@ async def event_stream(websocket: WebSocket, es_id: str):
3243

3344

3445
@app_internal.post("/event-stream/")
35-
def post_es():
46+
def post_es(request_body: EventStreamPostRequestBody):
3647
"""Create a new event-stream."""
3748
uuid_str = f"es-{uuid.uuid4()}"
38-
_ES_UUID_SET.add(uuid_str)
49+
_ES_UUID_MAP[uuid_str] = request_body.routing_key
50+
print(_ES_UUID_MAP)
51+
3952
return {"id": uuid_str}
4053

4154

4255
@app_internal.delete("/event-stream/{es_id}")
4356
def delete_es(es_id: str):
4457
"""Destroys an existing event-stream."""
45-
if es_id not in _ES_UUID_SET:
58+
print(_ES_UUID_MAP)
59+
if es_id not in _ES_UUID_MAP:
4660
raise HTTPException(
47-
status_code=status.HTTP_400_BAD_REQUEST,
61+
status_code=status.HTTP_404_NOT_FOUND,
4862
detail=f"EventStream {es_id} is not known",
4963
)
50-
_ES_UUID_SET.remove(es_id)
64+
_ = _ES_UUID_MAP.pop(es_id)
65+
5166
return {}

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
name = "squonk2-fastapi-ws-event-stream"
33
version = "1.0.0"
44
package-mode = false
5-
description = "A Python FastAPI Squonk2 AS EvenStream Service"
5+
description = "A Squonk2 Python FastAPI AS EvenStream Service"
66
authors = ["Alan Christie <[email protected]>"]
77
readme = "README.md"
88

99
[tool.poetry.dependencies]
1010
# Locked versions (for now)
1111
fastapi = "0.115.8"
1212
uvicorn = "0.34.0"
13-
13+
# Flexible versions
14+
pydantic = "^2.10.6"
1415
python = "^3.12"
1516

1617
[tool.poetry.group.dev.dependencies]

0 commit comments

Comments
 (0)