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
13 changes: 10 additions & 3 deletions src/fishaudio/core/client_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,26 @@ def get_headers(
) -> dict[str, str]:
"""Build headers including authentication and user agent.

A ``User-Agent`` in ``additional_headers`` (any casing), or else one set
on a user-supplied httpx client, is appended to the SDK's own.
A name in ``additional_headers`` replaces the SDK's header of that
name whatever its case, so the two are never sent side by side. The
exception is ``User-Agent``: one in ``additional_headers`` (any
casing), or else one set on a user-supplied httpx client, is appended
to the SDK's own.
"""
headers = {"Authorization": f"Bearer {self.api_key}"}
# Read from the client on every call, so a User-Agent changed on a
# supplied httpx client after construction is used. The SDK's own
# client carries USER_AGENT, which user_agent() does not double.
caller = _caller_user_agent(getattr(self, "_client", None))
extra = {}
for name, value in (additional_headers or {}).items():
if name.lower() == "user-agent":
caller = value
else:
headers[name] = value
extra[name] = value
replaced = {name.lower() for name in extra}
headers = {k: v for k, v in headers.items() if k.lower() not in replaced}
headers.update(extra)
headers["User-Agent"] = user_agent(caller)
return headers

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,39 @@ def test_additional_header_is_kept_without_an_explicit_one(self, mock_api_key):
assert kwargs["headers"]["Model"] == "transcribe-1-pro"
assert kwargs["headers"]["Authorization"] == f"Bearer {mock_api_key}"

@pytest.mark.parametrize(
"name", ["authorization", "AUTHORIZATION", "Authorization"]
)
def test_additional_header_replaces_the_sdk_header_of_other_case(
self, mock_api_key, name
):
# Sent through httpx, which would send two spellings as two headers.
seen = []

def handler(request: httpx.Request) -> httpx.Response:
seen.append(request.headers.get_list("authorization"))
return httpx.Response(200, json={})

http = httpx.Client(
base_url="https://api.test", transport=httpx.MockTransport(handler)
)
wrapper = ClientWrapper(api_key=mock_api_key, httpx_client=http)
wrapper.request(
"GET",
"/x",
request_options=RequestOptions(additional_headers={name: "Bearer other"}),
)
wrapper.request("GET", "/x")
assert seen == [["Bearer other"], [f"Bearer {mock_api_key}"]]

def test_additional_user_agent_is_sent_once_after_the_sdk_one(self, mock_api_key):
wrapper = ClientWrapper(api_key=mock_api_key)
headers = wrapper.get_headers({"user-agent": "mine"})
sent = {k: v for k, v in headers.items() if k.lower() == "user-agent"}
assert list(sent) == ["User-Agent"]
assert sent["User-Agent"].startswith("fish-audio-python/")
assert sent["User-Agent"].endswith(" mine")


class TestAsyncClientWrapper:
"""Test async AsyncClientWrapper."""
Expand Down