Skip to content

Commit 82a8788

Browse files
committed
Merge branch 'master' of https://github.com/OpenMined/PyDentity into improvement-controller-session
2 parents 87ad9ed + 4cb7c87 commit 82a8788

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

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

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
)
66
from dataclasses import dataclass
77
from pubsub import pub
8+
import sys
89

910
from .controllers.connections import ConnectionsController
1011
from .controllers.messaging import MessagingController
@@ -191,16 +192,21 @@ def register_listeners(self, listeners, defaults=True):
191192
Whether to connect to the default handlers for connections, basicmessage and present_proof
192193
(default is True)
193194
"""
194-
if defaults:
195-
if self.connections:
196-
pub.subscribe(self.connections.default_handler, "connections")
197-
if self.messaging:
198-
pub.subscribe(self.messaging.default_handler, "basicmessages")
199-
if self.proofs:
200-
pub.subscribe(self.proofs.default_handler, "present_proof")
195+
try:
196+
if defaults:
197+
if self.connections:
198+
pub.subscribe(self.connections.default_handler, "connections")
199+
if self.messaging:
200+
pub.subscribe(self.messaging.default_handler, "basicmessages")
201+
if self.proofs:
202+
pub.subscribe(self.proofs.default_handler, "present_proof")
203+
204+
for listener in listeners:
205+
self.add_listener(listener)
206+
except Exception as exc:
207+
print(f"Register webhooks listeners failed! {exc!r} occurred.")
208+
logger.warn(f"Register webhooks listeners failed! {exc!r} occurred.")
201209

202-
for listener in listeners:
203-
self.add_listener(listener)
204210

205211

206212
def add_listener(self, listener):
@@ -211,7 +217,12 @@ def add_listener(self, listener):
211217
listener : dict
212218
A dictionary comprised of a "handler": handler (fct) and a "topic":"topicname" key-value pairs
213219
"""
214-
pub.subscribe(listener["handler"], listener["topic"])
220+
try:
221+
pub.subscribe(listener["handler"], listener["topic"])
222+
except Exception as exc:
223+
print(f"Adding webhooks listener failed! {exc!r} occurred.")
224+
logger.warn(f"Adding webhooks listener failed! {exc!r} occurred.")
225+
215226

216227

217228
def remove_listener(self, listener):
@@ -222,10 +233,15 @@ def remove_listener(self, listener):
222233
listener : dict
223234
A dictionary comprised of a "handler": handler (fct) and a "topic":"topicname" key-value pairs
224235
"""
225-
if pub.isSubscribed(listener["handler"], listener["topic"]):
226-
pub.unsubscribe(listener["handler"], listener["topic"])
227-
else:
228-
logger.debug("Listener not subscribed", listener)
236+
try:
237+
if pub.isSubscribed(listener["handler"], listener["topic"]):
238+
pub.unsubscribe(listener["handler"], listener["topic"])
239+
else:
240+
logger.debug("Listener not subscribed", listener)
241+
except Exception as exc:
242+
print(f"Removing webhooks listener failed! {exc!r} occurred.")
243+
logger.warn(f"Removing webhooks listener failed! {exc!r} occurred.")
244+
229245

230246

231247
def remove_all_listeners(self, topic: str = None):
@@ -238,17 +254,27 @@ def remove_all_listeners(self, topic: str = None):
238254
"""
239255
# Note advanced use of function can include both listenerFilter and topicFilter for this
240256
# Add when needed
241-
pub.unsubAll(topicName=topic)
257+
try:
258+
pub.unsubAll(topicName=topic)
259+
except Exception as exc:
260+
print(f"Removing all webhooks listeners failed! {exc!r} occurred.")
261+
logger.warn(f"Removing all webhooks listeners failed! {exc!r} occurred.")
262+
242263

243264

244265
async def listen_webhooks(self):
245266
"""Create a server to listen to webhooks"""
246-
app = web.Application()
247-
app.add_routes([web.post(self.webhook_base + "/topic/{topic}/", self._receive_webhook)])
248-
runner = web.AppRunner(app)
249-
await runner.setup()
250-
self.webhook_site = web.TCPSite(runner, self.webhook_host, self.webhook_port)
251-
await self.webhook_site.start()
267+
try:
268+
app = web.Application()
269+
app.add_routes([web.post(self.webhook_base + "/topic/{topic}/", self._receive_webhook)])
270+
runner = web.AppRunner(app)
271+
await runner.setup()
272+
self.webhook_site = web.TCPSite(runner, self.webhook_host, self.webhook_port)
273+
await self.webhook_site.start()
274+
except Exception as exc:
275+
print(f"Listening webhooks failed! {exc!r} occurred.")
276+
logger.warn(f"Listening webhooks failed! {exc!r} occurred.")
277+
252278

253279

254280
async def _receive_webhook(self, request: ClientRequest):
@@ -265,9 +291,13 @@ async def _receive_webhook(self, request: ClientRequest):
265291
A response with status 200
266292
"""
267293
topic = request.match_info["topic"]
268-
payload = await request.json()
269-
await self._handle_webhook(topic, payload)
270-
return web.Response(status=200)
294+
try:
295+
payload = await request.json()
296+
await self._handle_webhook(topic, payload)
297+
return web.Response(status=200)
298+
except Exception as exc:
299+
logger.warn(f"Receiving webhooks failed! {exc!r} occurred.")
300+
271301

272302

273303
async def _handle_webhook(self, topic, payload):
@@ -280,13 +310,21 @@ async def _handle_webhook(self, topic, payload):
280310
payload : dict
281311
A JSON-like dictionary representation of the payload
282312
"""
283-
logging.debug(f"Handle Webhook - {topic}", payload)
284-
pub.sendMessage(topic, payload=payload)
285-
# return web.Response(status=200)
313+
try:
314+
logging.debug(f"Handle Webhook - {topic}", payload)
315+
pub.sendMessage(topic, payload=payload)
316+
# return web.Response(status=200)
317+
except Exception as exc:
318+
logger.warn(f"Handling webhooks failed! {exc!r} occurred when trying to handle this topic: {topic}")
319+
286320

287321

288322
async def terminate(self):
289323
"""Terminate the controller client session and webhook listeners"""
290-
await self.client_session.close()
291-
if self.webhook_site:
292-
await self.webhook_site.stop()
324+
try:
325+
await self.client_session.close()
326+
if self.webhook_site:
327+
await self.webhook_site.stop()
328+
except Exception as exc:
329+
print(f"Terminating webhooks listener failed! {exc!r} occurred.")
330+
logger.warn(f"Terminating webhooks listener failed! {exc!r} occurred.")

tutorials/1. Learning Aries, ACA-Py and the Basic Controller/notebooks/alice/3 Messages/Part 1 - Basic Message.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"metadata": {},
122122
"outputs": [],
123123
"source": [
124-
"await agent_controller.messaging.trust_ping(id)"
124+
"await agent_controller.messaging.trust_ping(id, \"hello, world!\")"
125125
]
126126
},
127127
{

tutorials/1. Learning Aries, ACA-Py and the Basic Controller/notebooks/bob/3 Messages/Part 1 - Basic Message.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
"name": "python",
167167
"nbconvert_exporter": "python",
168168
"pygments_lexer": "ipython3",
169-
"version": "3.7.6"
169+
"version": "3.8.5"
170170
}
171171
},
172172
"nbformat": 4,

0 commit comments

Comments
 (0)