Skip to content

Commit

Permalink
fix: aiohttp.ClientSession needs to be created inside an async function
Browse files Browse the repository at this point in the history
  • Loading branch information
Psycojoker committed Aug 14, 2024
1 parent ef1f77d commit d2f56b0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
59 changes: 37 additions & 22 deletions src/aleph/sdk/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class AlephHttpClient(AlephClient):
api_server: str
http_session: aiohttp.ClientSession
_http_session: Optional[aiohttp.ClientSession]

def __init__(
self,
Expand All @@ -48,35 +48,50 @@ def __init__(
if not self.api_server:
raise ValueError("Missing API host")

connector: Union[aiohttp.BaseConnector, None]
self.connector: Union[aiohttp.BaseConnector, None]
unix_socket_path = api_unix_socket or settings.API_UNIX_SOCKET

if ssl_context:
connector = aiohttp.TCPConnector(ssl=ssl_context)
self.connector = aiohttp.TCPConnector(ssl=ssl_context)
elif unix_socket_path and allow_unix_sockets:
check_unix_socket_valid(unix_socket_path)
connector = aiohttp.UnixConnector(path=unix_socket_path)
self.connector = aiohttp.UnixConnector(path=unix_socket_path)
else:
connector = None

# ClientSession timeout defaults to a private sentinel object and may not be None.
self.http_session = (
aiohttp.ClientSession(
base_url=self.api_server,
connector=connector,
timeout=timeout,
json_serialize=extended_json_encoder,
)
if timeout
else aiohttp.ClientSession(
base_url=self.api_server,
connector=connector,
json_serialize=lambda obj: json.dumps(
obj, default=extended_json_encoder
),
self.connector = None

self.timeout = timeout
self._http_session = None

@property
def http_session(self) -> aiohttp.ClientSession:
if self._http_session is None:
raise Exception(
f"{self.__class__.__name__} can only be using within an async context manager.\n\n"
"Please use it this way:\n\n"
" async with {self.__class__.__name__}(...) as client:"
)
)

return self._http_session

async def __aenter__(self) -> "AlephHttpClient":
if self._http_session is None:
self._http_session = (
aiohttp.ClientSession(
base_url=self.api_server,
connector=self.connector,
timeout=self.timeout,
json_serialize=extended_json_encoder,
)
if self.timeout
else aiohttp.ClientSession(
base_url=self.api_server,
connector=self.connector,
json_serialize=lambda obj: json.dumps(
obj, default=extended_json_encoder
),
)
)

return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def mock_session_with_post_success(
client = AuthenticatedAlephHttpClient(
account=ethereum_account, api_server="http://localhost"
)
client.http_session = http_session
client._http_session = http_session

return client

Expand Down Expand Up @@ -254,7 +254,7 @@ def get(self, *_args, **_kwargs):
http_session = MockHttpSession()

client = AlephHttpClient(api_server="http://localhost")
client.http_session = http_session
client._http_session = http_session

return client

Expand All @@ -281,6 +281,6 @@ def post(self, *_args, **_kwargs):
client = AuthenticatedAlephHttpClient(
account=ethereum_account, api_server="http://localhost"
)
client.http_session = http_session
client._http_session = http_session

return client

0 comments on commit d2f56b0

Please sign in to comment.