"""Request helper methods.""" from ledger.constants.constants import ( DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET, ERROR_INVALID_LIMIT_OFFSET, ) from ledger.constants.error import ERROR_POST_REQUEST_DUPLICATE_RECORDS def validate_pagination_params( limit: int = DEFAULT_PAGE_LIMIT, offset: int = DEFAULT_PAGE_OFFSET ) -> dict: """Format and validate pagination parameters.""" try: limit = int(limit) offset = int(offset) except ValueError: raise Exception(ERROR_INVALID_LIMIT_OFFSET) pagination_params = { 'limit': max(limit, 1), 'offset': max(offset, DEFAULT_PAGE_OFFSET), } return pagination_params def validate_post_request_contains_unique_data(request_body: list): """Validate if post request body contains unique records. Args: request_body (list): a POST request body """ list_of_unique_records = list() for record in request_body: if record in list_of_unique_records: raise Exception(ERROR_POST_REQUEST_DUPLICATE_RECORDS.format(record)) else: list_of_unique_records.append(record) return True