diff --git a/src/delega/__init__.py b/src/delega/__init__.py index a46b3c0..dc0f569 100644 --- a/src/delega/__init__.py +++ b/src/delega/__init__.py @@ -1,5 +1,6 @@ """Delega Python SDK - Official client for the Delega API.""" +from ._version import __version__ from .client import Delega from .exceptions import ( DelegaAPIError, @@ -11,8 +12,6 @@ from .models import Agent, Comment, Project, Task from .webhooks import verify_webhook -__version__ = "0.1.2" - __all__ = [ "Agent", "AsyncDelega", diff --git a/src/delega/_http.py b/src/delega/_http.py index ea52698..71ddfbd 100644 --- a/src/delega/_http.py +++ b/src/delega/_http.py @@ -8,6 +8,7 @@ import urllib.request from typing import Any, Optional +from ._version import USER_AGENT from .exceptions import ( DelegaAPIError, DelegaAuthError, @@ -64,6 +65,7 @@ def _headers(self) -> dict[str, str]: "X-Agent-Key": self._api_key, "Content-Type": "application/json", "Accept": "application/json", + "User-Agent": USER_AGENT, } def request( diff --git a/src/delega/_version.py b/src/delega/_version.py new file mode 100644 index 0000000..844697c --- /dev/null +++ b/src/delega/_version.py @@ -0,0 +1,4 @@ +"""Package version metadata.""" + +__version__ = "0.1.2" +USER_AGENT = f"delega-python/{__version__}" diff --git a/src/delega/async_client.py b/src/delega/async_client.py index 28bd203..1c25b5a 100644 --- a/src/delega/async_client.py +++ b/src/delega/async_client.py @@ -14,6 +14,7 @@ DelegaRateLimitError, ) from .models import Agent, Comment, Project, Task +from ._version import USER_AGENT _DEFAULT_BASE_URL = "https://api.delega.dev" @@ -43,6 +44,7 @@ def __init__(self, base_url: str, api_key: str, timeout: int = 30) -> None: "X-Agent-Key": api_key, "Content-Type": "application/json", "Accept": "application/json", + "User-Agent": USER_AGENT, }, timeout=timeout, ) diff --git a/tests/test_client.py b/tests/test_client.py index 3aa8531..c03696e 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -20,6 +20,7 @@ Project, Task, ) +from delega._version import USER_AGENT def _mock_response(data: Any, status: int = 200) -> MagicMock: @@ -388,6 +389,21 @@ def test_auth_header(self, mock_urlopen: MagicMock) -> None: request = mock_urlopen.call_args[0][0] self.assertEqual(request.get_header("X-agent-key"), "dlg_mykey123") self.assertEqual(request.get_header("Content-type"), "application/json") + self.assertEqual(request.get_header("User-agent"), USER_AGENT) + + @patch("delega.async_client._require_httpx") + def test_async_client_sets_user_agent(self, mock_require_httpx: MagicMock) -> None: + fake_httpx = MagicMock() + fake_async_client = MagicMock() + fake_httpx.AsyncClient = fake_async_client + mock_require_httpx.return_value = fake_httpx + + from delega.async_client import AsyncDelega + + AsyncDelega(api_key="dlg_async") + + _, kwargs = fake_async_client.call_args + self.assertEqual(kwargs["headers"]["User-Agent"], USER_AGENT) class TestModels(unittest.TestCase):