Skip to content

Commit 7c50614

Browse files
author
Delega Bot
committed
fix: send stable user agent from python sdk
1 parent 22d5bfb commit 7c50614

5 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/delega/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Delega Python SDK - Official client for the Delega API."""
22

3+
from ._version import __version__
34
from .client import Delega
45
from .exceptions import (
56
DelegaAPIError,
@@ -11,8 +12,6 @@
1112
from .models import Agent, Comment, Project, Task
1213
from .webhooks import verify_webhook
1314

14-
__version__ = "0.1.2"
15-
1615
__all__ = [
1716
"Agent",
1817
"AsyncDelega",

src/delega/_http.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import urllib.request
99
from typing import Any, Optional
1010

11+
from ._version import USER_AGENT
1112
from .exceptions import (
1213
DelegaAPIError,
1314
DelegaAuthError,
@@ -64,6 +65,7 @@ def _headers(self) -> dict[str, str]:
6465
"X-Agent-Key": self._api_key,
6566
"Content-Type": "application/json",
6667
"Accept": "application/json",
68+
"User-Agent": USER_AGENT,
6769
}
6870

6971
def request(

src/delega/_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""Package version metadata."""
2+
3+
__version__ = "0.1.2"
4+
USER_AGENT = f"delega-python/{__version__}"

src/delega/async_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
DelegaRateLimitError,
1515
)
1616
from .models import Agent, Comment, Project, Task
17+
from ._version import USER_AGENT
1718

1819
_DEFAULT_BASE_URL = "https://api.delega.dev"
1920

@@ -43,6 +44,7 @@ def __init__(self, base_url: str, api_key: str, timeout: int = 30) -> None:
4344
"X-Agent-Key": api_key,
4445
"Content-Type": "application/json",
4546
"Accept": "application/json",
47+
"User-Agent": USER_AGENT,
4648
},
4749
timeout=timeout,
4850
)

tests/test_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Project,
2121
Task,
2222
)
23+
from delega._version import USER_AGENT
2324

2425

2526
def _mock_response(data: Any, status: int = 200) -> MagicMock:
@@ -388,6 +389,21 @@ def test_auth_header(self, mock_urlopen: MagicMock) -> None:
388389
request = mock_urlopen.call_args[0][0]
389390
self.assertEqual(request.get_header("X-agent-key"), "dlg_mykey123")
390391
self.assertEqual(request.get_header("Content-type"), "application/json")
392+
self.assertEqual(request.get_header("User-agent"), USER_AGENT)
393+
394+
@patch("delega.async_client._require_httpx")
395+
def test_async_client_sets_user_agent(self, mock_require_httpx: MagicMock) -> None:
396+
fake_httpx = MagicMock()
397+
fake_async_client = MagicMock()
398+
fake_httpx.AsyncClient = fake_async_client
399+
mock_require_httpx.return_value = fake_httpx
400+
401+
from delega.async_client import AsyncDelega
402+
403+
AsyncDelega(api_key="dlg_async")
404+
405+
_, kwargs = fake_async_client.call_args
406+
self.assertEqual(kwargs["headers"]["User-Agent"], USER_AGENT)
391407

392408

393409
class TestModels(unittest.TestCase):

0 commit comments

Comments
 (0)