Skip to content

Commit feed475

Browse files
committed
WIP Add abtract base class for agent controller.
* needs testing * gets the general idea across of how to abstract this
1 parent ed31d09 commit feed475

File tree

3 files changed

+253
-217
lines changed

3 files changed

+253
-217
lines changed
Lines changed: 3 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
1-
from aiohttp import (
2-
ClientSession,
3-
)
41
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
222

3+
from .aries_controller_base import AriesAgentControllerBase
234
from .aries_webhook_server import AriesWebhookServer
245

256
import logging
@@ -28,112 +9,19 @@
289

2910

3011
@dataclass
31-
class AriesAgentController:
12+
class AriesAgentController(AriesAgentControllerBase):
3213
"""The Aries Agent Controller class
3314
3415
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)
4616
"""
4717

48-
admin_url: str
49-
is_multitenant: bool = False
50-
mediation: bool = False
51-
api_key: str = None
52-
5318
def __post_init__(self):
5419
"""Constructs additional attributes and logic
5520
Creates headers, instantiates a client sessions and initialises
5621
the controller interfaces for the aries swagger API.
5722
"""
5823

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__()
13725

13826
def webhook_server(
13927
self,
@@ -157,59 +45,6 @@ def webhook_server(
15745
webhook_base=webhook_base,
15846
is_multitenant=self.is_multitenant)
15947

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-
21348
def add_listener(self, listener):
21449
"""Subscribe to a listeners for a topic
21550
@@ -228,42 +63,6 @@ def add_listener(self, listener):
22863
logger.warning(
22964
f"Adding webhooks listener failed! {exc!r} occurred.")
23065

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-
26766
async def listen_webhooks(self):
26867
try:
26968
await self.webhook_server.listen_webhooks()
@@ -272,14 +71,3 @@ async def listen_webhooks(self):
27271
except Exception as exc:
27372
logger.warning(
27473
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

Comments
 (0)