changes
This commit is contained in:
@@ -114,38 +114,63 @@ def api_call_action(params, context):
|
|||||||
|
|
||||||
raw_body = params.get("body")
|
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"):
|
if isinstance(raw_body, str) and raw_body.startswith("$map"):
|
||||||
body = handle_map_expression(raw_body, context)
|
body = handle_map_expression(raw_body, context)
|
||||||
elif isinstance(raw_body, dict):
|
elif isinstance(raw_body, dict):
|
||||||
body = deep_resolve_values(raw_body, context)
|
body = deep_resolve_values(raw_body, context)
|
||||||
|
elif raw_body is None:
|
||||||
|
body = None
|
||||||
else:
|
else:
|
||||||
body = resolve_value(raw_body, context)
|
body = resolve_value(raw_body, context)
|
||||||
|
|
||||||
print(f" [API_CALL] {method} {url}")
|
print(f" [API_CALL] {method} {url}")
|
||||||
print(f" [API_CALL] Resolved Body: {body}")
|
print(f" [API_CALL] Resolved Body: {body}")
|
||||||
|
|
||||||
# Thực hiện request
|
# ============================
|
||||||
|
# Execute request
|
||||||
|
# ============================
|
||||||
if method == "POST":
|
if method == "POST":
|
||||||
resp = client.post(url, body, content_type="application/json")
|
resp = client.post(url, body, content_type="application/json")
|
||||||
elif method == "PATCH":
|
elif method == "PATCH":
|
||||||
resp = client.patch(url, body, content_type="application/json")
|
resp = client.patch(url, body, content_type="application/json")
|
||||||
elif method == "PUT":
|
elif method == "PUT":
|
||||||
resp = client.put(url, body, content_type="application/json")
|
resp = client.put(url, body, content_type="application/json")
|
||||||
|
elif method == "DELETE":
|
||||||
|
resp = client.delete(url)
|
||||||
else:
|
else:
|
||||||
resp = client.get(url)
|
resp = client.get(url)
|
||||||
|
|
||||||
print(f" [API_CALL] Status Code: {resp.status_code}")
|
print(f" [API_CALL] Status Code: {resp.status_code}")
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# Handle error
|
||||||
|
# ============================
|
||||||
if resp.status_code >= 400:
|
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}")
|
print(f" [API_CALL] Error: {error_content}")
|
||||||
raise Exception(f"API Call failed: {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}")
|
print(f" [API_CALL] Result: {result}")
|
||||||
|
|
||||||
if save_as:
|
if save_as:
|
||||||
context[save_as] = result
|
context[save_as] = result
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user