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
12 changes: 11 additions & 1 deletion clients/python/src/objectstore_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import Mapping, Sequence
from dataclasses import asdict, dataclass
from datetime import UTC, datetime, timedelta
from importlib.metadata import version
from io import BytesIO
from typing import IO, Any, Literal, NamedTuple, cast
from urllib.parse import urlparse
Expand Down Expand Up @@ -38,6 +39,9 @@
# Query parameter carrying a JWT, mirroring the `x-os-auth` header.
PARAM_AUTH = "os_auth"

# Identifies this library to the server, mirroring the Rust client's user agent.
USER_AGENT = f"objectstore-client/{version('objectstore-client')}"


class GetResponse(NamedTuple):
metadata: Metadata
Expand Down Expand Up @@ -122,7 +126,9 @@ class Client:

connection_kwargs: Additional keyword arguments to pass to the underlying
urllib3 connection pool (e.g., custom headers, SSL settings, advanced
timeouts).
timeouts). By default, requests carry a ``User-Agent`` header of
``objectstore-client/{version}``; pass a ``headers`` mapping with a
``User-Agent`` key here to override it.
token: A ``SecretKey`` that signs a fresh JWT for each request
using an EdDSA keypair, or a static pre-signed JWT string used
as-is for every request. Use a ``SecretKey`` for internal
Expand Down Expand Up @@ -158,6 +164,10 @@ def __init__(
if connection_kwargs:
connection_kwargs_to_use = {**connection_kwargs_to_use, **connection_kwargs}

headers = urllib3.HTTPHeaderDict({"User-Agent": USER_AGENT})
headers.update(connection_kwargs_to_use.get("headers") or {})
connection_kwargs_to_use["headers"] = headers

self._pool = urllib3.connectionpool.connection_from_url(
base_url, **connection_kwargs_to_use
)
Expand Down
27 changes: 27 additions & 0 deletions clients/python/tests/test_smoke.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from objectstore_client import Client, Usecase
from objectstore_client.client import USER_AGENT


def test_imports() -> None:
Expand Down Expand Up @@ -56,3 +57,29 @@ def test_object_url_empty_scope() -> None:
session.object_url("foo/bar")
== "http://127.0.0.1:8888/v1/objects/testing/_/foo/bar"
)


def test_default_user_agent() -> None:
client = Client("http://127.0.0.1:8888/")

assert client._pool.headers["User-Agent"] == USER_AGENT


def test_user_agent_can_be_overridden() -> None:
client = Client(
"http://127.0.0.1:8888/",
connection_kwargs={"headers": {"User-Agent": "custom-agent/1.0"}},
)

assert client._pool.headers["User-Agent"] == "custom-agent/1.0"


def test_user_agent_override_is_case_insensitive() -> None:
client = Client(
"http://127.0.0.1:8888/",
connection_kwargs={"headers": {"user-agent": "custom-agent/1.0"}},
)

headers = client._pool.headers
assert headers["User-Agent"] == "custom-agent/1.0"
assert len(headers) == 1
Loading