Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def __init__(self, *args, **kwargs) -> None:
# Use provided session_id or generate one
final_session_id = session_id if session_id is not None else str(uuid.uuid4())

# Ensure headers object exists for pass-through custom headers
headers: Optional[Dict[str, str]] = kwargs.get("headers")
if headers is None:
headers = {}
kwargs["headers"] = headers
# Copy pass-through custom headers so the session header below does not
# mutate the caller's dict (a dict shared by several clients would
# otherwise make every client send the last client's session id).
headers: Dict[str, str] = dict(kwargs.get("headers") or {})
kwargs["headers"] = headers

# Ensure every request has a session identifier header
headers["x-deepgram-session-id"] = final_session_id
Expand Down Expand Up @@ -183,11 +183,11 @@ def __init__(self, *args, **kwargs) -> None:
# Use provided session_id or generate one
final_session_id = session_id if session_id is not None else str(uuid.uuid4())

# Ensure headers object exists for pass-through custom headers
headers: Optional[Dict[str, str]] = kwargs.get("headers")
if headers is None:
headers = {}
kwargs["headers"] = headers
# Copy pass-through custom headers so the session header below does not
# mutate the caller's dict (a dict shared by several clients would
# otherwise make every client send the last client's session id).
headers: Dict[str, str] = dict(kwargs.get("headers") or {})
kwargs["headers"] = headers

# Ensure every request has a session identifier header
headers["x-deepgram-session-id"] = final_session_id
Expand Down
13 changes: 13 additions & 0 deletions tests/custom/test_client_construction_and_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def test_client_generates_session_id_and_opts_out_of_redaction(client_cls: typin
assert client.session_id # auto-generated UUID


@pytest.mark.parametrize("client_cls", [DeepgramClient, AsyncDeepgramClient])
def test_clients_sharing_a_headers_dict_keep_their_own_session_id(client_cls: typing.Any) -> None:
shared_headers = {"X-Custom": "value"}
first = client_cls(environment=_environment(), api_key="k", headers=shared_headers)
second = client_cls(environment=_environment(), api_key="k", headers=shared_headers)

for client in (first, second):
sent = client._client_wrapper.get_headers()
assert sent["x-deepgram-session-id"] == client.session_id
assert sent["X-Custom"] == "value"
assert shared_headers == {"X-Custom": "value"}


@pytest.mark.parametrize("client_cls", [DeepgramClient, AsyncDeepgramClient])
def test_client_with_transport_factory_disables_reconnect(client_cls: typing.Any) -> None:
from deepgram.transport import restore_transport
Expand Down