""" Ownership change request message processing logic ================================================= """ from codecs import utf_8_decode from datetime import datetime from ytownership import config from ytownership.connectors import mysql from ytownership.connectors.sentry import sentry_client from ytownership.logic import patch_ownership from ytownership.models.apilog import YouTubeApiLog from ytownership.validation.ownership import validate_sqs_message class MessageProcessingResult: """Represents result of message processing Depending on response code of YouTube API we should create different message processing results. We could have both recoverable errors (e.g. YouTube API server error 500), and non-recoverable errors (e.g. 404). For recoverable errors, the processing of the message should be retried. """ SUCCESS_STATUSES = [200] ERROR_STATUSES = [500, 401] FAILURE_STATUSES = [400, 403, 404] SUCCESS = 'success' FAILURE = 'failure' ERROR = 'error' @classmethod def get_processing_status(cls, status_code): """Maps status code, received from YouTube to MessageProcessingStatus Args: status_code (int): HTTP status code, received from YouTube Returns: str: SUCCESS, FAILURE or ERROR status """ if status_code in cls.ERROR_STATUSES: return cls.ERROR elif status_code in cls.FAILURE_STATUSES: return cls.FAILURE elif status_code in cls.SUCCESS_STATUSES: return cls.SUCCESS return cls.FAILURE def process_message(message): """Process ownership change request The processing consists of the following steps: 1) Validate the message payload against schema 2) Send a request to YouTube Content ID API 3) Save results of YouTube API call 4) return message processing result Args: message (JSONMessageExt): ownership change request message Returns: MessageProcessingResult """ if not validate_sqs_message(message): return MessageProcessingResult.FAILURE response = patch_ownership.patch_ownership(message) save_result(message, response) result = MessageProcessingResult.get_processing_status(response.status) return result def save_result(message, response): """Persists YouTube API call results to database Args: message (JSONMessageExt): ownership change request message response (Response): API call response """ if response.status in MessageProcessingResult.SUCCESS_STATUSES\ and not config.TRACK_SUCCESSFUL_API_CALLS: return True payload = message.get_body() try: with mysql.session_scope() as db_session: log = YouTubeApiLog() log.status = 'success' if response.status == 200 else 'error' log.territories = ','.join(payload['territories']) log.code = response.status log.content_owner_id = payload['content_owner_id'] log.correlation_id = message.correlation_id log.created_date_utc = datetime.utcnow() log.isrc = payload['isrc'] message = response.message if response.errors and type(response.errors) is bytes: message += ' errors: {0}' + utf_8_decode(response.errors)[0] log.message = message db_session.add(log) except Exception: if sentry_client: sentry_client.captureException() return False return True