diff --git a/CHANGELOG.md b/CHANGELOG.md index 7279ef6..3d87aae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## Unreleased + +- The `User-Agent` is now a chain of `vendor/package/version` tokens, starting with + `vatly/vatly-api-python/ Python/ httpx/` instead of + `vatly-api-python/`, matching the other Vatly SDKs. +- New `add_version_string()` on both clients: packages and integrations built on the SDK + append their own token, such as `vatly/vatly-fluent-python/`, so a request + names every package involved. A `User-Agent` in `default_headers` still replaces it. + ## 0.1.0a4 — 2026-11-25 - Sync `openapi.yaml` with the production specification. diff --git a/README.md b/README.md index 67cfe4d..6dc8c4c 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,20 @@ Unkeyed mutations do not retry. Redirects are not followed. `client_info={"app": "MyApp/1.0", "framework": "Django/5"}` to add sanitized identifiers, or `client_info=False` to suppress the SDK-generated header. +The `User-Agent` names every package involved, one `vendor/package/version` token each, +starting with `vatly/vatly-api-python/ Python/ httpx/`. +A package or integration built on the SDK appends its own token once, on the client it +uses, so Vatly sees the whole chain: + +```python +client.add_version_string("vatly/vatly-fluent-python/0.1.0a4") +# User-Agent: vatly/vatly-api-python/0.1.0a4 Python/3.12.8 httpx/0.28.1 vatly/vatly-fluent-python/0.1.0a4 +``` + +Use your PyPI package name and its version constant. Whitespace in a token becomes `-`, +and adding a token already present does nothing. A `User-Agent` in `default_headers` +replaces the whole chain. + ## Errors All API/transport errors extend `VatlyError`, with `status`, `body`, `url`, `method`, diff --git a/src/vatly/_transport.py b/src/vatly/_transport.py index 29be670..8fda07f 100644 --- a/src/vatly/_transport.py +++ b/src/vatly/_transport.py @@ -86,13 +86,19 @@ def __init__( self.timeout = timeout self.max_retries = max_retries self._key_generator = idempotency_key_generator + self._version_strings = [ + f"vatly/vatly-api-python/{__version__}", + f"Python/{platform.python_version()}", + f"httpx/{httpx.__version__}", + ] self._headers = httpx.Headers( { "Authorization": f"Bearer {key}", "Accept": "application/json", - "User-Agent": f"vatly-api-python/{__version__}", + "User-Agent": " ".join(self._version_strings), } ) + self._custom_user_agent = "User-Agent" in httpx.Headers(default_headers or {}) self._headers.update(default_headers or {}) if client_info is not False: info = f"sdk=vatly-api-python/{__version__}; runtime=python/{platform.python_version()}; os={platform.system()}; arch={platform.machine()}" @@ -108,6 +114,21 @@ def __init__( def _init_http(self) -> None: raise NotImplementedError + def add_version_string(self, version_string: str) -> None: + """Append a `vendor/package/version` token to the `User-Agent`. + + Packages built on the SDK call this once with their own token, such as + ``client.add_version_string("vatly/vatly-fluent-python/0.1.0a4")``. Whitespace + becomes ``-``; a token already present is not added again. A `User-Agent` set + in ``default_headers`` replaces the whole chain and is left unchanged. + """ + token = re.sub(r"\s", "-", version_string.strip()) + if not token or token in self._version_strings: + return + self._version_strings.append(token) + if not self._custom_user_agent: + self._headers["User-Agent"] = " ".join(self._version_strings) + @staticmethod def serialize(value: BaseModel | dict[str, Any] | None) -> dict[str, Any] | None: if isinstance(value, BaseModel): diff --git a/tests/test_sdk.py b/tests/test_sdk.py index bc134e3..d61324a 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -1,6 +1,7 @@ import asyncio import inspect import json +import platform import re from datetime import datetime, timedelta, timezone from email.utils import format_datetime @@ -458,6 +459,34 @@ def handler(r): assert "Injected/1" in seen[0].headers["Vatly-Client-Info"] +def test_user_agent_chains_package_tokens(): + seen = [] + + def handler(r): + seen.append(r) + return httpx.Response(200, json=CUSTOMER) + + transport = httpx.MockTransport(handler) + for headers in (None, {"user-agent": "Custom/1"}): + with VatlyClient(KEY, transport=transport, default_headers=headers) as c: + c.customers.get("customer_a") + c.add_version_string("vatly/vatly-fluent-python/1.2.3") + c.add_version_string(" vatly/vatly-django/4 5 ") + c.add_version_string("vatly/vatly-fluent-python/1.2.3") + c.customers.get("customer_a") + c.customers.get("customer_a", options=RequestOptions(headers={"User-Agent": "Own/1"})) + sdk = f"vatly/vatly-api-python/{vatly.__version__}" + runtime = f"Python/{platform.python_version()} httpx/{httpx.__version__}" + assert [r.headers["User-Agent"] for r in seen] == [ + f"{sdk} {runtime}", + f"{sdk} {runtime} vatly/vatly-fluent-python/1.2.3 vatly/vatly-django/4-5", + "Own/1", + "Custom/1", + "Custom/1", + "Own/1", + ] + + @pytest.mark.parametrize("asynchronous", [False, True]) def test_cursor_pagination_uses_same_endpoint_and_rejects_loops(asynchronous): seen = []