Skip to content

feat(core): Add client_headers to ToolboxSyncClient #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
Apr 22, 2025
Merged
Changes from 45 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
cd59f54
iter1: poc
twishabansal Apr 15, 2025
297f5a9
remove client headers from tool
twishabansal Apr 17, 2025
7a30444
merge correction
twishabansal Apr 17, 2025
c78b74e
cleanup
twishabansal Apr 17, 2025
4b92ace
client headers functionality
twishabansal Apr 17, 2025
0b739ce
Merge branch 'main' into add-headers-poc
twishabansal Apr 17, 2025
37a3984
small diff
twishabansal Apr 17, 2025
d7ab445
Merge remote-tracking branch 'origin/add-headers-poc' into add-header…
twishabansal Apr 17, 2025
bc6ca96
mypy
twishabansal Apr 17, 2025
f5fc526
raise error on duplicate headers
twishabansal Apr 21, 2025
5e91f15
docs
twishabansal Apr 21, 2025
154edc1
add client headers to tool
twishabansal Apr 21, 2025
b83eaef
lint
twishabansal Apr 21, 2025
6965ba1
lint
twishabansal Apr 21, 2025
e8a53c0
fix
twishabansal Apr 21, 2025
ea319e5
add client tests
twishabansal Apr 21, 2025
abeb8de
add client tests
twishabansal Apr 21, 2025
45c0fc6
fix tests
twishabansal Apr 21, 2025
7693011
fix
twishabansal Apr 21, 2025
a43ffad
lint
twishabansal Apr 21, 2025
f564e60
fix tests
twishabansal Apr 21, 2025
2361cdf
cleanup
twishabansal Apr 21, 2025
d46bfd0
cleanup
twishabansal Apr 21, 2025
f2f5cd2
lint
twishabansal Apr 21, 2025
0fd1ca2
fix
twishabansal Apr 21, 2025
516d151
cleanup
twishabansal Apr 21, 2025
16b6664
Merge branch 'main' into add-headers-poc
twishabansal Apr 21, 2025
80b40d7
lint
twishabansal Apr 21, 2025
70d55b6
lint
twishabansal Apr 21, 2025
551635b
lint
twishabansal Apr 21, 2025
2880b4d
lint
twishabansal Apr 21, 2025
7990b99
Update packages/toolbox-core/src/toolbox_core/client.py
twishabansal Apr 22, 2025
60572b4
lint
twishabansal Apr 22, 2025
1826ad9
fix
twishabansal Apr 22, 2025
a4eafe6
cleanup
twishabansal Apr 22, 2025
8502db5
use mock_tool_load in test
twishabansal Apr 22, 2025
d0731ec
test cleanup
twishabansal Apr 22, 2025
560dfec
test cleanup
twishabansal Apr 22, 2025
1ac9a14
lint
twishabansal Apr 22, 2025
35901aa
feat: add headers using sync client
twishabansal Apr 22, 2025
dbe326d
Merge branch 'main' into sync-add-headers
twishabansal Apr 22, 2025
e22511a
Merge branch 'main' into sync-add-headers
twishabansal Apr 22, 2025
af99a0e
lint
twishabansal Apr 22, 2025
71eaa9a
lint
twishabansal Apr 22, 2025
1cc9c0c
add docstrings
twishabansal Apr 22, 2025
c045984
lint
twishabansal Apr 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/toolbox-core/src/toolbox_core/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import asyncio
from threading import Thread
from typing import Any, Callable, Mapping, Optional, TypeVar, Union
from typing import Any, Callable, Coroutine, Mapping, Optional, TypeVar, Union

from .client import ToolboxClient
from .sync_tool import ToolboxSyncTool
Expand All @@ -24,7 +24,7 @@

class ToolboxSyncClient:
"""
An synchronous client for interacting with a Toolbox service.
A synchronous client for interacting with a Toolbox service.

Provides methods to discover and load tools defined by a remote Toolbox
service endpoint.
Expand All @@ -36,12 +36,14 @@ class ToolboxSyncClient:
def __init__(
self,
url: str,
client_headers: Optional[Mapping[str, Union[Callable, Coroutine, str]]] = None,
):
"""
Initializes the ToolboxSyncClient.

Args:
url: The base URL for the Toolbox service API (e.g., "http://localhost:5000").
client_headers: Headers to include in each request sent through this client.
"""
# Running a loop in a background thread allows us to support async
# methods from non-async environments.
Expand All @@ -53,7 +55,7 @@ def __init__(
self.__class__.__loop = loop

async def create_client():
return ToolboxClient(url)
return ToolboxClient(url, client_headers=client_headers)

# Ignoring type since we're already checking the existence of a loop above.
self.__async_client = asyncio.run_coroutine_threadsafe(
Expand Down Expand Up @@ -138,6 +140,21 @@ def load_toolset(
for async_tool in async_tools
]

def add_headers(self, headers: Mapping[str, Union[Callable, Coroutine, str]]) -> None:
"""
Synchronously Add headers to be included in each request sent through this client.

Args:
headers: Headers to include in each request sent through this client.

Raises:
ValueError: If any of the headers are already registered in the client.
"""
coro = self.__async_client.add_headers(headers)

# We have already created a new loop in the init method in case it does not already exist
asyncio.run_coroutine_threadsafe(coro, self.__loop).result() # type: ignore

def __enter__(self):
"""Enter the runtime context related to this client instance."""
return self
Expand Down
Loading