This commit is contained in:
anhduy-tech
2026-01-20 11:33:42 +07:00
parent c6f6cc1aa1
commit 20cbb4ab39

View File

@@ -111,41 +111,66 @@ def api_call_action(params, context):
method = params["method"].upper()
url = resolve_value(params["url"], context)
save_as = params.get("save_as")
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