def _export_v3_job_to_queue(self, request_params, request_retry=None, request_label="TMC v3 Job On Queue"):
        """Gather data using action export.json.

        :param request_params:
        :param request_retry:
        :return:
        """

        request_url = \
            self.tune_mat_request_path(
                mat_api_version="v3",
                controller=self.controller,
                action="exports/{}".format(
                    self.logs_advertisers_type
                )
            )

        self.logger.debug(
            msg=("TMC v3 Logs Advertisers Base: "
                 "Logs '{}': "
                 "Action 'exports': "
                 "Start Advertiser Report Actuals").format(self.logs_advertisers_type),
            extra={'start_date': request_params["start_date"],
                   'end_date': request_params["end_date"]}
        )

        try:
            response = self.mv_request.request(
                request_method="GET",
                request_url=request_url,
                request_params=request_params,
                request_retry=request_retry,
                request_retry_http_status_codes=None,
                request_retry_func=self.tune_v3_request_retry_func,
                request_retry_excps_func=None,
                request_label=request_label
            )

        except TuneRequestBaseError as tmc_req_ex:
            self.logger.error(
                "TMC v3 Logs Advertisers Base: Failed",
                extra=tmc_req_ex.to_dict(),
            )
            raise

        except TuneReportingError as tmc_rep_ex:
            self.logger.error(
                "TMC v3 Logs Advertisers Base: Failed",
                extra=tmc_rep_ex.to_dict(),
            )
            raise

        except Exception as ex:
            print_traceback(ex)

            self.logger.error("TMC v3 Logs Advertisers Base: {}".format(get_exception_message(ex)))
            raise

        json_response = validate_json_response(
            response,
            request_curl=self.mv_request.built_request_curl,
            request_label="TMC v3 Logs Advertisers: '{}': Action 'exports'".format(self.logs_advertisers_type)
        )

        if ('handle' not in json_response or not json_response['handle']):
            raise TuneReportingError(
                error_message=(
                    "TMC v3 Logs Advertisers Base: "
                    "Logs '{}': "
                    "Action 'exports': "
                    "Response missing 'handle': {}"
                ).format(self.logs_advertisers_type, str(json_response)),
                error_code=TuneReportingErrorCodes.REP_ERR_UNEXPECTED_VALUE
            )

        export_job = json_response['handle']

        if not export_job:
            raise TuneReportingError(
                error_message=(
                    "TMC v3 Logs Advertisers Base: "
                    "Logs '{}': "
                    "Action 'exports': "
                    "Response missing 'handle'"
                ).format(self.logs_advertisers_type),
                error_code=TuneReportingErrorCodes.REP_ERR_UNEXPECTED_VALUE
            )

        self.logger.info(
            "TuneV3LogsAdvertisersBase",
            extra={
                'advertiser_type': self.logs_advertisers_type,
                'action': 'exports',
                'start_date': request_params["start_date"],
                'end_date': request_params["end_date"],
                'job': export_job
            }
        )

        return export_job
    def _find_v3(self, request_params, request_retry=None, request_label="TMC v3 Logs Advertisers"):
        """Gather data using action 'find'

        Args:
            request_params:
            request_retry:

        Returns:

        """
        self.logger.debug(("TMC v3 Logs Advertisers Base: "
                           "Logs '{}': "
                           "Action 'find'").format(self.logs_advertisers_type))

        if "start_date" not in request_params:
            raise ValueError("Missing attribute 'start_date' in parameter 'request_params'")
        request_start_date = request_params["start_date"]

        if "end_date" not in request_params:
            raise ValueError("Missing attribute 'end_date' in parameter 'request_params'")
        request_end_date = request_params["end_date"]

        datetime_start = dt.datetime.strptime(request_start_date, '%Y-%m-%d')
        datetime_end = dt.datetime.strptime(request_end_date, '%Y-%m-%d')

        str_date_start = str(datetime_start.date())
        str_date_end = str(datetime_end.date())

        self.logger.debug(
            "TMC v3 Logs Advertisers Base: Action 'find'",
            extra={'start_date': str_date_start,
                   'end_date': str_date_end}
        )

        if not request_retry:
            request_retry = {'timeout': 600}

        request_url = \
            self.tune_mat_request_path(
                mat_api_version="v3",
                controller=self.controller,
                action=self.logs_advertisers_type
            )

        if 'response_format' not in request_params:
            request_params['response_format'] = \
                TuneV3LogsAdvertisersResponseFormats.JSON

        request_params["start_date"] += "T00:00:00Z"
        request_params["end_date"] += "T23:59:59Z"

        try:
            response = self.mv_request.request(
                request_method="GET",
                request_url=request_url,
                request_params=request_params,
                request_retry=None,
                request_retry_http_status_codes=None,
                request_retry_func=None,
                request_retry_excps_func=None,
                request_label=request_label
            )

        except TuneRequestBaseError as tmc_req_ex:
            self.logger.error(
                "TMC v3 Logs Advertisers Base: Failed",
                extra=tmc_req_ex.to_dict(),
            )
            raise

        except TuneReportingError as tmc_rep_ex:
            self.logger.error(
                "TMC v3 Logs Advertisers Base: Failed",
                extra=tmc_rep_ex.to_dict(),
            )
            raise

        except Exception as ex:
            print_traceback(ex)

            self.logger.error("TMC v3 Logs Advertisers Base: {}".format(get_exception_message(ex)))
            raise

        json_response = validate_json_response(
            response,
            request_curl=self.mv_request.built_request_curl,
            request_label="TMC v3 Logs Advertisers: Action 'find'",
        )

        data = json_response['data']

        self.data = data
