Skip to content

Commit efe3abe

Browse files
authored
Merge pull request #41 from didx-xyz/feature/context_manager
Feature/context manager
2 parents 070fa21 + 6adaea5 commit efe3abe

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

aries_cloudcontroller/aries_controller_base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from aiohttp import (
22
ClientSession,
33
)
4-
from abc import ABC
4+
from contextlib import AbstractAsyncContextManager
55
from dataclasses import dataclass
66
from pubsub import pub
77

@@ -27,7 +27,7 @@
2727

2828

2929
@dataclass
30-
class AriesAgentControllerBase(ABC):
30+
class AriesAgentControllerBase(AbstractAsyncContextManager):
3131
"""The Aries Agent Controller class
3232
3333
This class allows you to interact with Aries by exposing the aca-py API.
@@ -97,6 +97,12 @@ def __post_init__(self):
9797

9898
self.revocations = RevocationController(self.admin_url, self.client_session)
9999

100+
async def __aenter__(self):
101+
return self
102+
103+
async def __aexit__(self, exc_type, exc_value, exc_traceback):
104+
await self.terminate()
105+
100106
def init_webhook_server(self):
101107
raise NotImplementedError
102108

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def parse_requirements(filename):
2222
if __name__ == "__main__":
2323
setup(
2424
name=PACKAGE_NAME,
25-
version="0.2.6",
25+
version="0.2.7",
2626
description="A simple python package for controlling an aries agent through the admin-api interface",
2727
long_description=long_description,
2828
long_description_content_type="text/markdown",

tests/test_aries_controller_base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,19 @@ async def test_terminate(self, caplog):
211211
with pytest.raises(AttributeError):
212212
assert ac.webhook_server is None
213213
await ac.terminate()
214+
215+
@pytest.mark.asyncio
216+
async def test_context_manager(self, caplog):
217+
caplog.set_level(logging.INFO)
218+
api_key = "123456789"
219+
220+
async with AriesAgentControllerBase(admin_url="", api_key=api_key) as ac:
221+
assert ac.client_session
222+
assert ac.api_key == api_key
223+
assert ac.headers == {"X-API-Key": api_key}
224+
ac.remove_api_key()
225+
226+
assert ac.headers == {}
227+
assert ac.api_key is None
228+
assert ac.client_session.headers == {}
229+
assert "Client Session closed." in caplog.text

0 commit comments

Comments
 (0)