Skip to content

Commit be3d471

Browse files
Correct authorization to use Api-Key
- Updated client and tests to use `Api-Key` as the header name for API key injection. - Refactored default headers construction to reflect this change. - Modified related test assertions to validate the new header semantics. Assisted-by: Codex
1 parent 3220c95 commit be3d471

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

src/pdfrest/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
DEFAULT_BASE_URL = "https://api.pdfrest.com"
2525
API_KEY_ENV_VAR = "PDFREST_API_KEY"
26+
API_KEY_HEADER_NAME = "Api-Key"
2627
DEFAULT_TIMEOUT_SECONDS = 10.0
2728

2829
HttpMethod = Literal["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]
@@ -152,7 +153,7 @@ def __init__(
152153

153154
default_headers: dict[str, str] = {"Accept": "application/json"}
154155
if resolved_api_key is not None:
155-
default_headers["Authorization"] = f"Bearer {resolved_api_key}"
156+
default_headers[API_KEY_HEADER_NAME] = resolved_api_key
156157
if headers:
157158
for key, value in headers.items():
158159
default_headers[str(key)] = str(value)

tests/test_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_client_uses_provided_api_key(monkeypatch: pytest.MonkeyPatch) -> None:
4141
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
4242

4343
def handler(request: httpx.Request) -> httpx.Response:
44-
assert request.headers["Authorization"] == f"Bearer {VALID_API_KEY}"
44+
assert request.headers["Api-Key"] == VALID_API_KEY
4545
assert request.url.path == "/up"
4646
return httpx.Response(200, json=_build_up_response())
4747

@@ -60,7 +60,7 @@ def test_client_reads_api_key_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
6060
monkeypatch.setenv("PDFREST_API_KEY", VALID_API_KEY)
6161

6262
def handler(request: httpx.Request) -> httpx.Response:
63-
assert request.headers["Authorization"] == f"Bearer {VALID_API_KEY}"
63+
assert request.headers["Api-Key"] == VALID_API_KEY
6464
assert request.url.host == "example.com"
6565
return httpx.Response(200, json=_build_up_response())
6666

@@ -102,7 +102,7 @@ def test_client_allows_missing_api_key_for_custom_host(
102102
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
103103

104104
def handler(request: httpx.Request) -> httpx.Response:
105-
assert "Authorization" not in request.headers
105+
assert "Api-Key" not in request.headers
106106
assert request.url.host == "internal.example"
107107
return httpx.Response(200, json=_build_up_response())
108108

@@ -120,7 +120,7 @@ def test_up_with_custom_headers(monkeypatch: pytest.MonkeyPatch) -> None:
120120
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
121121

122122
def handler(request: httpx.Request) -> httpx.Response:
123-
assert request.headers["Authorization"] == f"Bearer {ANOTHER_VALID_API_KEY}"
123+
assert request.headers["Api-Key"] == ANOTHER_VALID_API_KEY
124124
assert request.headers["X-Test-Header"] == "value"
125125
return httpx.Response(200, json=_build_up_response())
126126

@@ -259,7 +259,7 @@ async def test_async_client_up(monkeypatch: pytest.MonkeyPatch) -> None:
259259
monkeypatch.setenv("PDFREST_API_KEY", ASYNC_API_KEY)
260260

261261
def handler(request: httpx.Request) -> httpx.Response:
262-
assert request.headers["Authorization"] == f"Bearer {ASYNC_API_KEY}"
262+
assert request.headers["Api-Key"] == ASYNC_API_KEY
263263
return httpx.Response(200, json=_build_up_response())
264264

265265
transport = httpx.MockTransport(handler)

0 commit comments

Comments
 (0)