Skip to content

Commit

Permalink
Merge pull request #5 from betaboon/feat-basic-connection-string-support
Browse files Browse the repository at this point in the history
feat: add basic connection-string-support
  • Loading branch information
betaboon authored Nov 11, 2022
2 parents ba821b6 + 14ae96c commit e76e777
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 15 deletions.
2 changes: 1 addition & 1 deletion eventstoredb/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from eventstoredb.client.client import Client
from eventstoredb.client.types import ClientOptions
from eventstoredb.client.options import ClientOptions
9 changes: 6 additions & 3 deletions eventstoredb/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from grpclib.client import Channel
from grpclib.exceptions import GRPCError

from eventstoredb.client.types import ClientOptions
from eventstoredb.client.options import ClientOptions
from eventstoredb.events import EventData, ReadEvent
from eventstoredb.generated.event_store.client.persistent_subscriptions import (
PersistentSubscriptionsStub,
Expand Down Expand Up @@ -49,8 +49,11 @@


class Client:
def __init__(self, options: ClientOptions) -> None:
self.options = options
def __init__(self, options: ClientOptions | str) -> None:
if isinstance(options, str):
self.options = ClientOptions.from_connection_string(options)
else:
self.options = options
self._channel: Channel | None = None

@property
Expand Down
43 changes: 43 additions & 0 deletions eventstoredb/client/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

from dataclasses import dataclass

from yarl import URL


class MalformedConnectionString(ValueError):
def __init__(self, message: str):
super().__init__(message)


@dataclass
class ClientOptions:
host: str
port: int = 2113
username: str | None = None
password: str | None = None
tls: bool = True
dns_discovery: bool = False
keep_alive_timeout: int = 10000
keep_alive_interval: int = 10000

@classmethod
def from_connection_string(cls, connection_string: str) -> ClientOptions:
url = URL(connection_string)
if not url.host:
raise MalformedConnectionString("'host' is undefined")

options = ClientOptions(host=url.host)

if url.scheme == "esdb":
options.dns_discovery = False
elif url.scheme == "esdb+discovery":
options.dns_discovery = True

if url.port:
options.port = url.port

options.username = url.user
options.password = url.password

return options
7 changes: 0 additions & 7 deletions eventstoredb/client/types.py

This file was deleted.

5 changes: 2 additions & 3 deletions examples/append_to_stream.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import asyncio
import logging

from eventstoredb.client.client import Client, ClientOptions
from eventstoredb.client.client import Client
from eventstoredb.events import JsonEvent

logging.basicConfig(level=logging.WARN)


async def main() -> None:
settings = ClientOptions(host="localhost", port=2113)
client = Client(settings)
client = Client("esdb://localhost:2113")
stream_name = "example-stream"
result = await client.append_to_stream(
stream_name=stream_name,
Expand Down
73 changes: 72 additions & 1 deletion pdm.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dynamic = ["version"]
requires-python = ">=3.9"
dependencies = [
"betterproto==2.0.0b5",
"yarl>=1.8.1",
]

[project.urls]
Expand Down
46 changes: 46 additions & 0 deletions tests/client/test_client_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from eventstoredb import ClientOptions


@pytest.mark.parametrize(
"connection_string,client_options",
[
(
"esdb://host",
ClientOptions(
host="host",
port=2113,
tls=True,
dns_discovery=False,
),
),
(
"esdb://host:1234",
ClientOptions(
host="host",
port=1234,
tls=True,
dns_discovery=False,
),
),
(
"esdb+discovery://host",
ClientOptions(
host="host",
tls=True,
dns_discovery=True,
),
),
(
"esdb://user:pass@host",
ClientOptions(
host="host",
username="user",
password="pass",
),
),
],
)
def test_from_url(connection_string: str, client_options: ClientOptions) -> None:
assert ClientOptions.from_connection_string(connection_string) == client_options

0 comments on commit e76e777

Please sign in to comment.