client: adds X-Client-Id header to pydase.Client

This commit is contained in:
Mose Müller 2025-02-20 17:17:19 +01:00
parent 9424d4c412
commit c5e1a08c54
2 changed files with 18 additions and 1 deletions

View File

@ -84,6 +84,7 @@ class Client:
url: str, url: str,
block_until_connected: bool = True, block_until_connected: bool = True,
sio_client_kwargs: dict[str, Any] = {}, sio_client_kwargs: dict[str, Any] = {},
client_id: str = "pydase_client",
): ):
# Parse the URL to separate base URL and path prefix # Parse the URL to separate base URL and path prefix
parsed_url = urllib.parse.urlparse(url) parsed_url = urllib.parse.urlparse(url)
@ -98,6 +99,7 @@ class Client:
self._url = url self._url = url
self._sio = socketio.AsyncClient(**sio_client_kwargs) self._sio = socketio.AsyncClient(**sio_client_kwargs)
self._loop = asyncio.new_event_loop() self._loop = asyncio.new_event_loop()
self._client_id = client_id
self.proxy = ProxyClass( self.proxy = ProxyClass(
sio_client=self._sio, loop=self._loop, reconnect=self.connect sio_client=self._sio, loop=self._loop, reconnect=self.connect
) )
@ -137,7 +139,10 @@ class Client:
logger.debug("Connecting to server '%s' ...", self._url) logger.debug("Connecting to server '%s' ...", self._url)
await self._setup_events() await self._setup_events()
await self._sio.connect( await self._sio.connect(
self._base_url, url=self._base_url,
headers={
"X-Client-Id": self._client_id,
},
socketio_path=f"{self._path_prefix}/ws/socket.io", socketio_path=f"{self._path_prefix}/ws/socket.io",
transports=["websocket"], transports=["websocket"],
retry=True, retry=True,

View File

@ -161,3 +161,15 @@ def test_context_manager(pydase_client: pydase.Client) -> None:
assert client.proxy.my_property == 1337.01 assert client.proxy.my_property == 1337.01
assert not client.proxy.connected assert not client.proxy.connected
def test_client_id(
pydase_client: pydase.Client, caplog: pytest.LogCaptureFixture
) -> None:
pydase.Client(url="ws://localhost:9999")
assert "Client [id=pydase_client] connected" in caplog.text
caplog.clear()
pydase.Client(url="ws://localhost:9999", client_id="my_service")
assert "Client [id=my_service] connected" in caplog.text