from labelaudit.lib import string_util """ Presentation ================= Thin presentation layer that handles structuring data for responses from this microservice API. These functions do not handle JSON encoding. That allows the output of any of these functions to be reused and possibly modified by somwhere else without the need for intermediate decoding. This will be useful, for example, when we need to index label audits and we want to reuse the label_audit presentation function to present a list of label audits. """ def failure(errors): """Crafts the contents of an error response. Args: errors (dict): error data to be encoded in the response body Return: dict: response contents """ return {'errors': errors} def label_audit(label_audit): """Crafts a representation of a label audit. Args: label_audit (dict): audit data from VAPI Return: dict: response contents """ properties = ('audit_status', 'created_timestamp', 'initiated_by_id', 'initiated_by_login', 'report_location', 'updated_timestamp', 'vendor_id', 'youtube_audit_id') data = {} # Convert the camel-cased keys from VAPI to snake-case for prop in properties: data[prop] = label_audit.get(string_util.snake_to_camel(prop)) return data def label_audits(vapi_audit_data): """Crafts a response containing a list of label audits. Args: vapi_audit_data (dict): Data from the audit_persistence.fetch_all. Return: dict: response contents. Structure: { 'audits': [ ... individual audit representations here ], 'total_records': 50 } """ formatted_audits = [] reports = vapi_audit_data.get('reports') or [] for vapi_audit in reports: formatted_audits.append(label_audit(vapi_audit)) return { 'audits': formatted_audits, 'total_records': vapi_audit_data.get('total_reports')}