示例#1
0
def _get_database():
    try:
        engine = _get_engine()
        logger.info("Connected to database!")
    except IOError:
        logger.exception("Failed to get database connection!")
        raise IOError

    return engine
示例#2
0
    def _get_eligible_users(self, url):

        try:
            response = requests.get(url, headers=self.headers)
            return response.json()
        except requests.exceptions.HTTPError as error:
            logger.exception(
                "Unable to communicate to auth service to fetch eligible users",
                error=error)
            raise error
示例#3
0
 def _get_user_first_name(self, email):
     type(email)
     url = f"{self.party_url}/party-api/v1/respondents/email"
     request_json = {"email": email}
     try:
         response = requests.get(url, json=request_json, auth=self.basic_auth)
         response.raise_for_status()
     except requests.exceptions.HTTPError as error:
         logger.exception("Unable to send request to party service. Can't proceed with notification.", error=error)
         raise error
     return response.json()["firstName"]
示例#4
0
def get_connection():
    """
    Get DB connection
    """
    try:
        con = _get_database().raw_connection()
        con.cursor().execute("SET SCHEMA '{}'".format(
            cfg.Config.DATABASE_SCHEMA))
    except Exception as e:
        logger.exception(e)
        raise AuthDueDeletionSchedulerError(
            "Unable to establish database connection.")

    return con
示例#5
0
 def _update_user(self, user, scheduler_column):
     logger.info('Updating user data with notification sent date',
                 notification=scheduler_column)
     form_data = {scheduler_column: datetime.utcnow()}
     try:
         requests.patch(f'{self.patch_url}/{user}',
                        data=form_data,
                        headers=self.headers)
     except requests.exceptions.HTTPError as error:
         logger.exception("Unable to update user notification date",
                          username=obfuscate_email(user),
                          error=error)
         raise error
     logger.info('user data with notification sent date updated',
                 notification=scheduler_column)
示例#6
0
    def _send_message(self, email, template_id, personalisation):
        """
        Send message to gov.uk notify via pubsub topic
        :param email: str email address of recipient
        :param template_id: the template id on gov.uk notify to be used
        """
        if not cfg.Config.SEND_EMAIL_TO_GOV_NOTIFY:
            logger.info("Notification not sent. Notify is disabled.")
            return

        notification = {
            'notify': {
                'email_address': email,
                'template_id': template_id,
                'personalisation': {}
            }
        }
        if personalisation:
            notification['notify']['personalisation'] = personalisation

        notification_str = json.dumps(notification)
        if self.publisher is None:
            self.publisher = pubsub_v1.PublisherClient()
        topic_path = self.publisher.topic_path(self.project_id, self.topic_id)
        logger.info('Publishing notification message to pub-sub topic', pubsub_topic=self.topic_id)
        future = self.publisher.publish(topic_path, data=notification_str.encode())
        # It's okay for us to catch a broad Exception here because the documentation for future.result() says it
        # throws either a TimeoutError or an Exception.
        try:
            msg_id = future.result()
            logger.info('Notification message published to pub-sub.', msg_id=msg_id, pubsub_topic=self.topic_id)
        except TimeoutError as e:
            logger.exception(e)
            raise NotifyError('There was a problem sending a notification via pub-sub topic to GOV.UK Notify.'
                              'Communication to pub-sub topic timed-out',
                              pubsub_topic=self.topic_id, error=e)
        except Exception as e:
            logger.exception(e)
            raise NotifyError('There was a problem sending a notification via pub-sub topic to GOV.UK Notify. '
                              'Communication to pub-sub topic raised an exception.', pubsub_topic=self.topic_id,
                              error=e)