1
- from aiohttp import (
2
- ClientSession ,
3
- )
4
1
from dataclasses import dataclass
5
- from pubsub import pub
6
-
7
- from .controllers .connections import ConnectionsController
8
- from .controllers .messaging import MessagingController
9
- from .controllers .mediation import MediationController
10
- from .controllers .schema import SchemaController
11
- from .controllers .wallet import WalletController
12
- from .controllers .definitions import DefinitionsController
13
- from .controllers .issuer import IssuerController
14
- from .controllers .proof import ProofController
15
- from .controllers .ledger import LedgerController
16
- from .controllers .credential import CredentialController
17
- from .controllers .multitenant import MultitenancyController
18
- from .controllers .server import ServerController
19
- from .controllers .oob import OOBController
20
- from .controllers .action_menu import ActionMenuController
21
- from .controllers .revocation import RevocationController
22
2
3
+ from .aries_controller_base import AriesAgentControllerBase
23
4
from .aries_webhook_server import AriesWebhookServer
24
5
25
6
import logging
28
9
29
10
30
11
@dataclass
31
- class AriesAgentController :
12
+ class AriesAgentController ( AriesAgentControllerBase ) :
32
13
"""The Aries Agent Controller class
33
14
34
15
This class allows you to interact with Aries by exposing the aca-py API.
35
-
36
- Attributes:
37
- ----------
38
- admin_url : str
39
- The URL for the Admin API
40
- is_multitenant : bool
41
- Initialise the multitenant interface (default is False)
42
- mediation : bool
43
- Initialise the mediation interface (default is False)
44
- api_key : str
45
- The API key (default is None)
46
16
"""
47
17
48
- admin_url : str
49
- is_multitenant : bool = False
50
- mediation : bool = False
51
- api_key : str = None
52
-
53
18
def __post_init__ (self ):
54
19
"""Constructs additional attributes and logic
55
20
Creates headers, instantiates a client sessions and initialises
56
21
the controller interfaces for the aries swagger API.
57
22
"""
58
23
59
- self .webhook_site = None
60
- self .connections_controller = None
61
-
62
- # Construct headers for Client Session and the session itself
63
- self .headers = {}
64
-
65
- if self .api_key :
66
- self .headers .update ({"X-API-Key" : self .api_key })
67
-
68
- self .client_session : ClientSession = ClientSession (
69
- headers = self .headers )
70
-
71
- # Instantiate controllers
72
- self .connections = ConnectionsController (
73
- self .admin_url ,
74
- self .client_session )
75
-
76
- self .messaging = MessagingController (
77
- self .admin_url ,
78
- self .client_session )
79
-
80
- self .proofs = ProofController (
81
- self .admin_url ,
82
- self .client_session )
83
-
84
- self .ledger = LedgerController (
85
- self .admin_url ,
86
- self .client_session )
87
-
88
- self .credentials = CredentialController (
89
- self .admin_url ,
90
- self .client_session )
91
-
92
- self .server = ServerController (
93
- self .admin_url ,
94
- self .client_session )
95
-
96
- self .oob = OOBController (
97
- self .admin_url ,
98
- self .client_session )
99
-
100
- if self .is_multitenant :
101
- self .multitenant = MultitenancyController (
102
- self .admin_url ,
103
- self .client_session )
104
-
105
- if self .mediation :
106
- self .mediation = MediationController (
107
- self .admin_url ,
108
- self .client_session )
109
-
110
- self .schema = SchemaController (
111
- self .admin_url ,
112
- self .client_session )
113
-
114
- self .wallet = WalletController (
115
- self .admin_url ,
116
- self .client_session )
117
-
118
- self .definitions = DefinitionsController (
119
- self .admin_url ,
120
- self .client_session )
121
-
122
- self .issuer = IssuerController (
123
- self .admin_url ,
124
- self .client_session ,
125
- self .connections ,
126
- self .wallet ,
127
- self .definitions )
128
-
129
- self .action_menu = ActionMenuController (
130
- self .admin_url ,
131
- self .client_session )
132
-
133
- self .revocations = RevocationController (
134
- self .admin_url ,
135
- self .client_session
136
- )
24
+ super ().__post_init__ ()
137
25
138
26
def webhook_server (
139
27
self ,
@@ -157,59 +45,6 @@ def webhook_server(
157
45
webhook_base = webhook_base ,
158
46
is_multitenant = self .is_multitenant )
159
47
160
- def update_api_key (self , api_key : str ):
161
- """Update the API Key attribute and the header
162
-
163
- Args:
164
- ----
165
- api_key : str
166
- The API Key
167
- """
168
- self .api_key = api_key
169
- self .headers .update ({"X-API-Key" : api_key })
170
- self .client_session .headers .update (self .headers )
171
-
172
- def remove_api_key (self ):
173
- """Removes the API key attribute and corresponding headers
174
- from the Client Session"""
175
- self .api_key = None
176
- if 'X-API-Key' in self .client_session .headers :
177
- del self .client_session .headers ['X-API-Key' ]
178
- del self .headers ['X-API-Key' ]
179
-
180
- def register_listeners (self , listeners , defaults = True ):
181
- """Registers the webhook listners
182
-
183
- Args:
184
- ----
185
- listeners : [dict]
186
- A collection of dictionaries comprised of "handler": handler (fct)
187
- and "topic":"topicname" key-value pairs
188
- defaults : bool
189
- Whether to connect to the default handlers for connections,
190
- basicmessage and present_proof (default is True)
191
- """
192
- try :
193
- if defaults :
194
- if self .connections :
195
- pub .subscribe (
196
- self .connections .default_handler ,
197
- "connections" )
198
- if self .messaging :
199
- pub .subscribe (
200
- self .messaging .default_handler ,
201
- "basicmessages" )
202
- if self .proofs :
203
- pub .subscribe (
204
- self .proofs .default_handler ,
205
- "present_proof" )
206
-
207
- for listener in listeners :
208
- self .add_listener (listener )
209
- except Exception as exc :
210
- logger .warning (
211
- f"Register webhooks listeners failed! { exc !r} occurred." )
212
-
213
48
def add_listener (self , listener ):
214
49
"""Subscribe to a listeners for a topic
215
50
@@ -228,42 +63,6 @@ def add_listener(self, listener):
228
63
logger .warning (
229
64
f"Adding webhooks listener failed! { exc !r} occurred." )
230
65
231
- def remove_listener (self , listener ):
232
- """Remove a listener for a topic
233
-
234
- Args:
235
- ----
236
- listener : dict
237
- A dictionary comprised of "handler": handler (fct) and
238
- "topic":"topicname" key-value pairs
239
- """
240
- try :
241
- if pub .isSubscribed (listener ["handler" ], listener ["topic" ]):
242
- pub .unsubscribe (listener ["handler" ], listener ["topic" ])
243
- else :
244
- logger .debug ("Listener not subscribed" , listener )
245
- except Exception as exc :
246
- logger .warning (
247
- f"Removing webhooks listener failed! { exc !r} occurred." )
248
-
249
- def remove_all_listeners (self , topic : str = None ):
250
- """Remove all listeners for one or all topics
251
-
252
- Args:
253
- ----
254
- topic : str
255
- The topic to stop listening for (default is None). Default will
256
- cause unsubscribing from all topics.
257
- """
258
- # Note: advanced use of function can include both listenerFilter and
259
- # topicFilter for this
260
- # Add when needed
261
- try :
262
- pub .unsubAll (topicName = topic )
263
- except Exception as exc :
264
- logger .warning (
265
- f"Removing all webhooks listeners failed! { exc !r} occurred." )
266
-
267
66
async def listen_webhooks (self ):
268
67
try :
269
68
await self .webhook_server .listen_webhooks ()
@@ -272,14 +71,3 @@ async def listen_webhooks(self):
272
71
except Exception as exc :
273
72
logger .warning (
274
73
f"Listening webhooks failed! { exc !r} occurred." )
275
-
276
- async def terminate (self ):
277
- await self .client_session .close ()
278
- try :
279
- await self .webhook_server .terminate ()
280
- except AttributeError :
281
- # There is no webhook listener
282
- return
283
- except Exception as exc :
284
- logger .warning (
285
- f"Terminate webhooks listener exception!\n { exc !r} occurred." )
0 commit comments