59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
import json
|
|
import requests
|
|
|
|
def buildOutput(response, action, outputs):
|
|
if hasattr(action, "outputs"):
|
|
try:
|
|
if hasattr(response, "content"):
|
|
if not response.content:
|
|
return outputs
|
|
|
|
response_data = response.json()
|
|
|
|
else:
|
|
if not response:
|
|
return outputs
|
|
|
|
response_data = json.loads(response)
|
|
|
|
except (json.JSONDecodeError, requests.exceptions.JSONDecodeError) as error:
|
|
print(f"JSON decoding failed during {action.name}: {error}")
|
|
return outputs
|
|
|
|
#if response is not None:
|
|
# if not response.content:
|
|
# return outputs
|
|
#
|
|
# try:
|
|
# response_data = response.json()
|
|
# except AttributeError:
|
|
# response_data = json.loads(response)
|
|
|
|
|
|
if isinstance(response_data, list):
|
|
outputs["response_list"] = response_data
|
|
return outputs
|
|
|
|
|
|
response_objects = [response_data]
|
|
|
|
while response_objects:
|
|
response_object = response_objects.pop()
|
|
|
|
for key, value, in response_object.items():
|
|
|
|
if isinstance(value, dict):
|
|
response_objects.append(value)
|
|
|
|
for output_name, response_key in action.outputs.items():
|
|
if key == response_key:
|
|
outputs[output_name] = value
|
|
#print (response_data)
|
|
|
|
#else:
|
|
# print("No response received"),
|
|
|
|
return
|
|
|
|
|