Skip to content

Commit 836aade

Browse files
author
Alan Christie
committed
feat: Better example code
1 parent 53849f4 commit 836aade

File tree

6 files changed

+111
-41
lines changed

6 files changed

+111
-41
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ To list (**GET**) all the existing event streams, run the following:
7878

7979
http localhost:8081/event-stream/ -b
8080

81+
The docker-compose file will start the web socket service on port 8080 and
82+
the internal API on port 8081. ThIt will also run a RabbitMQ server, which can be found
83+
on port 5672.
84+
85+
As well as creating and deleting sockets via the internal API some very simple Python
86+
modules have also been provided to inject messages onto the RabbitMQ bus and to
87+
read messages from the corresponding web socket: -
88+
89+
If you have a websocket you can start a simple listener with the following command,
90+
which will print each message received: -
91+
92+
./simple_es_subscriber.py <location>
93+
94+
You can then 'inject' a very simple **MerchantCharge** message that will be picked up
95+
by the client using the command: -
96+
97+
./simple_es_publisher.py <routing_key>
98+
8199
---
82100

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

poetry.lock

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

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ httpie = "^3.2.4"
2424
isort = "^6.0.0"
2525
black = "^25.1.0"
2626
mypy = "^1.15.0"
27+
im-protobuf = "^7.1.0"
2728

2829
[build-system]
2930
requires = ["poetry-core"]

simple_es_publisher.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
"""A simple RabbitMQ message publisher for local testing.
3+
Takes a 'routing key' and sends a message to the 'expected' AS exchange
4+
on a localhost RabbitMQ server.
5+
6+
Usage: simple_es_publisher.py <routing_key>
7+
"""
8+
import asyncio
9+
import sys
10+
from datetime import datetime, timezone
11+
12+
import aio_pika
13+
from google.protobuf import text_format
14+
from informaticsmatters.protobuf.accountserver.merchant_charge_message_pb2 import (
15+
MerchantChargeMessage,
16+
OperationEnum,
17+
)
18+
19+
_ROUTING_KEY: str = sys.argv[1]
20+
21+
_AMPQ_EXCHANGE: str = "event-streams"
22+
_AMPQ_URL: str = "amqp://es:cheddar1963@localhost:5672"
23+
24+
# A demonstration message
25+
_MESSAGE_CLASS: str = "accountserver.MerchantCharge"
26+
_MESSAGE: MerchantChargeMessage = MerchantChargeMessage()
27+
_MESSAGE.timestamp = datetime.now(timezone.utc).isoformat(timespec="milliseconds")
28+
_MESSAGE.merchant_kind = "DATA_MANAGER"
29+
_MESSAGE.merchant_name = "squonk"
30+
_MESSAGE.merchant_id = 1
31+
_MESSAGE.operation = OperationEnum.OPERATION_ENUM_PROCESSING
32+
_MESSAGE.auth_code = 456782
33+
_MESSAGE.value = "0.50"
34+
_MESSAGE.sqn = 1
35+
_MESSAGE_STRING: str = text_format.MessageToString(_MESSAGE, as_one_line=True)
36+
37+
38+
async def main() -> None:
39+
"""Publish a message."""
40+
connection = await aio_pika.connect_robust(_AMPQ_URL)
41+
async with connection:
42+
channel = await connection.channel()
43+
44+
es_exchange = await channel.declare_exchange(
45+
_AMPQ_EXCHANGE,
46+
aio_pika.ExchangeType.DIRECT,
47+
)
48+
await es_exchange.publish(
49+
aio_pika.Message(body=f"{_MESSAGE_CLASS}|{_MESSAGE_STRING}".encode()),
50+
routing_key=_ROUTING_KEY,
51+
)
52+
53+
54+
if __name__ == "__main__":
55+
asyncio.run(main())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
"""A simple WebSocket client for local testing.
33
4-
Usage: simple_ws_client.py <location>
4+
Usage: simple_es_subscriber.py <location>
55
"""
66
import asyncio
77
import sys
@@ -17,7 +17,7 @@ async def main():
1717
try:
1818
while True:
1919
data = await ws.receive()
20-
print(f"< {data}")
20+
print(str(data))
2121
except (KeyboardInterrupt, EOFError, ConnectionClosed):
2222
await ws.close()
2323

simple_message_publisher.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)