Made outputs response parsing and none handling more reliable.
This commit is contained in:
+44
-18
@@ -1,32 +1,58 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
def buildOutput(response, action, outputs):
|
||||
if hasattr(action, "outputs"):
|
||||
if response is not None:
|
||||
try:
|
||||
try:
|
||||
if hasattr(response, "content"):
|
||||
if not response.content:
|
||||
return outputs
|
||||
|
||||
response_data = response.json()
|
||||
except AttributeError:
|
||||
|
||||
else:
|
||||
if not response:
|
||||
return outputs
|
||||
|
||||
response_data = json.loads(response)
|
||||
response_objects = [response_data]
|
||||
|
||||
while response_objects:
|
||||
response_object = response_objects.pop()
|
||||
except (json.JSONDecodeError, requests.exceptions.JSONDecodeError) as error:
|
||||
print(f"JSON decoding failed during {action.name}: {error}")
|
||||
return outputs
|
||||
|
||||
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)
|
||||
#if response is not None:
|
||||
# if not response.content:
|
||||
# return outputs
|
||||
#
|
||||
# try:
|
||||
# response_data = response.json()
|
||||
# except AttributeError:
|
||||
# response_data = json.loads(response)
|
||||
|
||||
else:
|
||||
print("No response received"),
|
||||
|
||||
|
||||
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 outputs
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user