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
11 changes: 10 additions & 1 deletion src/linkup/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class LinkupClient:
x402_signer: An optional x402 signer for payment-gated endpoints. If provided, the
client will attempt to handle 402 responses automatically. Cannot be used together
with api_key.
auth_header: Custom header name to use for the API key (e.g.
``"Ocp-Apim-Subscription-Key"``). When set, the API key value is sent as
``<auth_header>: <api_key>`` instead of the default
``Authorization: Bearer <api_key>``.

Raises:
ValueError: If the API key is not provided and not found in the environment variable.
Expand All @@ -72,6 +76,7 @@ def __init__(
api_key: str | SecretStr | None = None,
base_url: str = "https://api.linkup.so/v1",
x402_signer: LinkupX402Signer | None = None,
auth_header: str | None = None,
) -> None:
if api_key is not None and x402_signer is not None:
raise ValueError("Cannot provide both api_key and x402_signer")
Expand All @@ -90,6 +95,7 @@ def __init__(
self._api_key = api_key

self._base_url: str = base_url
self._auth_header: str | None = auth_header

def search(
self,
Expand Down Expand Up @@ -889,7 +895,10 @@ def _user_agent(self) -> str: # pragma: no cover
def _headers(self) -> dict[str, str]: # pragma: no cover
headers: dict[str, str] = {"User-Agent": self._user_agent()}
if self._api_key is not None:
headers["Authorization"] = f"Bearer {self._api_key.get_secret_value()}"
if self._auth_header is not None:
headers[self._auth_header] = self._api_key.get_secret_value()
else:
headers["Authorization"] = f"Bearer {self._api_key.get_secret_value()}"
return headers

def _request(
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,24 @@ def test_client_both_api_key_and_x402_signer_raises(
linkup.Client(api_key="test-key", x402_signer=mock_x402_signer)


def test_client_custom_auth_header(
mocker: MockerFixture,
) -> None:
client = linkup.Client(api_key="my-key", auth_header="Ocp-Apim-Subscription-Key")

client_mock = mocker.patch("httpx.Client")
client_mock.return_value.__enter__.return_value = client_mock.return_value
client_mock.return_value.request.return_value = Response(
status_code=200, content=b'{"results": []}'
)
client.search(query="query", depth="standard", output_type="searchResults")

init_kwargs = client_mock.call_args[1]
assert "Ocp-Apim-Subscription-Key" in init_kwargs["headers"]
assert init_kwargs["headers"]["Ocp-Apim-Subscription-Key"] == "my-key"
assert "Authorization" not in init_kwargs["headers"]


def test_client_x402_no_auth_header(
mocker: MockerFixture,
x402_client: linkup.Client,
Expand Down
Loading