diff --git a/core/runtime/outputs.py b/core/runtime/outputs.py index f5dc045..43f206a 100644 --- a/core/runtime/outputs.py +++ b/core/runtime/outputs.py @@ -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