예제 #3
0
    def _export_v2_job_to_queue(
        self,
        export_controller,
        export_action,
        request_params,
        request_retry=None,
        request_label="TMC v2 Job To Queue",
    ):
        """Export Report Request to Job Queue

        Args:
            export_controller:
            export_action:
            request_params:
            request_retry:

        Returns:
            Export Job ID

        """
        request_url = \
            self.tune_mat_request_path(
                mat_api_version="v2",
                controller=export_controller,
                action=export_action
            )

        self.logger.debug(
            ("TMC v2 Advertiser Stats: "
             "Start Advertiser Report Actuals"),
            extra={
                'action': export_action,
                'start_date': request_params["start_date"],
                'end_date': request_params["end_date"]
            })

        try:
            response = self.mv_request.request(
                request_method="GET",
                request_url=request_url,
                request_params=request_params,
                request_label=request_label,
                request_retry_func=self.tune_v2_request_retry_func)

        except TuneRequestBaseError as tmc_req_ex:
            self.logger.error(
                "TMC v2 Advertiser Stats: Request Failed",
                extra=tmc_req_ex.to_dict(),
            )
            raise

        except TuneReportingError as tmc_rep_ex:
            self.logger.error(
                "TMC v2 Advertiser Stats: Reporting Failed",
                extra=tmc_rep_ex.to_dict(),
            )
            raise

        except Exception as ex:
            print_traceback(ex)

            self.logger.error(get_exception_message(ex))

            raise TuneReportingError(
                error_message=(
                    "TMC v2 Advertiser Stats: Unexpected Failed: {}").format(
                        get_exception_message(ex)),
                errors=ex,
                error_code=TuneReportingErrorCodes.REP_ERR_SOFTWARE)

        json_response = validate_json_response(
            response,
            request_curl=self.mv_request.built_request_curl,
            request_label="TMC v2 Advertiser Stats: Action '{}'".format(
                export_action))

        if hasattr(response, 'url'):
            self.logger.info(
                "TMC v2 Advertiser Stats: Reporting API: Export URL",
                extra={'response_url': response.url})

        if (not json_response or json_response['status_code'] != 200
                or 'errors' in json_response):
            raise TuneReportingError(
                error_message=("TMC v2 Advertiser Stats: "
                               "Action '{}': "
                               "Failed to export stats: {}, {}").format(
                                   export_action, json_response['status_code'],
                                   json_response['errors']),
                error_code=TuneReportingErrorCodes.REP_ERR_REQUEST)

        if ('data' not in json_response or not json_response['data']):
            raise TuneReportingError(
                error_message=("TMC v2 Advertiser Stats: "
                               "Action '{}': "
                               "Missing data").format(export_action),
                error_code=TuneReportingErrorCodes.REP_ERR_UNEXPECTED_VALUE)
        json_data = json_response['data']

        export_job_id = None
        if export_action == TuneV2AdvertiserStatsExportActions.EXPORT:
            if ('job_id' not in json_data or not json_data['job_id']):
                raise TuneReportingError(error_message=(
                    "TMC v2 Advertiser Stats: "
                    "Action '{}': "
                    "Response missing 'export_job_id': {}").format(
                        export_action, str(json_data)),
                                         error_code=TuneReportingErrorCodes.
                                         REP_ERR_UNEXPECTED_VALUE)

            export_job_id = json_data['job_id']
        elif export_action == TuneV2AdvertiserStatsExportActions.FIND_EXPORT_QUEUE:
            export_job_id = json_data

        if not export_job_id:
            raise TuneReportingError(
                error_message=(
                    "TMC v2 Advertiser Stats: "
                    "Action '{}': "
                    "Response missing 'job_id'").format(export_action),
                error_code=TuneReportingErrorCodes.REP_ERR_UNEXPECTED_VALUE)

        self.logger.info("TMC v2 Advertiser Stats: Reporting API: Job ID",
                         extra={'job_id': export_job_id})

        return export_job_id
