23
23
from .controllers .action_menu import ActionMenuController
24
24
from .controllers .revocation import RevocationController
25
25
26
+ from .aries_webhook_listener import AriesWebhookListener
27
+
26
28
import logging
27
29
28
30
logger = logging .getLogger ("aries_controller" )
@@ -48,7 +50,7 @@ class AriesAgentController:
48
50
Specify whether to create connecitons (default is True)
49
51
messaging : bool
50
52
Initialise the messaging interface (default is True)
51
- multitenant : bool
53
+ is_multitenant : bool
52
54
Initialise the multitenant interface (default is False)
53
55
mediation : bool
54
56
Initialise the mediation interface (default is False)
@@ -62,24 +64,26 @@ class AriesAgentController:
62
64
The API key (default is None)
63
65
tenant_jwt: str
64
66
The tenant JW token (default is None)
67
+ wallet_id : str
68
+ The tenant wallet identifier
65
69
"""
66
70
67
71
## TODO rethink how to initialise. Too many args?
68
72
## is it important to let users config connections/issuer etc
69
- webhook_host : str
70
- webhook_port : int
71
73
admin_url : str
74
+ webhook_host : str = None
75
+ webhook_port : int = None
72
76
webhook_base : str = ""
73
77
connections : bool = True
74
78
messaging : bool = True
75
- multitenant : bool = False
79
+ is_multitenant : bool = False
76
80
mediation : bool = False
77
81
issuer : bool = True
78
82
action_menu : bool = True
79
83
revocations : bool = True
80
84
api_key : str = None
81
85
tenant_jwt : str = None
82
- wallet_id : str = "base"
86
+ wallet_id : str = None
83
87
84
88
85
89
def __post_init__ (self ):
@@ -101,7 +105,9 @@ def __post_init__(self):
101
105
102
106
103
107
self .client_session : ClientSession = ClientSession (headers = self .headers )
104
-
108
+
109
+ self .webhook_listener : AriesWebhookListener = AriesWebhookListener (webhook_host = self .webhook_host , webhook_port = self .webhook_port , webhook_base = self .webhook_base , is_multitenant = self .is_multitenant )
110
+
105
111
# Instantiate controllers based on the provided attributes
106
112
if self .connections :
107
113
self .connections = ConnectionsController (self .admin_url , self .client_session )
@@ -115,7 +121,7 @@ def __post_init__(self):
115
121
self .server = ServerController (self .admin_url , self .client_session )
116
122
self .oob = OOBController (self .admin_url , self .client_session )
117
123
118
- if self .multitenant :
124
+ if self .is_multitenant :
119
125
self .multitenant = MultitenancyController (self .admin_url , self .client_session )
120
126
121
127
if self .mediation :
@@ -137,17 +143,29 @@ def __post_init__(self):
137
143
self .client_session
138
144
)
139
145
146
+ def update_wallet_id (self , wallet_id : str ):
147
+ """This wallet_id is used to register for webhooks specific to this sub_wallet
148
+
149
+ Args:
150
+ ----
151
+ wallet_id : str
152
+ The tenant wallet identifier
153
+ """
154
+ self .wallet_id = wallet_id
140
155
141
156
142
- def update_tenant_jwt (self , tenant_jwt : str ):
157
+ def update_tenant_jwt (self , tenant_jwt : str , wallet_id : str ):
143
158
"""Update the tenant JW token attribute and the header
144
159
145
160
Args:
146
161
----
147
162
tenant_jwt : str
148
163
The tenant's JW token
164
+ wallet_id : str
165
+ The tenant wallet identifier
149
166
"""
150
167
self .tenant_jwt = tenant_jwt
168
+ self .update_wallet_id (wallet_id )
151
169
self .headers .update ({'Authorization' : 'Bearer ' + tenant_jwt , 'content-type' : "application/json" })
152
170
self .client_session .headers .update (self .headers )
153
171
@@ -210,8 +228,6 @@ def register_listeners(self, listeners, defaults=True):
210
228
print (f"Register webhooks listeners failed! { exc !r} occurred." )
211
229
logger .warn (f"Register webhooks listeners failed! { exc !r} occurred." )
212
230
213
-
214
-
215
231
def add_listener (self , listener ):
216
232
"""Subscribe to a listeners for a topic
217
233
@@ -267,77 +283,15 @@ def remove_all_listeners(self, topic: str = None):
267
283
pub .unsubAll (topicName = topic )
268
284
except Exception as exc :
269
285
print (f"Removing all webhooks listeners failed! { exc !r} occurred." )
270
- logger .warn (f"Removing all webhooks listeners failed! { exc !r} occurred." )
271
-
286
+ logger .warning (f"Removing all webhooks listeners failed! { exc !r} occurred." )
272
287
273
288
274
289
async def listen_webhooks (self ):
275
- """Create a server to listen to webhooks"""
276
- try :
277
- app = web .Application ()
278
- app .add_routes ([web .post (self .webhook_base + "/{wallet}/topic/{topic}/" , self ._receive_webhook )])
279
- runner = web .AppRunner (app )
280
- await runner .setup ()
281
- self .webhook_site = web .TCPSite (runner , self .webhook_host , self .webhook_port )
282
- await self .webhook_site .start ()
283
- except Exception as exc :
284
- print (f"Listening webhooks failed! { exc !r} occurred." )
285
- logger .warn (f"Listening webhooks failed! { exc !r} occurred." )
286
-
287
-
288
-
289
- async def _receive_webhook (self , request : ClientRequest ):
290
- """Helper to receive webhooks by requesting it
291
-
292
- Args:
293
- ----
294
- request : ClientRequest
295
- The client request to which the corresponding webhooks shall be received
296
-
297
- Returns:
298
- -------
299
- Response:
300
- A response with status 200
301
- """
302
- topic = request .match_info ["topic" ]
303
- wallet = request .match_info ["wallet" ]
304
- print ("wallet" , wallet )
305
- try :
306
- payload = await request .json ()
307
- await self ._handle_webhook (wallet , topic , payload )
308
- return web .Response (status = 200 )
309
- except Exception as exc :
310
- logger .warn (f"Receiving webhooks failed! { exc !r} occurred." )
311
-
312
-
313
-
314
- async def _handle_webhook (self , wallet , topic , payload ):
315
- """Helper handling a webhook
316
-
317
- Args:
318
- ----
319
- topic : str
320
- The topic to handle webhooks for
321
- payload : dict
322
- A JSON-like dictionary representation of the payload
323
- """
324
- try :
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 )
329
- # return web.Response(status=200)
330
- except Exception as exc :
331
- logger .warn (f"Handling webhooks failed! { exc !r} occurred when trying to handle this topic: { topic } " )
332
-
290
+ # self.webhook_listener: AriesWebhookListener = AriesWebhookListener(webhook_host=webhook_host, webhook_port=webhook_port, webhook_base=webhook_base, is_multitenant=is_multitenant)
333
291
292
+ if self .webhook_listener :
293
+ await self .webhook_listener .listen_webhooks ()
334
294
335
295
async def terminate (self ):
336
- """Terminate the controller client session and webhook listeners"""
337
- try :
338
- await self .client_session .close ()
339
- if self .webhook_site :
340
- await self .webhook_site .stop ()
341
- except Exception as exc :
342
- print (f"Terminating webhooks listener failed! { exc !r} occurred." )
343
- logger .warn (f"Terminating webhooks listener failed! { exc !r} occurred." )
296
+ await self .client_session .close ()
297
+ await self .webhook_listener .terminate ()
0 commit comments