Skip to content

Commit 50e32da

Browse files
feat(core): Add client_headers to ToolboxSyncClient (#187)
* iter1: poc # Conflicts: # packages/toolbox-core/src/toolbox_core/client.py # packages/toolbox-core/src/toolbox_core/tool.py * remove client headers from tool * merge correction * cleanup * client headers functionality * small diff * mypy * raise error on duplicate headers * docs * add client headers to tool * lint * lint * fix * add client tests * add client tests * fix tests * fix * lint * fix tests * cleanup * cleanup * lint * fix * cleanup * lint * lint * lint * lint * Update packages/toolbox-core/src/toolbox_core/client.py Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * lint * fix * cleanup * use mock_tool_load in test * test cleanup * test cleanup * lint * feat: add headers using sync client * lint * lint * add docstrings * lint --------- Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
1 parent 58d8f7d commit 50e32da

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import asyncio
1616
from threading import Thread
17-
from typing import Any, Callable, Mapping, Optional, TypeVar, Union
17+
from typing import Any, Callable, Coroutine, Mapping, Optional, TypeVar, Union
1818

1919
from .client import ToolboxClient
2020
from .sync_tool import ToolboxSyncTool
@@ -24,7 +24,7 @@
2424

2525
class ToolboxSyncClient:
2626
"""
27-
An synchronous client for interacting with a Toolbox service.
27+
A synchronous client for interacting with a Toolbox service.
2828
2929
Provides methods to discover and load tools defined by a remote Toolbox
3030
service endpoint.
@@ -36,12 +36,14 @@ class ToolboxSyncClient:
3636
def __init__(
3737
self,
3838
url: str,
39+
client_headers: Optional[Mapping[str, Union[Callable, Coroutine, str]]] = None,
3940
):
4041
"""
4142
Initializes the ToolboxSyncClient.
4243
4344
Args:
4445
url: The base URL for the Toolbox service API (e.g., "http://localhost:5000").
46+
client_headers: Headers to include in each request sent through this client.
4547
"""
4648
# Running a loop in a background thread allows us to support async
4749
# methods from non-async environments.
@@ -53,7 +55,7 @@ def __init__(
5355
self.__class__.__loop = loop
5456

5557
async def create_client():
56-
return ToolboxClient(url)
58+
return ToolboxClient(url, client_headers=client_headers)
5759

5860
# Ignoring type since we're already checking the existence of a loop above.
5961
self.__async_client = asyncio.run_coroutine_threadsafe(
@@ -138,6 +140,23 @@ def load_toolset(
138140
for async_tool in async_tools
139141
]
140142

143+
def add_headers(
144+
self, headers: Mapping[str, Union[Callable, Coroutine, str]]
145+
) -> None:
146+
"""
147+
Synchronously Add headers to be included in each request sent through this client.
148+
149+
Args:
150+
headers: Headers to include in each request sent through this client.
151+
152+
Raises:
153+
ValueError: If any of the headers are already registered in the client.
154+
"""
155+
coro = self.__async_client.add_headers(headers)
156+
157+
# We have already created a new loop in the init method in case it does not already exist
158+
asyncio.run_coroutine_threadsafe(coro, self.__loop).result() # type: ignore
159+
141160
def __enter__(self):
142161
"""Enter the runtime context related to this client instance."""
143162
return self

0 commit comments

Comments
 (0)