Skip to content

Commit 0d73bfe

Browse files
Add SDK version and headers to client requests
- Injected `wsn` and `User-Agent` headers into all client requests. - Dynamically set SDK version using `importlib.metadata.version`. - Added test cases to validate the headers in both synchronous and asynchronous clients. Assisted-by: Codex
1 parent 0f19496 commit 0d73bfe

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/pdfrest/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import asyncio
6+
import importlib.metadata
67
import json
78
import os
89
import uuid
@@ -337,7 +338,12 @@ def __init__(
337338
if resolved_api_key is not None:
338339
self._validate_pdfrest_api_key(resolved_api_key, resolved_base_url)
339340

340-
default_headers: dict[str, str] = {"Accept": "application/json"}
341+
version = importlib.metadata.version("pdfrest")
342+
default_headers: dict[str, str] = {
343+
"Accept": "application/json",
344+
"wsn": "pdfrest-python",
345+
"User-Agent": f"pdfrest-python-sdk/{version}",
346+
}
341347
if resolved_api_key is not None:
342348
default_headers[API_KEY_HEADER_NAME] = resolved_api_key
343349
if headers:

tests/test_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
PdfRestConfigurationError,
1515
PdfRestTimeoutError,
1616
UpResponse,
17+
client as client_module,
1718
)
1819

1920
VALID_API_KEY = "12345678-1234-1234-1234-123456789abc"
@@ -61,6 +62,39 @@ def handler(request: httpx.Request) -> httpx.Response:
6162
assert response.product == "pdfRest API Toolkit"
6263

6364

65+
def test_client_sets_sdk_headers(monkeypatch: pytest.MonkeyPatch) -> None:
66+
monkeypatch.setenv("PDFREST_API_KEY", VALID_API_KEY)
67+
monkeypatch.setattr(client_module.importlib.metadata, "version", lambda _: "1.2.3")
68+
69+
def handler(request: httpx.Request) -> httpx.Response:
70+
assert request.headers["wsn"] == "pdfrest-python"
71+
assert request.headers["User-Agent"] == "pdfrest-python-sdk/1.2.3"
72+
assert request.headers["Accept"] == "application/json"
73+
return httpx.Response(200, json=_build_up_response())
74+
75+
transport = httpx.MockTransport(handler)
76+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
77+
client.up()
78+
79+
80+
@pytest.mark.asyncio
81+
async def test_async_client_sets_sdk_headers(
82+
monkeypatch: pytest.MonkeyPatch,
83+
) -> None:
84+
monkeypatch.setenv("PDFREST_API_KEY", ASYNC_API_KEY)
85+
monkeypatch.setattr(client_module.importlib.metadata, "version", lambda _: "4.5.6")
86+
87+
def handler(request: httpx.Request) -> httpx.Response:
88+
assert request.headers["wsn"] == "pdfrest-python"
89+
assert request.headers["User-Agent"] == "pdfrest-python-sdk/4.5.6"
90+
assert request.headers["Accept"] == "application/json"
91+
return httpx.Response(200, json=_build_up_response())
92+
93+
transport = httpx.MockTransport(handler)
94+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
95+
await client.up()
96+
97+
6498
def test_missing_api_key_raises(monkeypatch: pytest.MonkeyPatch) -> None:
6599
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
66100

0 commit comments

Comments
 (0)