def handle_request()

in src/lambdas/r8s_report_generator/handler.py [0:0]


    def handle_request(self, event, context):
        customer_name = event.get(CUSTOMER_ATTR)
        tenants = event.get(TENANTS_ATTR)

        customer = self.customer_service.get(name=customer_name)

        if not customer:
            _LOG.error(f'Customer \'{customer_name}\' does not exist.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Customer \'{customer_name}\' does not exist.'
            )
        if not tenants:
            _LOG.debug(f'\'{TENANTS_ATTR}\' attribute must be specified.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'\'{TENANTS_ATTR}\' attribute must be specified.'
            )
        response = {}
        for tenant_name in tenants:
            try:
                tenant = self.tenant_service.get(tenant_name=tenant_name)
                if not tenant:
                    _LOG.error(f'Tenant \'{tenant_name}\' does not exist.')
                    return build_response(
                        code=RESPONSE_BAD_REQUEST_CODE,
                        content=f'Tenant \'{tenant_name}\' does not exist.'
                    )

                processing_days = self.environment_service. \
                    mail_report_process_days()

                priority_saving_threshold = self.environment_service. \
                    mail_report_high_priority_threshold()
                _LOG.debug(f'Generating report for \'{customer_name}\' tenant '
                           f'\'{tenant_name}\'. Processing days: '
                           f'{processing_days}, high priority saving '
                           f'threshold: {priority_saving_threshold}')
                report = self.generate_report(
                    customer=customer, tenant=tenant,
                    processing_days=processing_days,
                    priority_saving_threshold= \
                        priority_saving_threshold)

                _LOG.debug(f'Preparing request for sending to maestro')
                formatted_report = self.prepare_request(report=report)

                _LOG.debug(f'Formatted report: {formatted_report}')
                _LOG.debug(f'Sending request to Maestro')

                response_code, response_message = \
                    self._send_notification_to_m3(json_model=formatted_report)
                _LOG.debug(f'Response: {response_message}')
                response[tenant_name] = response_message
            except Exception as e:
                message = f'Exception occurred while sending report for ' \
                          f'tenant: \'{tenant_name}\': {e}'
                _LOG.error(message)
                response[tenant_name] = message

        return build_response(
            code=RESPONSE_OK_CODE,
            content=response
        )