14
14
15
15
import asyncio
16
16
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
18
18
19
19
from .client import ToolboxClient
20
20
from .sync_tool import ToolboxSyncTool
24
24
25
25
class ToolboxSyncClient :
26
26
"""
27
- An synchronous client for interacting with a Toolbox service.
27
+ A synchronous client for interacting with a Toolbox service.
28
28
29
29
Provides methods to discover and load tools defined by a remote Toolbox
30
30
service endpoint.
@@ -36,12 +36,14 @@ class ToolboxSyncClient:
36
36
def __init__ (
37
37
self ,
38
38
url : str ,
39
+ client_headers : Optional [Mapping [str , Union [Callable , Coroutine , str ]]] = None ,
39
40
):
40
41
"""
41
42
Initializes the ToolboxSyncClient.
42
43
43
44
Args:
44
45
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.
45
47
"""
46
48
# Running a loop in a background thread allows us to support async
47
49
# methods from non-async environments.
@@ -53,7 +55,7 @@ def __init__(
53
55
self .__class__ .__loop = loop
54
56
55
57
async def create_client ():
56
- return ToolboxClient (url )
58
+ return ToolboxClient (url , client_headers = client_headers )
57
59
58
60
# Ignoring type since we're already checking the existence of a loop above.
59
61
self .__async_client = asyncio .run_coroutine_threadsafe (
@@ -138,6 +140,23 @@ def load_toolset(
138
140
for async_tool in async_tools
139
141
]
140
142
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
+
141
160
def __enter__ (self ):
142
161
"""Enter the runtime context related to this client instance."""
143
162
return self
0 commit comments