Skip to content

Commit 65c8b10

Browse files
authored
chore(toolbox-core): deprecate add_headers feature (#278)
* chore!: remove addHeaders feature * lint * remove files * deprecate instead of remove * Update client.py * Update sync_client.py * lint * update requirements * add tests for deprecation warnings
1 parent 701578b commit 65c8b10

File tree

7 files changed

+54
-28
lines changed

7 files changed

+54
-28
lines changed

packages/toolbox-core/README.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,24 +300,15 @@ that fresh credentials or header values can be used.
300300

301301
### Configuration
302302

303-
You can configure these dynamic headers in two ways:
303+
You can configure these dynamic headers as seen below:
304304

305-
1. **During Client Initialization**
306-
307-
```python
308-
from toolbox_core import ToolboxClient
309-
310-
async with ToolboxClient("toolbox-url", client_headers={"header1": header1_getter, "header2": header2_getter, ...}) as client:
311-
```
312-
313-
1. **After Client Initialization**
314-
315-
```python
316-
from toolbox_core import ToolboxClient
305+
```python
306+
from toolbox_core import ToolboxClient
317307

318-
async with ToolboxClient("toolbox-url") as client:
319-
client.add_headers({"header1": header1_getter, "header2": header2_getter, ...})
320-
```
308+
async with ToolboxClient("toolbox-url", client_headers={"header1": header1_getter, "header2": header2_getter, ...}) as client:
309+
# Use client
310+
pass
311+
```
321312

322313
### Authenticating with Google Cloud Servers
323314

packages/toolbox-core/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ authors = [
1212
dependencies = [
1313
"pydantic>=2.7.0,<3.0.0",
1414
"aiohttp>=3.8.6,<4.0.0",
15+
"deprecated>=1.2.15,<2.0.0",
1516
]
1617

1718
classifiers = [
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
aiohttp==3.12.12
22
pydantic==2.11.5
3+
deprecated==1.2.15

packages/toolbox-core/src/toolbox_core/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import Any, Awaitable, Callable, Mapping, Optional, Union
1818

1919
from aiohttp import ClientSession
20+
from deprecated import deprecated
2021

2122
from .protocol import ManifestSchema, ToolSchema
2223
from .tool import ToolboxTool
@@ -340,16 +341,17 @@ async def load_toolset(
340341

341342
return tools
342343

344+
@deprecated(
345+
"Use the `client_headers` parameter in the ToolboxClient constructor instead."
346+
)
343347
def add_headers(
344348
self,
345349
headers: Mapping[str, Union[Callable[[], str], Callable[[], Awaitable[str]]]],
346350
) -> None:
347351
"""
348352
Add headers to be included in each request sent through this client.
349-
350353
Args:
351354
headers: Headers to include in each request sent through this client.
352-
353355
Raises:
354356
ValueError: If any of the headers are already registered in the client.
355357
"""

packages/toolbox-core/src/toolbox_core/sync_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from threading import Thread
1818
from typing import Any, Awaitable, Callable, Mapping, Optional, Union
1919

20+
from deprecated import deprecated
21+
2022
from .client import ToolboxClient
2123
from .sync_tool import ToolboxSyncTool
2224

@@ -153,6 +155,9 @@ def load_toolset(
153155
for async_tool in async_tools
154156
]
155157

158+
@deprecated(
159+
"Use the `client_headers` parameter in the ToolboxClient constructor instead."
160+
)
156161
def add_headers(
157162
self,
158163
headers: Mapping[

packages/toolbox-core/tests/test_client.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,23 +1443,41 @@ async def test_add_headers_success(
14431443
)
14441444

14451445
async with ToolboxClient(TEST_BASE_URL) as client:
1446-
client.add_headers(static_header)
1446+
with pytest.warns(
1447+
DeprecationWarning,
1448+
match="Use the `client_headers` parameter in the ToolboxClient constructor instead.",
1449+
):
1450+
client.add_headers(static_header)
14471451
assert client._ToolboxClient__client_headers == static_header
14481452

14491453
tool = await client.load_tool(tool_name)
14501454
result = await tool(param1="test")
14511455
assert result == expected_payload["result"]
14521456

1457+
@pytest.mark.asyncio
1458+
async def test_add_headers_deprecation_warning(self):
1459+
"""Tests that add_headers issues a DeprecationWarning."""
1460+
async with ToolboxClient(TEST_BASE_URL) as client:
1461+
with pytest.warns(
1462+
DeprecationWarning,
1463+
match="Use the `client_headers` parameter in the ToolboxClient constructor instead.",
1464+
):
1465+
client.add_headers({"X-Deprecated-Test": "value"})
1466+
14531467
@pytest.mark.asyncio
14541468
async def test_add_headers_duplicate_fail(self, static_header):
14551469
"""Tests that adding a duplicate header via add_headers raises
14561470
ValueError."""
14571471
async with ToolboxClient(TEST_BASE_URL, client_headers=static_header) as client:
1458-
with pytest.raises(
1459-
ValueError,
1460-
match=f"Client header\\(s\\) `X-Static-Header` already registered",
1472+
with pytest.warns(
1473+
DeprecationWarning,
1474+
match="Use the `client_headers` parameter in the ToolboxClient constructor instead.",
14611475
):
1462-
await client.add_headers(static_header)
1476+
with pytest.raises(
1477+
ValueError,
1478+
match=f"Client header\\(s\\) `X-Static-Header` already registered",
1479+
):
1480+
client.add_headers(static_header)
14631481

14641482
@pytest.mark.asyncio
14651483
async def test_client_header_auth_token_conflict_fail(

packages/toolbox-core/tests/test_sync_client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,11 @@ def post_callback(url, **kwargs):
383383
f"{TEST_BASE_URL}/api/tool/{tool_name}/invoke", callback=post_callback
384384
)
385385

386-
sync_client.add_headers(headers_to_add)
386+
with pytest.warns(
387+
DeprecationWarning,
388+
match="Use the `client_headers` parameter in the ToolboxClient constructor instead.",
389+
):
390+
sync_client.add_headers(headers_to_add)
387391
tool = sync_client.load_tool(tool_name)
388392
result = tool(param1="test")
389393
assert result == expected_payload["result"]
@@ -409,11 +413,15 @@ def mock_add_headers(headers):
409413
with ToolboxSyncClient(
410414
TEST_BASE_URL, client_headers=initial_headers
411415
) as client:
412-
with pytest.raises(
413-
ValueError,
414-
match="Client header\\(s\\) `X-Initial-Header` already registered",
416+
with pytest.warns(
417+
DeprecationWarning,
418+
match="Use the `client_headers` parameter in the ToolboxClient constructor instead.",
415419
):
416-
client.add_headers({"X-Initial-Header": "another_value"})
420+
with pytest.raises(
421+
ValueError,
422+
match="Client header\\(s\\) `X-Initial-Header` already registered",
423+
):
424+
client.add_headers({"X-Initial-Header": "another_value"})
417425

418426

419427
class TestSyncAuth:

0 commit comments

Comments
 (0)