Skip to content

Commit 7fe6064

Browse files
rcsantana777copybara-github
authored andcommitted
feat: Add custom User-Agent header to RestApiTool requests
Merge #2641 This PR adds a custom `User-Agent` header to all requests made via `RestApiTool`. This allows backend services to identify traffic originating from the ADK. The header format is `google-adk/<version> (tool: <tool_name>)`, where `<version>` is the current version of the `google-adk` package, fetched dynamically from `google.adk.version`, and `<tool_name>` is the name of the specific `RestApiTool` instance. **Associated Issue** Fixes #2676 **Testing Plan** **Unit Tests** I ran the full suite of unit tests locally to ensure the changes did not introduce any regressions. All tests passed successfully. ```bash $ pytest ./tests/unittests ================================= 4202 passed, 2459 warnings in 44.68s ==================== ``` The warnings are related to existing experimental features and are not affected by this change. **Manual End-to-End (E2E) Test** I performed a manual test to ensure the integrated flow works as expected. * **Setup:** Created a clean virtual environment (`~/venvs/adk-e2e-test`) and installed the locally built `google-adk` package using `uv venv --seed` and `pip install dist/google_adk-*.whl`. Ran a custom `mock_server.py` script (using Flask) in one terminal to listen for requests and print headers. Ran a custom `run_test_tool.py` script in a second terminal to send a request using the modified `RestApiTool`. * **Execution:** The `run_test_tool.py` script successfully sent a POST request to the mock server's `/test` endpoint. The mock server received the request and printed the headers. **`run_test_tool.py` Output:** ```json { "message": "Request received successfully!" } ``` **`mock_server.py` Output (Headers):** ```text --- Request Received --- Headers: Host: 127.0.0.1:8000 User-Agent: google-adk/1.12.0 (tool: TestTool) Accept-Encoding: gzip, deflate Accept: */* Connection: keep-alive Content-Type: application/json Content-Length: 2 ------------------------ ``` The `User-Agent: google-adk/1.12.0 (tool: TestTool)` header confirms the change is working as intended, dynamically fetching the package version and including the tool name. **Checklist** * [x] Associated issue linked * [x] Unit tests passed * [x] End-to-end test performed and results documented * [x] Code formatted with `./autoformat.sh` --- COPYBARA_INTEGRATE_REVIEW=#2641 from rcsantana777:feat/add-user-agent a9a9375 PiperOrigin-RevId: 798232385
1 parent 395a50b commit 7fe6064

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ def _prepare_request_params(
245245
Example:
246246
self._prepare_request_params({"input_id": "test-id"})
247247
"""
248+
248249
method = self.endpoint.method.lower()
249250
if not method:
250251
raise ValueError("Operation method not found.")
@@ -254,6 +255,12 @@ def _prepare_request_params(
254255
header_params: Dict[str, Any] = {}
255256
cookie_params: Dict[str, Any] = {}
256257

258+
from ....version import __version__ as adk_version
259+
260+
# Set the custom User-Agent header
261+
user_agent = f"google-adk/{adk_version} (tool: {self.name})"
262+
header_params["User-Agent"] = user_agent
263+
257264
params_map: Dict[str, ApiParameter] = {p.py_name: p for p in parameters}
258265

259266
# Fill in path, query, header and cookie parameters to the request

0 commit comments

Comments
 (0)