changes
This commit is contained in:
@@ -114,38 +114,63 @@ def api_call_action(params, context):
|
||||
|
||||
raw_body = params.get("body")
|
||||
|
||||
# Nếu body chứa biểu thức $map, thực hiện biến đổi dữ liệu trước khi gửi
|
||||
# ============================
|
||||
# Resolve body
|
||||
# ============================
|
||||
if isinstance(raw_body, str) and raw_body.startswith("$map"):
|
||||
body = handle_map_expression(raw_body, context)
|
||||
elif isinstance(raw_body, dict):
|
||||
body = deep_resolve_values(raw_body, context)
|
||||
elif raw_body is None:
|
||||
body = None
|
||||
else:
|
||||
body = resolve_value(raw_body, context)
|
||||
|
||||
print(f" [API_CALL] {method} {url}")
|
||||
print(f" [API_CALL] Resolved Body: {body}")
|
||||
|
||||
# Thực hiện request
|
||||
# ============================
|
||||
# Execute request
|
||||
# ============================
|
||||
if method == "POST":
|
||||
resp = client.post(url, body, content_type="application/json")
|
||||
elif method == "PATCH":
|
||||
resp = client.patch(url, body, content_type="application/json")
|
||||
elif method == "PUT":
|
||||
resp = client.put(url, body, content_type="application/json")
|
||||
elif method == "DELETE":
|
||||
resp = client.delete(url)
|
||||
else:
|
||||
resp = client.get(url)
|
||||
|
||||
print(f" [API_CALL] Status Code: {resp.status_code}")
|
||||
|
||||
# ============================
|
||||
# Handle error
|
||||
# ============================
|
||||
if resp.status_code >= 400:
|
||||
error_content = resp.content.decode('utf-8')
|
||||
error_content = resp.content.decode("utf-8") if resp.content else ""
|
||||
print(f" [API_CALL] Error: {error_content}")
|
||||
raise Exception(f"API Call failed: {error_content}")
|
||||
|
||||
result = resp.json()
|
||||
# ============================
|
||||
# Handle response safely
|
||||
# ============================
|
||||
if resp.status_code == 204 or not resp.content:
|
||||
# DELETE / No Content
|
||||
result = {"deleted": True}
|
||||
else:
|
||||
try:
|
||||
result = resp.json()
|
||||
except ValueError:
|
||||
# Fallback nếu response không phải JSON
|
||||
result = resp.content.decode("utf-8")
|
||||
|
||||
print(f" [API_CALL] Result: {result}")
|
||||
|
||||
if save_as:
|
||||
context[save_as] = result
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user