Skip to content

Commit 3e8c322

Browse files
Merge pull request #20 from gomessguii/fix/url-safe-characters
feat(custom_tools): URL encode path parameters and improve response handling
2 parents ef4e4ee + 9135aa5 commit 3e8c322

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/services/custom_tools.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from google.adk.tools import FunctionTool
3232
import requests
3333
import json
34+
import urllib.parse
3435
from src.utils.logger import setup_logger
3536

3637
logger = setup_logger(__name__)
@@ -70,7 +71,9 @@ def http_tool(**kwargs):
7071
url = endpoint
7172
for param, value in path_params.items():
7273
if param in all_values:
73-
url = url.replace(f"{{{param}}}", str(all_values[param]))
74+
# URL encode the value for URL safe characters
75+
replacement_value = urllib.parse.quote(str(all_values[param]), safe='')
76+
url = url.replace(f"{{{param}}}", replacement_value)
7477

7578
# Process query parameters
7679
query_params_dict = {}
@@ -119,8 +122,12 @@ def http_tool(**kwargs):
119122
f"Error in the request: {response.status_code} - {response.text}"
120123
)
121124

122-
# Always returns the response as a string
123-
return json.dumps(response.json())
125+
# Try to parse the response as JSON, if it fails, return the text content
126+
try:
127+
return json.dumps(response.json())
128+
except ValueError:
129+
# Response is not JSON, return the text content
130+
return json.dumps({"content": response.text})
124131

125132
except Exception as e:
126133
logger.error(f"Error executing tool {name}: {str(e)}")

0 commit comments

Comments
 (0)