Skip to content

Commit

Permalink
feat: grpc_address
Browse files Browse the repository at this point in the history
  • Loading branch information
felicijus committed Oct 1, 2024
1 parent 52d1783 commit 4b8de61
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 54 deletions.
9 changes: 4 additions & 5 deletions pyzeebe/channel/insecure_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@


def create_insecure_channel(
hostname: Optional[str] = None, port: Optional[int] = None, channel_options: Optional[ChannelArgumentType] = None
grpc_address: Optional[str] = None, channel_options: Optional[ChannelArgumentType] = None
) -> grpc.aio.Channel:
"""
Create an insecure channel
Args:
hostname (Optional[str], optional): Zeebe gateway hostname
port (Optional[int], optional): Zeebe gateway port
grpc_address (Optional[str], optional): Zeebe gateway hostname
channel_options (Optional[Dict], optional): GRPC channel options.
See https://grpc.github.io/grpc/python/glossary.html#term-channel_arguments
Returns:
grpc.aio.Channel: A GRPC Channel connected to the Zeebe gateway.
"""
address = create_address(hostname, port)
return grpc.aio.insecure_channel(address, options=get_channel_options(channel_options))
grpc_address = create_address(grpc_address=grpc_address)
return grpc.aio.insecure_channel(target=grpc_address, options=get_channel_options(channel_options))
9 changes: 5 additions & 4 deletions pyzeebe/channel/secure_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@


def create_secure_channel(
hostname: Optional[str] = None,
port: Optional[int] = None,
grpc_address: Optional[str] = None,
channel_options: Optional[ChannelArgumentType] = None,
channel_credentials: Optional[grpc.ChannelCredentials] = None,
) -> grpc.aio.Channel:
Expand All @@ -27,6 +26,8 @@ def create_secure_channel(
Returns:
grpc.aio.Channel: A GRPC Channel connected to the Zeebe gateway.
"""
address = create_address(hostname, port)
grpc_address = create_address(grpc_address=grpc_address)
credentials = channel_credentials or grpc.ssl_channel_credentials()
return grpc.aio.secure_channel(address, credentials, options=get_channel_options(channel_options))
return grpc.aio.secure_channel(
target=grpc_address, credentials=credentials, options=get_channel_options(channel_options)
)
7 changes: 3 additions & 4 deletions pyzeebe/channel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@


def create_address(
hostname: Optional[str] = None,
port: Optional[int] = None,
grpc_address: Optional[str] = None,
) -> str:
if hostname or port:
return f"{hostname or DEFAULT_HOSTNAME}:{port or DEFAULT_PORT}"
if grpc_address:
return grpc_address
return os.getenv("ZEEBE_ADDRESS", DEFAULT_ADDRESS)
18 changes: 1 addition & 17 deletions tests/unit/channel/insecure_channel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,4 @@ def test_uses_default_address(self, insecure_channel_mock: Mock):
create_insecure_channel()

insecure_channel_call = insecure_channel_mock.mock_calls[0]
assert insecure_channel_call.args[0] == create_address()

def test_overrides_default_port_if_provided(self, insecure_channel_mock: Mock):
port = 123

create_insecure_channel(port=port)

insecure_channel_call = insecure_channel_mock.mock_calls[0]
assert insecure_channel_call.args[0] == f"{DEFAULT_HOSTNAME}:{port}"

def test_overrides_default_hostname_if_provided(self, insecure_channel_mock: Mock):
hostname = str(uuid4())

create_insecure_channel(hostname=hostname)

insecure_channel_call = insecure_channel_mock.mock_calls[0]
assert insecure_channel_call.args[0] == f"{hostname}:{DEFAULT_PORT}"
assert insecure_channel_call.kwargs["target"] == create_address()
18 changes: 1 addition & 17 deletions tests/unit/channel/secure_channel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,4 @@ def test_uses_default_address(self, secure_channel_mock: Mock):
create_secure_channel()

secure_channel_call = secure_channel_mock.mock_calls[0]
assert secure_channel_call.args[0] == create_address()

def test_overrides_default_port_if_provided(self, secure_channel_mock: Mock):
port = 123

create_secure_channel(port=port)

secure_channel_call = secure_channel_mock.mock_calls[0]
assert secure_channel_call.args[0] == f"{DEFAULT_HOSTNAME}:{port}"

def test_overrides_default_hostname_if_provided(self, secure_channel_mock: Mock):
hostname = str(uuid4())

create_secure_channel(hostname=hostname)

secure_channel_call = secure_channel_mock.mock_calls[0]
assert secure_channel_call.args[0] == f"{hostname}:{DEFAULT_PORT}"
assert secure_channel_call.kwargs["target"] == create_address()
10 changes: 3 additions & 7 deletions tests/unit/channel/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ def test_returns_default_address(self):

assert address == DEFAULT_ADDRESS

def test_default_port_is_26500(self):
address = create_address(hostname=str(uuid4()))

assert address.split(":")[1] == "26500"

def test_default_hostname_is_localhost(self):
address = create_address(port=12)
def test_default_hostname_port(self):
address = create_address()

assert address.split(":")[0] == "localhost"
assert address.split(":")[1] == "26500"

def test_returns_env_var_if_provided(self):
zeebe_address = str(uuid4())
Expand Down

0 comments on commit 4b8de61

Please sign in to comment.