Updated handling of next config in the driver

This commit is contained in:
dvanaken 2020-01-07 17:13:33 -05:00 committed by Dana Van Aken
parent 8cec62160f
commit 3786714093
2 changed files with 28 additions and 10 deletions

View File

@ -339,18 +339,20 @@ def get_result(max_time_sec=180, interval_sec=5, upload_code=None):
elapsed = 0 elapsed = 0
response_dict = None response_dict = None
response = '' response = ''
rout = ''
while elapsed <= max_time_sec: while elapsed <= max_time_sec:
rsp = requests.get(url) rsp = requests.get(url)
response = get_content(rsp) response = get_content(rsp)
assert response != 'null' assert response != 'null'
rout = json.dumps(response, indent=4) if isinstance(response, dict) else response
LOG.debug('%s [status code: %d, content_type: %s, elapsed: %ds]', response, LOG.debug('%s [status code: %d, content_type: %s, elapsed: %ds]', rout,
rsp.status_code, rsp.headers.get('content-type', ''), elapsed) rsp.status_code, rsp.headers.get('Content-Type', ''), elapsed)
if rsp.status_code == 200: if rsp.status_code == 200:
# Success # Success
response_dict = json.loads(rsp.json(), object_pairs_hook=OrderedDict) response_dict = response
break break
elif rsp.status_code == 202: elif rsp.status_code == 202:
@ -362,11 +364,23 @@ def get_result(max_time_sec=180, interval_sec=5, upload_code=None):
# Failure # Failure
raise Exception( raise Exception(
"Failed to download the next config.\nStatus code: {}\nMessage: {}\n".format( "Failed to download the next config.\nStatus code: {}\nMessage: {}\n".format(
rsp.status_code, response)) rsp.status_code, rout))
elif rsp.status_code == 500:
# Failure
if len(response) > 5000:
with open('error.html', 'w') as f:
f.write(response)
msg = "Saved HTML error to 'error.html'."
else:
msg = rout
raise Exception(
"Failed to download the next config.\nStatus code: {}\nMessage: {}\n".format(
rsp.status_code, msg))
else: else:
raise NotImplementedError( raise NotImplementedError(
"Unhandled status code: '{}'.\nMessage: {}".format(rsp.status_code, response)) "Unhandled status code: '{}'.\nMessage: {}".format(rsp.status_code, rout))
if not response_dict: if not response_dict:
assert elapsed > max_time_sec, \ assert elapsed > max_time_sec, \
@ -374,9 +388,9 @@ def get_result(max_time_sec=180, interval_sec=5, upload_code=None):
response, elapsed, max_time_sec) response, elapsed, max_time_sec)
raise Exception( raise Exception(
'Failed to download the next config in {}s: {} (elapsed: {}s)'.format( 'Failed to download the next config in {}s: {} (elapsed: {}s)'.format(
max_time_sec, response, elapsed)) max_time_sec, rout, elapsed))
LOG.info('Downloaded the next config in %ds: %s', elapsed, json.dumps(response_dict, indent=4)) LOG.info('Downloaded the next config in %ds: %s', elapsed, rout)
return response_dict return response_dict

View File

@ -54,9 +54,13 @@ def parse_bool(value):
def get_content(response): def get_content(response):
content = response.content content_type = response.headers.get('Content-Type', '')
if isinstance(content, bytes): if content_type == 'application/json':
content = content.decode('utf-8') content = response.json()
else:
content = response.content
if isinstance(content, bytes):
content = content.decode('utf-8')
return content return content