Skip to content

Commit b6b5eb6

Browse files
committed
mvp of webhooks for subwallets
1 parent cd38ee3 commit b6b5eb6

File tree

6 files changed

+333
-294
lines changed

6 files changed

+333
-294
lines changed

libs/aries-basic-controller/aries_basic_controller/aries_controller.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class AriesAgentController:
7979
revocations: bool = True
8080
api_key: str = None
8181
tenant_jwt: str = None
82+
wallet_id: str = "base"
8283

8384

8485
def __post_init__(self):
@@ -98,6 +99,7 @@ def __post_init__(self):
9899
if self.tenant_jwt:
99100
self.headers.update({'Authorization': 'Bearer ' + self.tenant_jwt, 'content-type': "application/json"})
100101

102+
101103
self.client_session: ClientSession = ClientSession(headers=self.headers)
102104

103105
# Instantiate controllers based on the provided attributes
@@ -135,6 +137,7 @@ def __post_init__(self):
135137
self.client_session
136138
)
137139

140+
138141

139142
def update_tenant_jwt(self, tenant_jwt: str):
140143
"""Update the tenant JW token attribute and the header
@@ -218,8 +221,13 @@ def add_listener(self, listener):
218221
A dictionary comprised of a "handler": handler (fct) and a "topic":"topicname" key-value pairs
219222
"""
220223
try:
221-
pub.subscribe(listener["handler"], listener["topic"])
222-
logger.debug("Lister added for topic : ", listener["topic"])
224+
pub_topic_path = listener['topic']
225+
if self.wallet_id:
226+
pub_topic_path = f"{self.wallet_id}.{pub_topic_path}"
227+
print("Subscribing too: " + pub_topic_path)
228+
pub.subscribe(listener["handler"], pub_topic_path)
229+
230+
logger.debug("Lister added for topic : ", pub_topic_path)
223231
except Exception as exc:
224232
print(f"Adding webhooks listener failed! {exc!r} occurred.")
225233
logger.warn(f"Adding webhooks listener failed! {exc!r} occurred.")
@@ -267,7 +275,7 @@ async def listen_webhooks(self):
267275
"""Create a server to listen to webhooks"""
268276
try:
269277
app = web.Application()
270-
app.add_routes([web.post(self.webhook_base + "/topic/{topic}/", self._receive_webhook)])
278+
app.add_routes([web.post(self.webhook_base + "/{wallet}/topic/{topic}/", self._receive_webhook)])
271279
runner = web.AppRunner(app)
272280
await runner.setup()
273281
self.webhook_site = web.TCPSite(runner, self.webhook_host, self.webhook_port)
@@ -292,17 +300,18 @@ async def _receive_webhook(self, request: ClientRequest):
292300
A response with status 200
293301
"""
294302
topic = request.match_info["topic"]
295-
303+
wallet = request.match_info["wallet"]
304+
print("wallet", wallet)
296305
try:
297306
payload = await request.json()
298-
await self._handle_webhook(topic, payload)
307+
await self._handle_webhook(wallet, topic, payload)
299308
return web.Response(status=200)
300309
except Exception as exc:
301310
logger.warn(f"Receiving webhooks failed! {exc!r} occurred.")
302311

303312

304313

305-
async def _handle_webhook(self, topic, payload):
314+
async def _handle_webhook(self, wallet, topic, payload):
306315
"""Helper handling a webhook
307316
308317
Args:
@@ -313,8 +322,10 @@ async def _handle_webhook(self, topic, payload):
313322
A JSON-like dictionary representation of the payload
314323
"""
315324
try:
316-
logging.debug(f"Handle Webhook - {topic}", payload)
317-
pub.sendMessage(topic, payload=payload)
325+
pub_topic_path = f"{wallet}.{topic}"
326+
print(f"Handle Webhook - {pub_topic_path}", payload)
327+
logging.debug(f"Handle Webhook - {pub_topic_path}", payload)
328+
pub.sendMessage(pub_topic_path, payload=payload)
318329
# return web.Response(status=200)
319330
except Exception as exc:
320331
logger.warn(f"Handling webhooks failed! {exc!r} occurred when trying to handle this topic: {topic}")

tutorials/4. Multitenancy/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ services:
9999
- AGENT_NAME=${MULTITENANT_AGENT_NAME}
100100
- ADMIN_PORT=${MULTITENANT_ADMIN_PORT}
101101
- HTTP_PORT=${MULTITENANT_HTTP_PORT}
102-
- ACAPY_WEBHOOK_URL=${MULTITENANT_WEBHOOK_URL}
102+
- ACAPY_WEBHOOK_URL=${MULTITENANT_WEBHOOK_URL}/base
103103
- AGENT_ENDPOINT=${MULTITENANT_AGENT_ENDPOINT}
104104
- WALLET_SEED=${MULTITENANT_WALLET_SEED}
105105
- WALLET_NAME=${MULTITENANT_WALLET_NAME}

tutorials/4. Multitenancy/notebooks/mediator/Configure Mediator.ipynb

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
{
3434
"cell_type": "code",
35-
"execution_count": 2,
35+
"execution_count": 1,
3636
"metadata": {},
3737
"outputs": [
3838
{
@@ -64,7 +64,7 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 3,
67+
"execution_count": 2,
6868
"metadata": {},
6969
"outputs": [],
7070
"source": [
@@ -78,7 +78,7 @@
7878
},
7979
{
8080
"cell_type": "code",
81-
"execution_count": 4,
81+
"execution_count": 3,
8282
"metadata": {},
8383
"outputs": [],
8484
"source": [
@@ -102,9 +102,17 @@
102102
},
103103
{
104104
"cell_type": "code",
105-
"execution_count": 5,
105+
"execution_count": 4,
106106
"metadata": {},
107-
"outputs": [],
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"base.connections\n"
113+
]
114+
}
115+
],
108116
"source": [
109117
"\n",
110118
"loop = asyncio.get_event_loop()\n",
@@ -139,36 +147,16 @@
139147
},
140148
{
141149
"cell_type": "code",
142-
"execution_count": 6,
150+
"execution_count": 5,
143151
"metadata": {},
144152
"outputs": [
145153
{
146154
"name": "stdout",
147155
"output_type": "stream",
148156
"text": [
149-
"Connection Handler Called\n",
150-
"Connection 98e7d448-5a16-40b0-bab6-afe9e7b2f96f in State invitation\n",
151-
"Connection Handler Called\n",
152-
"Connection 98e7d448-5a16-40b0-bab6-afe9e7b2f96f in State invitation\n",
153-
"Connection ID 98e7d448-5a16-40b0-bab6-afe9e7b2f96f\n",
157+
"Connection ID aa9a800e-9e65-4070-98d8-4f56971d6292\n",
154158
"Invitation\n",
155-
"{'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation', '@id': '4d68d1f0-83d4-49b9-bb96-9ac35283878d', 'serviceEndpoint': 'https://6512c3c6a284.ngrok.io', 'label': 'MEDIATOR', 'recipientKeys': ['8kPjcAdzMdf8giMrsWackJRDHEUeG6uoDu7ECVm1yGrY']}\n",
156-
"Connection Handler Called\n",
157-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State invitation\n",
158-
"Connection Handler Called\n",
159-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State invitation\n",
160-
"Connection Handler Called\n",
161-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State request\n",
162-
"Connection Handler Called\n",
163-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State request\n",
164-
"Connection Handler Called\n",
165-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State response\n",
166-
"Connection Handler Called\n",
167-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State response\n",
168-
"Connection Handler Called\n",
169-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State active\n",
170-
"Connection Handler Called\n",
171-
"Connection ab02b489-033d-4378-8202-ad31751d64f0 in State active\n"
159+
"{'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation', '@id': '61520ae1-80f8-473c-ae56-31472c252ceb', 'label': 'MEDIATOR', 'recipientKeys': ['EZ1hdmdntvBTMDW7SHFmQrL28hSV2U3Ctj83ERCzL34C'], 'serviceEndpoint': 'https://7976bda13d4b.ngrok.io'}\n"
172160
]
173161
}
174162
],
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}

0 commit comments

Comments
 (0)