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