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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

- The `User-Agent` is now a chain of `vendor/package/version` tokens, starting with
`vatly/vatly-api-python/<version> Python/<version> httpx/<version>` instead of
`vatly-api-python/<version>`, 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/<version>`, 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.
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<version> Python/<version> httpx/<version>`.
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`,
Expand Down
23 changes: 22 additions & 1 deletion src/vatly/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"
Expand All @@ -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):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_sdk.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = []
Expand Down
Loading