Skip to content

Commit 288c2c4

Browse files
xuanyang15copybara-github
authored andcommitted
chore: Add ADK logger in RestApiTool
Fixes: #3780 Co-authored-by: Xuan Yang <xygoogle@google.com> PiperOrigin-RevId: 862897383
1 parent ecce7e5 commit 288c2c4

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
import logging
1718
import ssl
1819
from typing import Any
1920
from typing import Callable
@@ -48,6 +49,8 @@
4849
from .operation_parser import OperationParser
4950
from .tool_auth_handler import ToolAuthHandler
5051

52+
logger = logging.getLogger("google_adk." + __name__)
53+
5154

5255
def snake_to_lower_camel(snake_case_string: str):
5356
"""Converts a snake_case string to a lower_camel_case string.
@@ -158,6 +161,7 @@ def __init__(
158161
self._default_headers: Dict[str, str] = {}
159162
self._ssl_verify = ssl_verify
160163
self._header_provider = header_provider
164+
self._logger = logger
161165
if should_parse_operation:
162166
self._operation_parser = OperationParser(self.operation)
163167

@@ -493,14 +497,40 @@ async def call(
493497
if provider_headers:
494498
request_params.setdefault("headers", {}).update(provider_headers)
495499

500+
# Log the API request
501+
self._logger.debug(
502+
"API Request: %s %s",
503+
request_params.get("method", "").upper(),
504+
request_params.get("url", ""),
505+
)
506+
self._logger.debug("API Request params: %s", request_params.get("params"))
507+
if "json" in request_params:
508+
self._logger.debug("API Request body: %s", request_params.get("json"))
509+
496510
response = requests.request(**request_params)
497511

512+
# Log the API response
513+
self._logger.debug(
514+
"API Response: %s %s - Status: %d",
515+
request_params.get("method", "").upper(),
516+
request_params.get("url", ""),
517+
response.status_code,
518+
)
519+
498520
# Parse API response
499521
try:
500522
response.raise_for_status() # Raise HTTPError for bad responses
501-
return response.json() # Try to decode JSON
523+
result = response.json() # Try to decode JSON
524+
self._logger.debug("API Response body: %s", result)
525+
return result
502526
except requests.exceptions.HTTPError:
503527
error_details = response.content.decode("utf-8")
528+
self._logger.warning(
529+
"API call failed for tool %s: Status %d - %s",
530+
self.name,
531+
response.status_code,
532+
error_details,
533+
)
504534
return {
505535
"error": (
506536
f"Tool {self.name} execution failed. Analyze this execution error"
@@ -510,6 +540,7 @@ async def call(
510540
)
511541
}
512542
except ValueError:
543+
self._logger.debug("API Response (non-JSON): %s", response.text)
513544
return {"text": response.text} # Return text if not JSON
514545

515546
def __str__(self):

tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,10 +1048,9 @@ async def test_call_with_verify_options(
10481048
if expected_verify_in_call == "USE_SSL_FIXTURE":
10491049
expected_verify_in_call = mock_ssl_context
10501050

1051-
mock_response = mock.create_autospec(
1052-
requests.Response, instance=True, spec_set=True
1053-
)
1051+
mock_response = mock.create_autospec(requests.Response, instance=True)
10541052
mock_response.json.return_value = {"result": "success"}
1053+
mock_response.configure_mock(status_code=200)
10551054

10561055
tool = RestApiTool(
10571056
name="test_tool",
@@ -1084,10 +1083,9 @@ async def test_call_with_configure_verify(
10841083
sample_auth_credential,
10851084
):
10861085
"""Test that configure_verify updates the verify setting."""
1087-
mock_response = mock.create_autospec(
1088-
requests.Response, instance=True, spec_set=True
1089-
)
1086+
mock_response = mock.create_autospec(requests.Response, instance=True)
10901087
mock_response.json.return_value = {"result": "success"}
1088+
mock_response.configure_mock(status_code=200)
10911089

10921090
tool = RestApiTool(
10931091
name="test_tool",
@@ -1153,10 +1151,9 @@ async def test_call_with_header_provider(
11531151
sample_auth_credential,
11541152
):
11551153
"""Test that header_provider adds headers to the request."""
1156-
mock_response = mock.create_autospec(
1157-
requests.Response, instance=True, spec_set=True
1158-
)
1154+
mock_response = mock.create_autospec(requests.Response, instance=True)
11591155
mock_response.json.return_value = {"result": "success"}
1156+
mock_response.configure_mock(status_code=200)
11601157

11611158
def my_header_provider(context):
11621159
return {"X-Custom-Header": "custom-value", "X-Request-ID": "12345"}
@@ -1192,10 +1189,9 @@ async def test_call_header_provider_receives_tool_context(
11921189
sample_auth_credential,
11931190
):
11941191
"""Test that header_provider receives the tool_context."""
1195-
mock_response = mock.create_autospec(
1196-
requests.Response, instance=True, spec_set=True
1197-
)
1192+
mock_response = mock.create_autospec(requests.Response, instance=True)
11981193
mock_response.json.return_value = {"result": "success"}
1194+
mock_response.configure_mock(status_code=200)
11991195

12001196
received_context = []
12011197

@@ -1232,10 +1228,9 @@ async def test_call_without_header_provider(
12321228
sample_auth_credential,
12331229
):
12341230
"""Test that call works without header_provider."""
1235-
mock_response = mock.create_autospec(
1236-
requests.Response, instance=True, spec_set=True
1237-
)
1231+
mock_response = mock.create_autospec(requests.Response, instance=True)
12381232
mock_response.json.return_value = {"result": "success"}
1233+
mock_response.configure_mock(status_code=200)
12391234

12401235
tool = RestApiTool(
12411236
name="test_tool",

0 commit comments

Comments
 (0)