1414
1515import asyncio
1616from 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
1919from .client import ToolboxClient
2020from .sync_tool import ToolboxSyncTool
2424
2525class 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