Skip to content

Commit 3996901

Browse files
committed
dumping in text mode as last resort
1 parent ad19d07 commit 3996901

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

python-lib/rest_api_recipe_session.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataikuapi.utils import DataikuException
22
from rest_api_client import RestAPIClient
33
from safe_logger import SafeLogger
4-
from dku_utils import parse_keys_for_json, get_value_from_path, decode_csv_data, de_NaN
4+
from dku_utils import parse_keys_for_json, get_value_from_path, decode_csv_data, de_NaN, decode_bytes
55
from dku_constants import DKUConstants
66
import copy
77
import json
@@ -108,6 +108,7 @@ def retrieve_next_page(self, is_raw_output):
108108
# Todo: check api_response key is free and add something overwise
109109
base_row = copy.deepcopy(metadata)
110110
if is_raw_output:
111+
assert_json(json_response)
111112
if is_error_message(json_response):
112113
base_row.update(parse_keys_for_json(json_response))
113114
else:
@@ -125,9 +126,18 @@ def retrieve_next_page(self, is_raw_output):
125126
base_row.update(self.initial_parameter_columns)
126127
page_rows.append(base_row)
127128
else:
128-
json_response = decode_csv_data(json_response)
129+
decoded_csv_data = decode_csv_data(json_response)
129130
is_api_returning_dict = False
130-
for row in json_response:
131+
if not decoded_csv_data and json_response:
132+
logger.warning("Data is not in CSV format. Dumping it in text mode.")
133+
decoded_csv_data = [
134+
{
135+
DKUConstants.API_RESPONSE_KEY: "{}".format(
136+
decode_bytes(json_response)
137+
)
138+
}
139+
]
140+
for row in decoded_csv_data:
131141
base_row = copy.deepcopy(metadata)
132142
base_row.update(parse_keys_for_json(row))
133143
base_row.update(self.initial_parameter_columns)
@@ -182,3 +192,9 @@ def is_error_message(jsons_response):
182192
return True
183193
else:
184194
return False
195+
196+
197+
def assert_json(variable_to_check):
198+
if isinstance(variable_to_check, dict) or isinstance(variable_to_check, list):
199+
return
200+
raise Exception("Returned data is not JSON format. Try again with 'Raw JSON output' un-checked.")

0 commit comments

Comments
 (0)