Skip to content

Commit 0790638

Browse files
authored
fix: Fix async constructors (#36)
* fix: Fix async constructors * fix: Update import to be realtive
1 parent 4048989 commit 0790638

File tree

13 files changed

+33
-71
lines changed

13 files changed

+33
-71
lines changed

eventsourcingdb_client_python/client.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919

2020

2121
class Client(AbstractBaseClient):
22-
__create_key = object()
23-
24-
@classmethod
25-
async def create(
26-
cls,
22+
def __init__(
23+
self,
2724
base_url: str,
2825
access_token: str,
2926
options: ClientOptions = ClientOptions()
30-
) -> 'Client':
27+
):
3128
configuration = ClientConfiguration(
3229
base_url=base_url,
3330
timeout_seconds=options.timeout_seconds,
@@ -36,19 +33,10 @@ async def create(
3633
max_tries=options.max_tries
3734
)
3835

39-
http_client = await HttpClient.create(configuration)
40-
41-
return cls(Client.__create_key, http_client)
42-
43-
def __init__(
44-
self,
45-
create_key,
46-
http_client: HttpClient
47-
):
48-
assert create_key == Client.__create_key, \
49-
'Client objects must be created using Client.create.'
36+
self.__http_client = HttpClient(configuration)
5037

51-
self.__http_client = http_client
38+
async def initialize(self) -> None:
39+
await self.__http_client.initialize()
5240

5341
async def close(self):
5442
await self.__http_client.close()

eventsourcingdb_client_python/event/source.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
22

3-
from eventsourcingdb_client_python.event.event_candidate import EventCandidate
4-
from eventsourcingdb_client_python.event.tracing import TracingContext
3+
from ..event.tracing import TracingContext
4+
from ..event.event_candidate import EventCandidate
55

66

77
@dataclass

eventsourcingdb_client_python/handlers/read_event_types/event_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
22
from typing import TypeVar
33

4-
from eventsourcingdb_client_python.errors.validation_error import ValidationError
4+
from ...errors.validation_error import ValidationError
55

66
Self = TypeVar("Self", bound="EventType")
77

eventsourcingdb_client_python/http_client/http_client.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,20 @@ class UninitializedError(CustomError):
2424

2525

2626
class HttpClient:
27-
__create_key = object()
28-
29-
@classmethod
30-
async def create(cls, client_configuration: ClientConfiguration) -> 'HttpClient':
31-
session = aiohttp.ClientSession(
32-
timeout=aiohttp.ClientTimeout(
33-
connect=client_configuration.timeout_seconds,
34-
sock_read=client_configuration.timeout_seconds
35-
)
36-
)
37-
38-
return cls(HttpClient.__create_key, client_configuration, session)
39-
4027
def __init__(
4128
self,
42-
create_key,
43-
client_configuration: ClientConfiguration,
44-
session: aiohttp.ClientSession
29+
client_configuration: ClientConfiguration
4530
):
46-
assert create_key == HttpClient.__create_key, \
47-
'HttpClient objects must be created using HttpClient.create.'
48-
4931
self.__client_configuration: ClientConfiguration = client_configuration
50-
self.__session: ClientSession = session
32+
self.__session: ClientSession | None = None
33+
34+
async def initialize(self) -> None:
35+
self.__session = aiohttp.ClientSession(
36+
timeout=aiohttp.ClientTimeout(
37+
connect=self.__client_configuration.timeout_seconds,
38+
sock_read=self.__client_configuration.timeout_seconds
39+
)
40+
)
5141

5242
async def close(self):
5343
if self.__session is not None:

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from eventsourcingdb_client_python.event.source import Source
66
from eventsourcingdb_client_python.event.tracing import TracingContext
77
from eventsourcingdb_client_python.http_client.http_client import HttpClient
8+
from .shared.build_database import build_database
89
from .shared.database import Database
910

1011
from .shared.start_local_http_server import \
@@ -14,6 +15,10 @@
1415
pytest_plugins = ('pytest_asyncio', )
1516

1617

18+
def pytest_sessionstart():
19+
build_database('tests/shared/docker/eventsourcingdb')
20+
21+
1722
@pytest_asyncio.fixture
1823
async def get_http_client():
1924
stop_server: StopServer | None = None

tests/shared/containerized_testing_database.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ async def create(
3232
container = image.run(command, True, True)
3333
exposed_port = container.get_exposed_port(3_000)
3434
base_url = f'http://127.0.0.1:{exposed_port}'
35-
client = await Client.create(base_url, access_token=access_token, options=options)
35+
client = Client(base_url, access_token=access_token, options=options)
36+
await client.initialize()
3637

3738
await client.ping()
3839

tests/shared/database.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ async def create(cls) -> 'Database':
3535
ClientOptions()
3636
)
3737

38+
with_invalid_url_client = Client(
39+
base_url='http://localhost.invalid', access_token=access_token
40+
)
41+
await with_invalid_url_client.initialize()
3842
with_invalid_url = TestingDatabase(
39-
await Client.create(base_url='http://localhost.invalid', access_token=access_token)
43+
with_invalid_url_client
4044
)
4145

4246
return cls(Database.__create_key, with_authorization, with_invalid_url)

tests/shared/start_local_http_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ def stop_server():
5959
server.terminate()
6060
server.join()
6161

62-
client = await Client.create(
62+
client = Client(
6363
f'http://localhost:{port}',
6464
'access-token',
6565
ClientOptions(max_tries=2)
6666
)
67+
await client.initialize()
6768

6869
return client, stop_server

tests/test_observe_events.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
IfEventIsMissingDuringObserve
1414
from .conftest import TestData
1515

16-
from .shared.build_database import build_database
1716
from .shared.database import Database
1817
from .shared.event.assert_event import assert_event
1918
from .shared.start_local_http_server import \
@@ -23,10 +22,6 @@
2322

2423

2524
class TestObserveEvents:
26-
@classmethod
27-
def setup_class(cls):
28-
build_database('tests/shared/docker/eventsourcingdb')
29-
3025
@staticmethod
3126
@pytest.mark.asyncio
3227
async def test_throws_error_if_server_is_not_reachable(

tests/test_ping.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from eventsourcingdb_client_python.client import Client
77
from eventsourcingdb_client_python.errors.server_error import ServerError
8-
from .shared.build_database import build_database
98
from .shared.database import Database
109
from .shared.start_local_http_server import \
1110
AttachHandler, \
@@ -14,12 +13,6 @@
1413

1514

1615
class TestPing:
17-
database: Database
18-
19-
@classmethod
20-
def setup_class(cls):
21-
build_database('tests/shared/docker/eventsourcingdb')
22-
2316
@staticmethod
2417
@pytest.mark.asyncio
2518
async def test_throws_no_error_if_server_is_reachable(database: Database):

0 commit comments

Comments
 (0)