Skip to content

Commit

Permalink
refactor: remove support for using an existing session
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <dbluhm@pm.me>
  • Loading branch information
dbluhm committed Apr 9, 2024
1 parent 9f38d9b commit fcb3541
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions acapy_controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def __init__(
subwallet_token: Optional[str] = None,
headers: Optional[Mapping[str, str]] = None,
event_queue: Optional[Queue[Event]] = None,
session: Optional[ClientSession] = None,
):
"""Initialize and ACA-Py Controller."""
self.base_url = base_url
Expand All @@ -135,7 +134,6 @@ def __init__(
self.headers["Authorization"] = f"Bearer {subwallet_token}"

self._event_queue: Optional[Queue[Event]] = event_queue
self._session: Optional[ClientSession] = session

self._stack: Optional[AsyncExitStack] = None

Expand Down Expand Up @@ -169,10 +167,6 @@ async def setup(self) -> "Controller":
self._stack = await AsyncExitStack().__aenter__()
if not self._event_queue:
self._event_queue = await self._stack.enter_async_context(EventQueue(self))
if not self._session:
self._session = await self._stack.enter_async_context(
ClientSession(base_url=self.base_url, headers=self.headers)
)

# Get settings
settings = await self.record("settings")
Expand Down Expand Up @@ -251,38 +245,31 @@ async def request(
response: Optional[Type[T]] = None,
) -> Union[T, Mapping[str, Any]]:
"""Make an HTTP request."""
if self._session:
session = self._session
else:
session = await ClientSession(
base_url=self.base_url, headers=self.headers
).__aenter__()

headers = dict(headers or {})
headers.update(self.headers)

if method == "GET" or method == "DELETE":
async with session.request(
method, url, params=params, headers=headers
) as resp:
body = await self._handle_response(resp)
value = _deserialize(body, response)

elif method == "POST" or method == "PUT":
json_ = _serialize(json)
if not data and not json_:
json_ = {}

async with session.request(
method, url, data=data, json=json_, params=params
) as resp:
body = await self._handle_response(resp, data=data, json=json_)
value = _deserialize(body, response)
else:
raise ValueError(f"Unsupported method {method}")

if not self._session:
await session.__aexit__(None, None, None)
async with ClientSession(
base_url=self.base_url, headers=self.headers
) as session:
headers = dict(headers or {})
headers.update(self.headers)

if method == "GET" or method == "DELETE":
async with session.request(
method, url, params=params, headers=headers
) as resp:
body = await self._handle_response(resp)
value = _deserialize(body, response)

elif method == "POST" or method == "PUT":
json_ = _serialize(json)
if not data and not json_:
json_ = {}

async with session.request(
method, url, data=data, json=json_, params=params
) as resp:
body = await self._handle_response(resp, data=data, json=json_)
value = _deserialize(body, response)
else:
raise ValueError(f"Unsupported method {method}")

return value

Expand Down

0 comments on commit fcb3541

Please sign in to comment.