예제 #4
0
    def _find_v2(
        self,
        request_params,
        request_retry=None,
        request_label="TMC v2 Advertiser Stats Find",
    ):
        """Gather data using action find.json.
        """

        self.logger.debug("TuneV2AdvertiserStatsBase",
                          extra={'action': 'find'})

        if "start_date" not in request_params:
            raise ValueError(
                "Missing attribute 'start_date' in parameter 'request_params'")
        request_start_date = request_params["start_date"]

        if "end_date" not in request_params:
            raise ValueError(
                "Missing attribute 'end_date' in parameter 'request_params'")
        request_end_date = request_params["end_date"]

        datetime_start = dt.datetime.strptime(request_start_date, '%Y-%m-%d')
        datetime_end = dt.datetime.strptime(request_end_date, '%Y-%m-%d')

        str_date_start = str(datetime_start.date())
        str_date_end = str(datetime_end.date())

        self.logger.debug(
            ("TuneV2AdvertiserStatsBase"),
            extra={
                'action': 'find',
                'start_date': str_date_start,
                'end_date': str_date_end
            })

        if not request_retry:
            request_retry = {'timeout': 60}

        request_url = \
            self.tune_mat_request_path(
                mat_api_version="v2",
                controller=self.controller,
                action="find"
            )

        if 'format' not in request_params:
            request_params['format'] = TuneV2AdvertiserStatsFormats.JSON

        request_params["start_date"] += " 00:00:00"
        request_params["end_date"] += " 23:59:59"

        try:
            response = self.mv_request.request(request_method="GET",
                                               request_url=request_url,
                                               request_params=request_params,
                                               request_label=request_label)

        except TuneRequestBaseError as tmc_req_ex:
            self.logger.error(
                "TMC v2 Advertiser Stats: Failed",
                extra=tmc_req_ex.to_dict(),
            )
            raise

        except TuneReportingError as tmc_rep_ex:
            self.logger.error(
                "TMC v2 Advertiser Stats: Failed",
                extra=tmc_rep_ex.to_dict(),
            )
            raise

        except Exception as ex:
            print_traceback(ex)

            self.logger.error("TMC v2 Advertiser Stats: {}".format(
                get_exception_message(ex)))
            raise

        json_response = validate_json_response(
            response,
            request_curl=self.mv_request.built_request_curl,
            request_label="TMC v2 Advertiser Stats: Action 'find'")

        if json_response['status_code'] != 200:
            raise TuneReportingError(
                error_message=("TMC v2 Advertiser Stats: "
                               "Action 'find': "
                               "Failed to find stats: {}, {}").format(
                                   json_response['status_code'],
                                   json.dumps(json_response),
                               ),
                error_code=TuneReportingErrorCodes.REP_ERR_REQUEST)

        data = json_response['data']
        self.data = data
    def get_advertiser_id(self, auth_type, auth_value, request_retry=None):
        """Get Advertiser ID

        Args:
            auth_type:
            auth_value:
            request_retry:

        Returns:

        """
        if not auth_type:
            raise ValueError(
                "TMC v2 Advertisers: Get Advertiser ID: Value 'auth_type' not provided."
            )
        if not auth_value:
            raise ValueError(
                "TMC v2 Advertisers: Get Advertiser ID: Value 'auth_value' not provided."
            )

        request_url = \
            self.tune_mat_request_path(
                mat_api_version="v2",
                controller="advertiser",
                action="find"
            )

        request_params = {auth_type: auth_value}

        self.logger.info("TMC v2 Advertisers: Advertiser ID")

        try:
            response = self.mv_request.request(
                request_method="GET",
                request_url=request_url,
                request_params=request_params,
                request_retry=None,
                request_retry_http_status_codes=None,
                request_retry_func=self.tune_v2_request_retry_func,
                request_retry_excps_func=None,
                request_label="TMC v2 Advertisers")

        except TuneRequestBaseError as tmc_req_ex:
            self.logger.error(
                "TMC v2 Advertisers: Advertiser ID: Failed",
                extra=tmc_req_ex.to_dict(),
            )
            raise

        except TuneReportingError as tmc_rep_ex:
            self.logger.error(
                "TMC v2 Advertisers: Advertiser ID: Failed",
                extra=tmc_rep_ex.to_dict(),
            )
            raise

        except Exception as ex:
            print_traceback(ex)

            self.logger.error(
                "TMC v2 Advertisers: Advertiser ID: Failed",
                extra={'error': get_exception_message(ex)},
            )

            raise TuneReportingError(
                error_message=("TMC v2 Advertisers: Failed: {}").format(
                    get_exception_message(ex)),
                errors=ex,
                error_code=TuneReportingErrorCodes.REP_ERR_SOFTWARE)

        json_response = validate_json_response(
            response,
            request_curl=self.mv_request.built_request_curl,
            request_label="TMC v2 Advertisers: Advertiser ID:")

        self.logger.debug("TMC v2 Advertisers: Advertiser ID",
                          extra={'response': json_response})

        json_response_status_code = json_response['status_code']

        http_status_successful = is_http_status_type(
            http_status_code=json_response_status_code,
            http_status_type=HttpStatusType.SUCCESSFUL)

        if not http_status_successful or not json_response['data']:
            raise TuneReportingError(
                error_message="TMC v2 Advertisers: Failed: {}".format(
                    json_response_status_code),
                error_code=TuneReportingErrorCodes.REP_ERR_SOFTWARE)

        if 'data' not in json_response or \
                not json_response['data'] or \
                len(json_response['data']) == 0:
            raise TuneReportingError(
                error_message="TMC v2 Advertisers: Advertiser ID: Failed",
                error_code=TuneReportingErrorCodes.REP_ERR_SOFTWARE)

        advertiser_data = json_response['data'][0]

        self.advertiser_id = advertiser_data.get('id', None)
        if self.advertiser_id is None:
            raise TuneReportingError(
                error_message="TMC v2 Advertisers: Advertiser ID: Failed",
                error_code=TuneReportingErrorCodes.REP_ERR_SOFTWARE)

        self.logger.info("TMC v2 Advertisers: Advertiser ID: {}".format(
            self.advertiser_id))

        return True
    def get_session_token(self, tmc_api_key, request_retry=None):
        """Generate session token is returned to provide
        access to service.

        Args:
            tmc_api_key:
            request_retry:

        Returns:

        """
        self.logger.info("TMC v2 Session Authenticate: Get Token")

        self.api_key = tmc_api_key

        request_url = \
            self.tune_mat_request_path(
                mat_api_version="v2",
                controller="session/authenticate",
                action="api_key"
            )

        request_params = \
            {
                'api_keys': tmc_api_key,
                "source": "multiverse"
            }

        try:
            response = self.mv_request.request(
                request_method="GET",
                request_url=request_url,
                request_params=request_params,
                request_retry=None,
                request_retry_http_status_codes=None,
                request_retry_func=self.tune_v2_request_retry_func,
                request_retry_excps_func=None,
                request_label="TMC v2 Session Authenticate")

        except TuneRequestBaseError as tmc_req_ex:
            self.logger.error(
                "TMC v2 Session Authenticate: Failed",
                extra=tmc_req_ex.to_dict(),
            )
            raise

        except TuneReportingError as tmc_rep_ex:
            self.logger.error(
                "TMC v2 Session Authenticate: Failed",
                extra=tmc_rep_ex.to_dict(),
            )
            raise

        except Exception as ex:
            print_traceback(ex)

            self.logger.error("TMC v2 Session Authenticate: Failed: {}".format(
                get_exception_message(ex)))

            raise TuneReportingError(
                error_message=(
                    "TMC v2 Session Authenticate: Failed: {}").format(
                        get_exception_message(ex)),
                errors=ex,
                error_code=TuneReportingErrorCodes.REP_ERR_SOFTWARE)

        json_response = validate_json_response(
            response,
            request_curl=self.mv_request.built_request_curl,
            request_label="TMC v2 Get Session Token: Validate",
        )

        self.logger.debug(
            "TMC v2 Session Authenticate: Details:",
            extra={'response': json_response},
        )

        response_status_code = json_response.get('status_code', None)
        status_code_successful = is_http_status_type(
            http_status_code=response_status_code,
            http_status_type=HttpStatusType.SUCCESSFUL)

        response_errors = json_response.get('errors', None)

        response_error_message = ""
        if response_errors:
            if isinstance(response_errors, dict):
                error_message = response_errors.get('message', None)
                if error_message:
                    if error_message.startswith("Invalid api key"):
                        response_status_code = TuneReportingErrorCodes.UNAUTHORIZED
                    response_error_message += error_message
            elif isinstance(response_errors, list):
                for response_error in response_errors:
                    if isinstance(response_error, dict) \
                            and 'message' in response_error:
                        error_message = response_error.get('message', None)
                        if error_message:
                            if error_message.startswith("Invalid api key"):
                                response_status_code = TuneReportingErrorCodes.UNAUTHORIZED
                            response_error_message += error_message

        error_code = response_status_code

        if not status_code_successful or not json_response['data']:
            raise TuneReportingError(
                error_message="TMC v2 Session Authenticate: Failed: {}".format(
                    response_error_message),
                error_code=error_code,
            )

        self.session_token = json_response['data']

        self.logger.info("TMC v2 Session Authenticate",
                         extra={'session_token': self.session_token})
        self.logger.info("TMC v2 Session Authenticate: Finished")

        return True