Skip to content
Merged
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
5 changes: 3 additions & 2 deletions src/capawesome_cloud/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ def __init__(
base_url=base_url.rstrip("/"),
timeout=timeout,
)
# The token must always apply; identify ourselves on clients we create
# without clobbering a caller-supplied client's User-Agent.
# The token and the client header must always apply. The User-Agent is
# set only on clients we create, so a caller-supplied one keeps its own.
self._client.headers["Authorization"] = f"Bearer {token}"
self._client.headers["X-Capawesome-Client"] = f"cloud-python/{__version__}"
self._client.headers.setdefault("Accept", "application/json")
if self._owns_client:
self._client.headers["User-Agent"] = f"capawesome-cloud-python/{__version__}"
Expand Down
7 changes: 4 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import httpx
import pytest

from capawesome_cloud import CapawesomeCloud, CapawesomeCloudError
from capawesome_cloud import CapawesomeCloud, CapawesomeCloudError, __version__


def test_token_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down Expand Up @@ -41,11 +41,12 @@ def test_missing_token_raises(monkeypatch: pytest.MonkeyPatch) -> None:
CapawesomeCloud(token=" ")


def test_sets_auth_and_user_agent_headers() -> None:
def test_sets_auth_and_client_headers() -> None:
client = CapawesomeCloud(token="abc")
headers = client._http._client.headers
assert headers["Authorization"] == "Bearer abc"
assert headers["User-Agent"].startswith("capawesome-cloud-python/")
assert headers["User-Agent"] == f"capawesome-cloud-python/{__version__}"
assert headers["X-Capawesome-Client"] == f"cloud-python/{__version__}"


def test_context_manager_closes() -> None:
Expand Down
Loading