Skip to content

Commit c01ab68

Browse files
committed
fix(python): preserve dynamic admin endpoint
1 parent 5d0a61c commit c01ab68

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

streamline_sdk/_admin_http.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import asyncio
1414
import json
15-
from collections.abc import Collection
15+
from collections.abc import Callable, Collection
1616
from typing import Any
1717

1818
from .exceptions import TopicError
@@ -34,17 +34,20 @@ class _AdminHttpTransport:
3434
``urllib.request`` call executed on a worker thread otherwise.
3535
"""
3636

37-
def __init__(self, base_url: str) -> None:
37+
def __init__(self, base_url: str | Callable[[], str]) -> None:
3838
"""Initialize the transport.
3939
4040
Args:
41-
base_url: Base URL of the Streamline HTTP REST API.
41+
base_url: Base URL or a callable that resolves the current URL.
4242
"""
4343
self._base_url = base_url
4444

45+
def _current_base_url(self) -> str:
46+
return self._base_url() if callable(self._base_url) else self._base_url
47+
4548
async def get(self, path: str) -> Any:
4649
"""Make an HTTP GET request to the Streamline REST API."""
47-
url = f"{self._base_url}{path}"
50+
url = f"{self._current_base_url()}{path}"
4851

4952
if HAS_AIOHTTP:
5053
async with aiohttp.ClientSession() as session:
@@ -69,7 +72,7 @@ def _sync_get():
6972

7073
async def post(self, path: str, body: Any) -> Any:
7174
"""Make an HTTP POST request to the Streamline REST API."""
72-
url = f"{self._base_url}{path}"
75+
url = f"{self._current_base_url()}{path}"
7376

7477
if HAS_AIOHTTP:
7578
async with aiohttp.ClientSession() as session:
@@ -97,7 +100,7 @@ def _sync_post():
97100

98101
async def delete(self, path: str) -> None:
99102
"""Make an HTTP DELETE request to the Streamline REST API."""
100-
url = f"{self._base_url}{path}"
103+
url = f"{self._current_base_url()}{path}"
101104

102105
if HAS_AIOHTTP:
103106
async with aiohttp.ClientSession() as session:

streamline_sdk/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def __init__(self, client_config: Any):
236236
self._client_config = client_config
237237
self._admin: AIOKafkaAdminClient | None = None
238238
self._started = False
239-
self._http = _AdminHttpTransport(client_config.http_url)
239+
self._http = _AdminHttpTransport(lambda: self._client_config.http_url)
240240

241241
async def start(self) -> None:
242242
"""Start the admin client."""

tests/test_admin_transport.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,12 @@ async def _recording_to_thread(func: Any, *args: Any, **kwargs: Any) -> Any:
575575
await transport.delete("/v1/branches/exp-a")
576576

577577
assert len(to_thread_calls) == 1
578+
579+
580+
def test_transport_resolves_dynamic_base_url():
581+
current = {"url": "http://first:9094"}
582+
transport = _AdminHttpTransport(lambda: current["url"])
583+
584+
assert transport._current_base_url() == "http://first:9094"
585+
current["url"] = "http://second:9094"
586+
assert transport._current_base_url() == "http://second:9094"

0 commit comments

Comments
 (0)