Skip to content

feat: 85 remove initialize and close from the client class #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions eventsourcingdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ async def __aexit__(
) -> None:
await self.__http_client.__aexit__(exc_type, exc_val, exc_tb)

async def initialize(self) -> None:
await self.__http_client.initialize()

async def close(self) -> None:
await self.__http_client.close()

@property
def http_client(self) -> HttpClient:
return self.__http_client
Expand Down
21 changes: 11 additions & 10 deletions eventsourcingdb/http_client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import aiohttp
from aiohttp import ClientSession

from ..errors.custom_error import CustomError

from .get_get_headers import get_get_headers
from .get_post_headers import get_post_headers
from .response import Response
Expand All @@ -21,7 +19,7 @@ def __init__(
self.__session: ClientSession | None = None

async def __aenter__(self) -> 'HttpClient':
await self.initialize()
await self.__initialize()
return self

async def __aexit__(
Expand All @@ -30,12 +28,16 @@ async def __aexit__(
exc_val: BaseException | None = None,
exc_tb: TracebackType | None = None,
) -> None:
await self.close()
await self.__close()

async def __initialize(self) -> None:
# If a session already exists, close it first to prevent leaks
if self.__session is not None:
await self.__session.close()

async def initialize(self) -> None:
self.__session = aiohttp.ClientSession()
self.__session = aiohttp.ClientSession(connector_owner=True)

async def close(self) -> None:
async def __close(self) -> None:
if self.__session is not None:
await self.__session.close()
self.__session = None
Expand All @@ -49,7 +51,7 @@ def join_segments(first: str, *rest: str) -> str:

async def post(self, path: str, request_body: str) -> Response:
if self.__session is None:
await self.initialize()
await self.__initialize()

url_path = HttpClient.join_segments(self.__base_url, path)
headers = get_post_headers(self.__api_token)
Expand All @@ -70,8 +72,7 @@ async def get(
with_authorization: bool = True,
) -> Response:
if self.__session is None:
raise CustomError(
"HTTP client session not initialized. Call initialize() before making requests.")
await self.__initialize()

async def __request_executor() -> Response:
url_path = HttpClient.join_segments(self.__base_url, path)
Expand Down
28 changes: 24 additions & 4 deletions tests/shared/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ def _create_container(cls, api_token, image_tag) -> Container:
@staticmethod
async def _initialize_clients(container, api_token) -> tuple[Client, Client]:
with_authorization_client = container.get_client()
await with_authorization_client.initialize()

with_invalid_url_client = Client(
base_url='http://localhost.invalid',
api_token=api_token
)
await with_invalid_url_client.initialize()

return with_authorization_client, with_invalid_url_client

@classmethod
Expand Down Expand Up @@ -111,12 +107,36 @@ def get_client(self, client_type: str = CLIENT_TYPE_WITH_AUTH) -> Client:

raise ValueError(f'Unknown client type: {client_type}')

async def __aenter__(self) -> 'Database':
return self

async def __aexit__(
self,
exc_type: type[BaseException] | None = None,
exc_val: BaseException | None = None,
exc_tb: object | None = None
) -> None:
await self.stop()

def get_base_url(self) -> str:
return self.__container.get_base_url()

def get_api_token(self) -> str:
return self.__container.get_api_token()

async def stop(self) -> None:
if self.__with_authorization_client:
try:
await self.__with_authorization_client.__aexit__(None, None, None)
except (ConnectionError) as e:
logging.warning("Error closing authorization client: %s", e)

if self.__with_invalid_url_client:
try:
await self.__with_invalid_url_client.__aexit__(None, None, None)
except (ConnectionError) as e:
logging.warning("Error closing invalid URL client: %s", e)

# Then stop the container
if (container := getattr(self.__class__, '_Database__container', None)):
container.stop()
2 changes: 0 additions & 2 deletions tests/shared/start_local_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,4 @@ def stop_server() -> None:
f'http://localhost:{local_http_server.port}',
'access-token',
)
await client.initialize()

return client, stop_server