Merge pull request #235 from tiqi-group/feat/adds_client_id_default

feat: adds client id default
This commit is contained in:
Mose Müller 2025-05-22 16:16:02 +02:00 committed by GitHub
commit 982875dee6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 5 deletions

View File

@ -58,7 +58,7 @@ class MyService(pydase.DataService):
proxy = pydase.Client( proxy = pydase.Client(
url="ws://<ip_addr>:<service_port>", url="ws://<ip_addr>:<service_port>",
block_until_connected=False, block_until_connected=False,
client_id="my_pydase_client_id", client_id="my_pydase_client_id", # optional, defaults to system hostname
).proxy ).proxy
# For SSL-encrypted services, use the wss protocol # For SSL-encrypted services, use the wss protocol
@ -77,7 +77,7 @@ if __name__ == "__main__":
In this example: In this example:
- The `MyService` class has a `proxy` attribute that connects to a `pydase` service at `<ip_addr>:<service_port>`. - The `MyService` class has a `proxy` attribute that connects to a `pydase` service at `<ip_addr>:<service_port>`.
- By setting `block_until_connected=False`, the service can start without waiting for the connection to succeed. - By setting `block_until_connected=False`, the service can start without waiting for the connection to succeed.
- By setting `client_id`, the server will log a descriptive identifier for this client via the `X-Client-Id` HTTP header. - The `client_id` is optional. If not specified, it defaults to the system hostname, which will be sent in the `X-Client-Id` HTTP header for logging or authentication on the server side.
## Custom `socketio.AsyncClient` Connection Parameters ## Custom `socketio.AsyncClient` Connection Parameters

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
import logging import logging
import socket
import sys import sys
import threading import threading
import urllib.parse import urllib.parse
@ -59,7 +60,8 @@ class Client:
client's behaviour (e.g., reconnection attempts or reconnection delay). client's behaviour (e.g., reconnection attempts or reconnection delay).
client_id: An optional client identifier. This ID is sent to the server as the client_id: An optional client identifier. This ID is sent to the server as the
`X-Client-Id` HTTP header. It can be used for logging or authentication `X-Client-Id` HTTP header. It can be used for logging or authentication
purposes on the server side. purposes on the server side. If not provided, it defaults to the hostname
of the machine running the client.
proxy_url: An optional proxy URL to route the connection through. This is useful proxy_url: An optional proxy URL to route the connection through. This is useful
if the service is only reachable via an SSH tunnel or behind a firewall if the service is only reachable via an SSH tunnel or behind a firewall
(e.g., `socks5://localhost:2222`). (e.g., `socks5://localhost:2222`).
@ -112,7 +114,7 @@ class Client:
self._path_prefix = parsed_url.path.rstrip("/") # Remove trailing slash if any self._path_prefix = parsed_url.path.rstrip("/") # Remove trailing slash if any
self._url = url self._url = url
self._proxy_url = proxy_url self._proxy_url = proxy_url
self._client_id = client_id self._client_id = client_id or socket.gethostname()
self._sio_client_kwargs = sio_client_kwargs self._sio_client_kwargs = sio_client_kwargs
self._loop: asyncio.AbstractEventLoop | None = None self._loop: asyncio.AbstractEventLoop | None = None
self._thread: threading.Thread | None = None self._thread: threading.Thread | None = None

View File

@ -168,9 +168,11 @@ def test_context_manager(pydase_client: pydase.Client) -> None:
def test_client_id( def test_client_id(
pydase_client: pydase.Client, caplog: pytest.LogCaptureFixture pydase_client: pydase.Client, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
import socket
pydase.Client(url="ws://localhost:9999") pydase.Client(url="ws://localhost:9999")
assert "Client [sid=" in caplog.text assert f"Client [id={socket.gethostname()}]" in caplog.text
caplog.clear() caplog.clear()
pydase.Client(url="ws://localhost:9999", client_id="my_service") pydase.Client(url="ws://localhost:9999", client_id="my_service")