Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit c60f96a

Browse files
author
Tony Oreglia
committed
better action export error handling
1 parent ccd2ee1 commit c60f96a

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

safetypy/safetypy.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,26 +388,29 @@ def get_audit_actions(self, date_modified, offset=0, page_length=100):
388388
)
389389
result = self.parse_json(response.content) if response.status_code == requests.codes.ok else None
390390
self.log_http_status(response.status_code, 'GET actions')
391-
if result is None or None in [result.get('count'), result.get('offset'), result.get('total')]:
391+
if result is None or None in [result.get('count'), result.get('offset'), result.get('total'), result.get('actions')]:
392392
return None
393393
return self.get_page_of_actions(logger, date_modified, result, offset, page_length)
394394

395-
def get_page_of_actions(self, logger, date_modified, result, offset=0, page_length=100):
395+
def get_page_of_actions(self, logger, date_modified, previous_page, offset=0, page_length=100):
396396
"""
397397
Returns a page of action search results
398398
399399
:param logger: the logger
400400
:param date_modified: fetch from that date onwards
401-
:param result: a page of action search results
401+
:param previous_page: a page of action search results
402402
:param offset: the index to start retrieving actions from
403403
:param page_length: the number of actions to return on the search page (max 100)
404404
:return: Array of action objects
405405
"""
406-
if result['count'] + result['offset'] < result['total']:
407-
logger.info('Paging Actions. Offset: ' + str(offset + page_length) + '. Total: ' + str(result['total']))
408-
return self.get_audit_actions(date_modified, offset + page_length) + result['actions']
409-
elif result['count'] + result['offset'] == result['total']:
410-
return result['actions']
406+
if previous_page['count'] + previous_page['offset'] < previous_page['total']:
407+
logger.info('Paging Actions. Offset: ' + str(offset + page_length) + '. Total: ' + str(previous_page['total']))
408+
next_page = self.get_audit_actions(date_modified, offset + page_length)
409+
if next_page is None:
410+
return None
411+
return next_page + previous_page['actions']
412+
elif previous_page['count'] + previous_page['offset'] == previous_page['total']:
413+
return previous_page['actions']
411414

412415
def get_audit(self, audit_id):
413416
"""
@@ -431,7 +434,6 @@ def log_http_status(status_code, message):
431434
:param status_code: http status code to log
432435
:param message: to describe where the status code was obtained
433436
"""
434-
435437
logger = logging.getLogger('sp_logger')
436438
status_description = requests.status_codes._codes[status_code][0]
437439
log_string = str(status_code) + ' [' + status_description + '] status received ' + message

tools/exporter/exporter.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,6 @@ def show_export_profiles_and_exit(list_export_profiles, sc_client):
719719
print(row_boundary)
720720
sys.exit(0)
721721

722-
723722
def export_actions(logger, settings, sc_client):
724723
"""
725724
Export all actions created after date specified
@@ -745,15 +744,13 @@ def sync_exports(logger, settings, sc_client):
745744
:param settings: Settings from command line and configuration file
746745
:param sc_client: Instance of SDK object
747746
"""
748-
# GET list of audits
749-
last_successful = get_last_successful(logger)
750-
list_of_audits = sc_client.discover_audits(modified_after=last_successful)
751-
# exporting actions if specified by user
752747
if 'actions' in settings[EXPORT_FORMATS]:
753748
export_actions(logger, settings, sc_client)
754-
# if there are audits start exporting
755-
if list_of_audits is not None and bool(set(settings[EXPORT_FORMATS]) & {'pdf', 'docx', 'csv', 'media',
756-
'web-report-link'}):
749+
if not bool(set(settings[EXPORT_FORMATS]) & {'pdf', 'docx', 'csv', 'media', 'web-report-link'}):
750+
return
751+
last_successful = get_last_successful(logger)
752+
list_of_audits = sc_client.discover_audits(modified_after=last_successful)
753+
if list_of_audits is not None:
757754
logger.info(str(list_of_audits['total']) + ' audits discovered')
758755
export_count = 1
759756
export_total = list_of_audits['total']

0 commit comments

Comments
 (0)