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
4 changes: 2 additions & 2 deletions src/deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(self, *args, **kwargs) -> None:
# Set a placeholder api_key if none provided (base client requires it)
if kwargs.get("api_key") is None:
kwargs["api_key"] = "token"
elif kwargs.get("api_key") is None:
elif "api_key" not in kwargs:
# The generated base client takes os.getenv("DEEPGRAM_API_KEY") as a default
# argument, so it is read once at import. Re-read it here so a key set after
# import (load_dotenv below the imports) is still picked up.
Expand Down Expand Up @@ -199,7 +199,7 @@ def __init__(self, *args, **kwargs) -> None:
# Set a placeholder api_key if none provided (base client requires it)
if kwargs.get("api_key") is None:
kwargs["api_key"] = "token"
elif kwargs.get("api_key") is None:
elif "api_key" not in kwargs:
# The generated base client takes os.getenv("DEEPGRAM_API_KEY") as a default
# argument, so it is read once at import. Re-read it here so a key set after
# import (load_dotenv below the imports) is still picked up.
Expand Down
44 changes: 37 additions & 7 deletions tests/custom/test_api_key_env_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
working (issue #734).
"""

import os
import subprocess
import sys
import textwrap
import typing

import pytest
Expand All @@ -25,13 +29,31 @@ def _auth(client: typing.Any) -> typing.Optional[str]:
return client._client_wrapper.api_key


def test_env_key_set_after_import_is_used(
no_env_key: None, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The import already happened above with no key set; this is the load_dotenv case."""
monkeypatch.setenv("DEEPGRAM_API_KEY", "set-after-import")
assert _auth(DeepgramClient()) == "set-after-import"
assert _auth(AsyncDeepgramClient()) == "set-after-import"
def test_env_key_set_after_import_is_used() -> None:
"""Exercise the load_dotenv ordering in a process that starts without a key."""
env = os.environ.copy()
env.pop("DEEPGRAM_API_KEY", None)
code = textwrap.dedent(
"""
import os

from deepgram import AsyncDeepgramClient, DeepgramClient

os.environ["DEEPGRAM_API_KEY"] = "set-after-import"
assert DeepgramClient()._client_wrapper.api_key == "set-after-import"
assert AsyncDeepgramClient()._client_wrapper.api_key == "set-after-import"
"""
)

result = subprocess.run(
[sys.executable, "-c", code],
env=env,
capture_output=True,
text=True,
check=False,
)

assert result.returncode == 0, result.stderr


def test_explicit_api_key_beats_the_environment(monkeypatch: pytest.MonkeyPatch) -> None:
Expand All @@ -40,6 +62,14 @@ def test_explicit_api_key_beats_the_environment(monkeypatch: pytest.MonkeyPatch)
assert _auth(AsyncDeepgramClient(api_key="explicit")) == "explicit"


def test_explicit_none_does_not_fall_back_to_environment(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("DEEPGRAM_API_KEY", "from-env")
with pytest.raises(ApiError):
DeepgramClient(api_key=None)
with pytest.raises(ApiError):
AsyncDeepgramClient(api_key=None)


def test_access_token_still_takes_precedence(monkeypatch: pytest.MonkeyPatch) -> None:
"""access_token callers get the placeholder, not the environment key."""
monkeypatch.setenv("DEEPGRAM_API_KEY", "from-env")
Expand Down
Loading