"""API Utility Functions. Functions to support generating API Responses. """ from copy import deepcopy from oto import status as response_code from oto.response import create_error_response from oto.response import Response from backend.constants import api as api_consts from backend.constants import error as error_consts from backend.constants import field as field_consts def create_get_list_response(items, status=response_code.OK): """Create get items response. Wraps item list in pagination API list. NOTE: Pagination isn't supported, this will need to be implemented if needed in the future. Args: items (list): List of objects status (int): HTTP response status code Returns: Response object """ total_records = len(items) message = { 'items': items, 'pagination': { 'type': api_consts.PAGINATION_TYPE_NONE, 'total_records': total_records } } return Response(message=message, status=status) def create_get_nested_lists_response(lists, status=response_code.OK, key_name='items'): """Create get items response. Wraps item nested lists in pagination API list. NOTE: Pagination isn't supported, this will need to be implemented if needed in the future. Args: lists (list): List of lists of objects status (int): HTTP response status code key_name (str): A key name: items, tracks... Returns: Response object """ message = [{key_name: sublist} for sublist in lists] return Response(message=message, status=status) def create_validation_response(items, errors, warnings=None): """Create track validation response. Args: items (list): List of unique entity objects errors (list): List of errors keyed by entity objects warnings (list): List of warnings keyed by entity objects Returns: Response object """ base_data = { field_consts.TOTAL_TRACKS: len(items), field_consts.VALID_TRACKS: len(items) - len(errors), field_consts.ERRORS: errors, field_consts.MESSAGE_VALID: False if len(errors) else True } if warnings: base_data[field_consts.WARNINGS] = warnings return Response(base_data) def create_ok_response(): """Create status 200 OK response.""" return Response() def create_validation_error_response( message=error_consts.VALIDATION_ERROR_MSG, *args): """Create validation error response. Args: message (str): Message for error *args: List of args to pass when formatting message """ return create_error_response( code=error_consts.VALIDATION_ERROR_CODE, message=message.format(*args)) def copy_response(response): """Copy response object. Args: response (Response): Response object Return: Response """ return Response( status=response.status, message=deepcopy(response.message), errors=deepcopy(response.errors)) def create_isrc_validation_response(items, count, products=None, status=response_code.OK,): """Create track validation response. Args: items (list): List of unique entity objects errors (list): List of errors keyed by entity objects warnings (list): List of warnings keyed by entity objects Returns: Response object """ base_data = { field_consts.TOTAL_PRODUCTS: len(items), field_consts.TOTAL_PRODUCT_ISSUES: count, field_consts.PRODUCTS: products, field_consts.MESSAGE_VALID: False if len(products) else True } return Response(status=status, message=base_data)