mirror of
https://github.com/GSA/notifications-api.git
synced 2026-06-08 15:28:41 -04:00
Fix conflict
This commit is contained in:
@@ -70,6 +70,7 @@ def create_app(app_name=None):
|
||||
from app.provider_details.rest import provider_details as provider_details_blueprint
|
||||
from app.spec.rest import spec as spec_blueprint
|
||||
from app.organisation.rest import organisation_blueprint
|
||||
from app.delivery.rest import delivery_blueprint
|
||||
|
||||
application.register_blueprint(service_blueprint, url_prefix='/service')
|
||||
application.register_blueprint(user_blueprint, url_prefix='/user')
|
||||
@@ -78,6 +79,7 @@ def create_app(app_name=None):
|
||||
application.register_blueprint(notifications_blueprint)
|
||||
application.register_blueprint(job_blueprint)
|
||||
application.register_blueprint(invite_blueprint)
|
||||
application.register_blueprint(delivery_blueprint)
|
||||
application.register_blueprint(accept_invite, url_prefix='/invite')
|
||||
|
||||
application.register_blueprint(template_statistics_blueprint)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from flask import request, jsonify, _request_ctx_stack, current_app
|
||||
from flask import request, _request_ctx_stack, current_app
|
||||
from sqlalchemy.exc import DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from notifications_python_client.authentication import decode_jwt_token, get_token_issuer
|
||||
@@ -37,8 +38,10 @@ def requires_auth():
|
||||
if client == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
return handle_admin_key(auth_token, current_app.config.get('ADMIN_CLIENT_SECRET'))
|
||||
|
||||
api_keys = get_model_api_keys(client)
|
||||
|
||||
try:
|
||||
api_keys = get_model_api_keys(client)
|
||||
except DataError:
|
||||
raise AuthError("Invalid token: service id is not the right data type", 403)
|
||||
for api_key in api_keys:
|
||||
try:
|
||||
get_decode_errors(auth_token, api_key.unsigned_secret)
|
||||
@@ -59,19 +62,19 @@ def requires_auth():
|
||||
if not api_keys:
|
||||
raise AuthError("Invalid token: service has no API keys", 403)
|
||||
else:
|
||||
raise AuthError("Invalid token: signature", 403)
|
||||
raise AuthError("Invalid token: signature, api token is not valid", 403)
|
||||
|
||||
|
||||
def handle_admin_key(auth_token, secret):
|
||||
try:
|
||||
get_decode_errors(auth_token, secret)
|
||||
return
|
||||
except TokenDecodeError as e:
|
||||
except TokenDecodeError:
|
||||
raise AuthError("Invalid token: signature", 403)
|
||||
|
||||
|
||||
def get_decode_errors(auth_token, unsigned_secret):
|
||||
try:
|
||||
decode_jwt_token(auth_token, unsigned_secret)
|
||||
except TokenExpiredError as e:
|
||||
except TokenExpiredError:
|
||||
raise AuthError("Invalid token: expired", 403)
|
||||
|
||||
@@ -1,31 +1,17 @@
|
||||
from datetime import datetime
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.recipients import (
|
||||
validate_and_format_phone_number
|
||||
)
|
||||
from notifications_utils.template import Template, get_sms_fragment_count
|
||||
from notifications_utils.renderers import HTMLEmail, PlainTextEmail, SMSMessage
|
||||
|
||||
from app import notify_celery, statsd_client, clients, create_uuid
|
||||
from app.clients.email import EmailClientException
|
||||
from app.clients.sms import SmsClientException
|
||||
from app.dao.notifications_dao import (
|
||||
get_notification_by_id,
|
||||
dao_update_notification,
|
||||
update_notification_status_by_id
|
||||
)
|
||||
from app.dao.provider_details_dao import get_provider_details_by_notification_type
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
|
||||
from app.models import SMS_TYPE, EMAIL_TYPE, KEY_TYPE_TEST, BRANDING_ORG
|
||||
from app import notify_celery
|
||||
from app.dao import notifications_dao
|
||||
from app.dao.notifications_dao import update_notification_status_by_id
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
from app.delivery import send_to_providers
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
|
||||
def retry_iteration_to_delay(retry=0):
|
||||
"""
|
||||
:param retry times we have performed a retry
|
||||
Given current retry calculate some delay before retrying
|
||||
0: 10 seconds
|
||||
1: 60 seconds (1 minutes)
|
||||
@@ -47,153 +33,93 @@ def retry_iteration_to_delay(retry=0):
|
||||
return delays.get(retry, 10)
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="deliver_sms", max_retries=5, default_retry_delay=5)
|
||||
@statsd(namespace="tasks")
|
||||
def deliver_sms(self, notification_id):
|
||||
try:
|
||||
notification = notifications_dao.get_notification_by_id(notification_id)
|
||||
if not notification:
|
||||
raise NoResultFound()
|
||||
send_to_providers.send_sms_to_provider(notification)
|
||||
except Exception as e:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
"RETRY: SMS notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification_id),
|
||||
e
|
||||
)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="deliver_email", max_retries=5, default_retry_delay=5)
|
||||
@statsd(namespace="tasks")
|
||||
def deliver_email(self, notification_id):
|
||||
try:
|
||||
notification = notifications_dao.get_notification_by_id(notification_id)
|
||||
if not notification:
|
||||
raise NoResultFound()
|
||||
send_to_providers.send_email_to_provider(notification)
|
||||
except Exception as e:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
"RETRY: Email notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification_id),
|
||||
e
|
||||
)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="send-sms-to-provider", max_retries=5, default_retry_delay=5)
|
||||
@statsd(namespace="tasks")
|
||||
def send_sms_to_provider(self, service_id, notification_id):
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
provider = provider_to_use(SMS_TYPE, notification_id)
|
||||
notification = get_notification_by_id(notification_id)
|
||||
if notification.status == 'created':
|
||||
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
|
||||
template = Template(
|
||||
template_model.__dict__,
|
||||
values={} if not notification.personalisation else notification.personalisation,
|
||||
renderer=SMSMessage(prefix=service.name, sender=service.sms_sender)
|
||||
)
|
||||
try:
|
||||
notification = notifications_dao.get_notification_by_id(notification_id)
|
||||
if not notification:
|
||||
raise NoResultFound()
|
||||
send_to_providers.send_sms_to_provider(notification)
|
||||
except Exception as e:
|
||||
try:
|
||||
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
||||
send_sms_response.apply_async(
|
||||
(provider.get_name(), str(notification_id), notification.to), queue='research-mode'
|
||||
)
|
||||
notification.billable_units = 0
|
||||
else:
|
||||
provider.send_sms(
|
||||
to=validate_and_format_phone_number(notification.to),
|
||||
content=template.replaced,
|
||||
reference=str(notification_id),
|
||||
sender=service.sms_sender
|
||||
)
|
||||
notification.billable_units = get_sms_fragment_count(template.replaced_content_count)
|
||||
|
||||
notification.sent_at = datetime.utcnow()
|
||||
notification.sent_by = provider.get_name()
|
||||
notification.status = 'sending'
|
||||
dao_update_notification(notification)
|
||||
except SmsClientException as e:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
"RETRY: SMS notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification.id),
|
||||
e
|
||||
)
|
||||
update_notification_status_by_id(notification.id, 'technical-failure')
|
||||
|
||||
current_app.logger.info(
|
||||
"SMS {} sent to provider at {}".format(notification_id, notification.sent_at)
|
||||
)
|
||||
delta_milliseconds = (datetime.utcnow() - notification.created_at).total_seconds() * 1000
|
||||
statsd_client.timing("sms.total-time", delta_milliseconds)
|
||||
|
||||
|
||||
def provider_to_use(notification_type, notification_id):
|
||||
active_providers_in_order = [
|
||||
provider for provider in get_provider_details_by_notification_type(notification_type) if provider.active
|
||||
]
|
||||
|
||||
if not active_providers_in_order:
|
||||
current_app.logger.error(
|
||||
"{} {} failed as no active providers".format(notification_type, notification_id)
|
||||
)
|
||||
raise Exception("No active {} providers".format(notification_type))
|
||||
|
||||
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
|
||||
current_app.logger.error(
|
||||
"RETRY: SMS notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification_id),
|
||||
e
|
||||
)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="send-email-to-provider", max_retries=5, default_retry_delay=5)
|
||||
@statsd(namespace="tasks")
|
||||
def send_email_to_provider(self, service_id, notification_id):
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
provider = provider_to_use(EMAIL_TYPE, notification_id)
|
||||
notification = get_notification_by_id(notification_id)
|
||||
if notification.status == 'created':
|
||||
try:
|
||||
notification = notifications_dao.get_notification_by_id(notification_id)
|
||||
if not notification:
|
||||
raise NoResultFound()
|
||||
send_to_providers.send_email_to_provider(notification)
|
||||
except Exception as e:
|
||||
try:
|
||||
template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__
|
||||
|
||||
html_email = Template(
|
||||
template_dict,
|
||||
values=notification.personalisation,
|
||||
renderer=get_html_email_renderer(service)
|
||||
current_app.logger.error(
|
||||
"RETRY: Email notification {} failed".format(notification_id)
|
||||
)
|
||||
|
||||
plain_text_email = Template(
|
||||
template_dict,
|
||||
values=notification.personalisation,
|
||||
renderer=PlainTextEmail()
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification_id),
|
||||
e
|
||||
)
|
||||
|
||||
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
||||
reference = str(create_uuid())
|
||||
send_email_response.apply_async(
|
||||
(provider.get_name(), reference, notification.to), queue='research-mode'
|
||||
)
|
||||
notification.billable_units = 0
|
||||
else:
|
||||
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from,
|
||||
current_app.config['NOTIFY_EMAIL_DOMAIN'])
|
||||
reference = provider.send_email(
|
||||
from_address,
|
||||
notification.to,
|
||||
plain_text_email.replaced_subject,
|
||||
body=plain_text_email.replaced,
|
||||
html_body=html_email.replaced,
|
||||
reply_to_address=service.reply_to_email_address,
|
||||
)
|
||||
|
||||
notification.reference = reference
|
||||
notification.sent_at = datetime.utcnow()
|
||||
notification.sent_by = provider.get_name(),
|
||||
notification.status = 'sending'
|
||||
dao_update_notification(notification)
|
||||
except EmailClientException as e:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
"RETRY: Email notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries))
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification.id),
|
||||
e
|
||||
)
|
||||
update_notification_status_by_id(notification.id, 'technical-failure')
|
||||
|
||||
current_app.logger.info(
|
||||
"Email {} sent to provider at {}".format(notification_id, notification.sent_at)
|
||||
)
|
||||
delta_milliseconds = (datetime.utcnow() - notification.created_at).total_seconds() * 1000
|
||||
statsd_client.timing("email.total-time", delta_milliseconds)
|
||||
|
||||
|
||||
def get_html_email_renderer(service):
|
||||
govuk_banner = service.branding != BRANDING_ORG
|
||||
if service.organisation:
|
||||
logo = '{}{}{}'.format(
|
||||
current_app.config['ADMIN_BASE_URL'],
|
||||
current_app.config['BRANDING_PATH'],
|
||||
service.organisation.logo
|
||||
)
|
||||
branding = {
|
||||
'brand_colour': service.organisation.colour,
|
||||
'brand_logo': logo,
|
||||
'brand_name': service.organisation.name,
|
||||
}
|
||||
else:
|
||||
branding = {}
|
||||
|
||||
return HTMLEmail(govuk_banner=govuk_banner, **branding)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
from app.clients import (Client, ClientException)
|
||||
|
||||
|
||||
class SmsClientException(ClientException):
|
||||
class SmsClientResponseException(ClientException):
|
||||
'''
|
||||
Base Exception for SmsClients
|
||||
Base Exception for SmsClientsResponses
|
||||
'''
|
||||
pass
|
||||
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return "Message {}".format(self.message)
|
||||
|
||||
|
||||
class SmsClient(Client):
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
from monotonic import monotonic
|
||||
from requests import request, RequestException, HTTPError
|
||||
from requests import request, RequestException
|
||||
|
||||
from app.clients.sms import (
|
||||
SmsClient,
|
||||
SmsClientException
|
||||
)
|
||||
from app.clients.sms import (SmsClient, SmsClientResponseException)
|
||||
from app.clients import STATISTICS_DELIVERED, STATISTICS_FAILURE
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -42,13 +40,14 @@ def get_firetext_responses(status):
|
||||
return firetext_responses[status]
|
||||
|
||||
|
||||
class FiretextClientException(SmsClientException):
|
||||
def __init__(self, response):
|
||||
self.code = response['code']
|
||||
self.description = response['description']
|
||||
class FiretextClientResponseException(SmsClientResponseException):
|
||||
def __init__(self, response, exception):
|
||||
self.status_code = response.status_code
|
||||
self.text = response.text
|
||||
self.exception = exception
|
||||
|
||||
def __str__(self):
|
||||
return "Code {} description {}".format(self.code, self.description)
|
||||
return "Code {} text {} exception {}".format(self.status_code, self.text, str(self.exception))
|
||||
|
||||
|
||||
class FiretextClient(SmsClient):
|
||||
@@ -62,11 +61,27 @@ class FiretextClient(SmsClient):
|
||||
self.api_key = current_app.config.get('FIRETEXT_API_KEY')
|
||||
self.from_number = current_app.config.get('FROM_NUMBER')
|
||||
self.name = 'firetext'
|
||||
self.url = "https://www.firetext.co.uk/api/sendsms/json"
|
||||
self.statsd_client = statsd_client
|
||||
|
||||
def get_name(self):
|
||||
return self.name
|
||||
|
||||
def record_outcome(self, success, response):
|
||||
log_message = "API {} request {} on {} response status_code {}".format(
|
||||
"POST",
|
||||
"succeeded" if success else "failed",
|
||||
self.url,
|
||||
response.status_code
|
||||
)
|
||||
|
||||
if success:
|
||||
self.current_app.logger.info(log_message)
|
||||
self.statsd_client.incr("clients.firetext.success")
|
||||
else:
|
||||
self.statsd_client.incr("clients.firetext.error")
|
||||
self.current_app.logger.error(log_message)
|
||||
|
||||
def send_sms(self, to, content, reference, sender=None):
|
||||
|
||||
data = {
|
||||
@@ -81,36 +96,23 @@ class FiretextClient(SmsClient):
|
||||
try:
|
||||
response = request(
|
||||
"POST",
|
||||
"https://www.firetext.co.uk/api/sendsms/json",
|
||||
self.url,
|
||||
data=data
|
||||
)
|
||||
firetext_response = response.json()
|
||||
if firetext_response['code'] != 0:
|
||||
raise FiretextClientException(firetext_response)
|
||||
response.raise_for_status()
|
||||
self.current_app.logger.info(
|
||||
"API {} request on {} succeeded with {} '{}'".format(
|
||||
"POST",
|
||||
"https://www.firetext.co.uk/api/sendsms",
|
||||
response.status_code,
|
||||
firetext_response.items()
|
||||
)
|
||||
)
|
||||
try:
|
||||
json.loads(response.text)
|
||||
if response.json()['code'] != 0:
|
||||
raise ValueError()
|
||||
except (ValueError, AttributeError) as e:
|
||||
self.record_outcome(False, response)
|
||||
raise FiretextClientResponseException(response=response, exception=e)
|
||||
self.record_outcome(True, response)
|
||||
except RequestException as e:
|
||||
api_error = HTTPError.create(e)
|
||||
logger.error(
|
||||
"API {} request on {} failed with {} '{}'".format(
|
||||
"POST",
|
||||
"https://www.firetext.co.uk/api/sendsms",
|
||||
api_error.status_code,
|
||||
api_error.message
|
||||
)
|
||||
)
|
||||
self.statsd_client.incr("clients.firetext.error")
|
||||
raise api_error
|
||||
self.record_outcome(False, e.response)
|
||||
raise FiretextClientResponseException(response=e.response, exception=e)
|
||||
finally:
|
||||
elapsed_time = monotonic() - start_time
|
||||
self.current_app.logger.info("Firetext request finished in {}".format(elapsed_time))
|
||||
self.statsd_client.incr("clients.firetext.success")
|
||||
self.statsd_client.timing("clients.firetext.request-time", elapsed_time)
|
||||
return response
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import logging
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.clients.sms.firetext import (
|
||||
FiretextClient
|
||||
)
|
||||
@@ -13,7 +16,9 @@ class LoadtestingClient(FiretextClient):
|
||||
|
||||
def init_app(self, config, statsd_client, *args, **kwargs):
|
||||
super(FiretextClient, self).__init__(*args, **kwargs)
|
||||
self.current_app = current_app
|
||||
self.api_key = config.config.get('LOADTESTING_API_KEY')
|
||||
self.from_number = config.config.get('LOADTESTING_NUMBER')
|
||||
self.from_number = config.config.get('FROM_NUMBER')
|
||||
self.name = 'loadtesting'
|
||||
self.url = "https://www.firetext.co.uk/api/sendsms/json"
|
||||
self.statsd_client = statsd_client
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import json
|
||||
from monotonic import monotonic
|
||||
from requests import (request, RequestException, HTTPError)
|
||||
from requests import (request, RequestException)
|
||||
|
||||
from app.clients import (STATISTICS_DELIVERED, STATISTICS_FAILURE)
|
||||
from app.clients.sms import (SmsClient, SmsClientException)
|
||||
from app.clients.sms import (SmsClient, SmsClientResponseException)
|
||||
|
||||
mmg_response_map = {
|
||||
'2': {
|
||||
@@ -42,13 +43,14 @@ def get_mmg_responses(status):
|
||||
return mmg_response_map.get(status, mmg_response_map.get('default'))
|
||||
|
||||
|
||||
class MMGClientException(SmsClientException):
|
||||
def __init__(self, error_response):
|
||||
self.code = error_response['Error']
|
||||
self.description = error_response['Description']
|
||||
class MMGClientResponseException(SmsClientResponseException):
|
||||
def __init__(self, response, exception):
|
||||
self.status_code = response.status_code
|
||||
self.text = response.text
|
||||
self.exception = exception
|
||||
|
||||
def __str__(self):
|
||||
return "Code {} description {}".format(self.code, self.description)
|
||||
return "Code {} text {} exception {}".format(self.status_code, self.text, str(self.exception))
|
||||
|
||||
|
||||
class MMGClient(SmsClient):
|
||||
@@ -65,6 +67,21 @@ class MMGClient(SmsClient):
|
||||
self.statsd_client = statsd_client
|
||||
self.mmg_url = current_app.config.get('MMG_URL')
|
||||
|
||||
def record_outcome(self, success, response):
|
||||
log_message = "API {} request {} on {} response status_code {}".format(
|
||||
"POST",
|
||||
"succeeded" if success else "failed",
|
||||
self.mmg_url,
|
||||
response.status_code
|
||||
)
|
||||
|
||||
if success:
|
||||
self.current_app.logger.info(log_message)
|
||||
self.statsd_client.incr("clients.mmg.success")
|
||||
else:
|
||||
self.statsd_client.incr("clients.mmg.error")
|
||||
self.current_app.logger.error(log_message)
|
||||
|
||||
def get_name(self):
|
||||
return self.name
|
||||
|
||||
@@ -79,38 +96,30 @@ class MMGClient(SmsClient):
|
||||
}
|
||||
|
||||
start_time = monotonic()
|
||||
|
||||
try:
|
||||
response = request("POST", self.mmg_url,
|
||||
data=json.dumps(data),
|
||||
headers={'Content-Type': 'application/json',
|
||||
'Authorization': 'Basic {}'.format(self.api_key)})
|
||||
if response.status_code != 200:
|
||||
raise MMGClientException(response.json())
|
||||
response = request(
|
||||
"POST",
|
||||
self.mmg_url,
|
||||
data=json.dumps(data),
|
||||
headers={
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Basic {}'.format(self.api_key)
|
||||
}
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
self.current_app.logger.info(
|
||||
"API {} request on {} succeeded with {} '{}'".format(
|
||||
"POST",
|
||||
self.mmg_url,
|
||||
response.status_code,
|
||||
response.json().items()
|
||||
)
|
||||
)
|
||||
try:
|
||||
json.loads(response.text)
|
||||
except (ValueError, AttributeError) as e:
|
||||
self.record_outcome(False, response)
|
||||
raise MMGClientResponseException(response=response, exception=e)
|
||||
self.record_outcome(True, response)
|
||||
except RequestException as e:
|
||||
api_error = HTTPError.create(e)
|
||||
self.current_app.logger.error(
|
||||
"API {} request on {} failed with {} '{}'".format(
|
||||
"POST",
|
||||
self.mmg_url,
|
||||
api_error.status_code,
|
||||
api_error.message
|
||||
)
|
||||
)
|
||||
self.statsd_client.incr("clients.mmg.error")
|
||||
raise api_error
|
||||
self.record_outcome(False, e.response)
|
||||
raise MMGClientResponseException(response=e.response, exception=e)
|
||||
finally:
|
||||
elapsed_time = monotonic() - start_time
|
||||
self.statsd_client.timing("clients.mmg.request-time", elapsed_time)
|
||||
self.statsd_client.incr("clients.mmg.success")
|
||||
self.current_app.logger.info("MMG request finished in {}".format(elapsed_time))
|
||||
|
||||
return response
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
from datetime import date, timedelta, datetime
|
||||
from sqlalchemy import desc, asc, cast, Date as sql_date
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import func, desc, asc, cast, Date as sql_date
|
||||
|
||||
from app import db
|
||||
from app.dao import days_ago
|
||||
from app.models import Job, NotificationHistory, JOB_STATUS_SCHEDULED
|
||||
from app.statsd_decorators import statsd
|
||||
from sqlalchemy import func, asc
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@@ -26,11 +27,18 @@ def dao_get_job_by_service_id_and_job_id(service_id, job_id):
|
||||
return Job.query.filter_by(service_id=service_id, id=job_id).one()
|
||||
|
||||
|
||||
def dao_get_jobs_by_service_id(service_id, limit_days=None):
|
||||
def dao_get_jobs_by_service_id(service_id, limit_days=None, page=1, page_size=50, statuses=None):
|
||||
query_filter = [Job.service_id == service_id]
|
||||
if limit_days is not None:
|
||||
query_filter.append(cast(Job.created_at, sql_date) >= days_ago(limit_days))
|
||||
return Job.query.filter(*query_filter).order_by(desc(Job.created_at)).all()
|
||||
if statuses is not None and statuses != ['']:
|
||||
query_filter.append(
|
||||
Job.job_status.in_(statuses)
|
||||
)
|
||||
return Job.query \
|
||||
.filter(*query_filter) \
|
||||
.order_by(desc(Job.created_at)) \
|
||||
.paginate(page=page, per_page=page_size)
|
||||
|
||||
|
||||
def dao_get_job_by_id(job_id):
|
||||
|
||||
@@ -228,14 +228,17 @@ def get_notifications(filter_dict=None):
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def get_notifications_for_service(service_id,
|
||||
filter_dict=None,
|
||||
page=1,
|
||||
page_size=None,
|
||||
limit_days=None,
|
||||
key_type=None,
|
||||
personalisation=False,
|
||||
include_jobs=False):
|
||||
def get_notifications_for_service(
|
||||
service_id,
|
||||
filter_dict=None,
|
||||
page=1,
|
||||
page_size=None,
|
||||
limit_days=None,
|
||||
key_type=None,
|
||||
personalisation=False,
|
||||
include_jobs=False,
|
||||
include_from_test_key=False
|
||||
):
|
||||
if page_size is None:
|
||||
page_size = current_app.config['PAGE_SIZE']
|
||||
|
||||
@@ -250,7 +253,7 @@ def get_notifications_for_service(service_id,
|
||||
|
||||
if key_type is not None:
|
||||
filters.append(Notification.key_type == key_type)
|
||||
else:
|
||||
elif not include_from_test_key:
|
||||
filters.append(Notification.key_type != KEY_TYPE_TEST)
|
||||
|
||||
query = Notification.query.filter(*filters)
|
||||
|
||||
0
app/delivery/__init__.py
Normal file
0
app/delivery/__init__.py
Normal file
46
app/delivery/rest.py
Normal file
46
app/delivery/rest.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from flask import Blueprint, jsonify
|
||||
|
||||
from app.delivery import send_to_providers
|
||||
from app.models import EMAIL_TYPE
|
||||
from app.celery import provider_tasks
|
||||
from app.dao import notifications_dao
|
||||
from flask import current_app
|
||||
|
||||
delivery_blueprint = Blueprint('delivery', __name__)
|
||||
|
||||
from app.errors import register_errors
|
||||
|
||||
register_errors(delivery_blueprint)
|
||||
|
||||
|
||||
@delivery_blueprint.route('/deliver/notification/<uuid:notification_id>', methods=['POST'])
|
||||
def send_notification_to_provider(notification_id):
|
||||
notification = notifications_dao.get_notification_by_id(notification_id)
|
||||
if not notification:
|
||||
return jsonify({"result": "error", "message": "No result found"}), 404
|
||||
|
||||
if notification.notification_type == EMAIL_TYPE:
|
||||
send_response(
|
||||
send_to_providers.send_email_to_provider,
|
||||
provider_tasks.deliver_email,
|
||||
notification,
|
||||
'send-email')
|
||||
else:
|
||||
send_response(
|
||||
send_to_providers.send_sms_to_provider,
|
||||
provider_tasks.deliver_sms,
|
||||
notification,
|
||||
'send-sms')
|
||||
return jsonify({}), 204
|
||||
|
||||
|
||||
def send_response(send_call, task_call, notification, queue):
|
||||
try:
|
||||
send_call(notification)
|
||||
except Exception as e:
|
||||
current_app.logger.exception(
|
||||
"Failed to send notification, retrying in celery. ID {} type {}".format(
|
||||
notification.id,
|
||||
notification.notification_type),
|
||||
e)
|
||||
task_call.apply_async((str(notification.id)), queue=queue)
|
||||
135
app/delivery/send_to_providers.py
Normal file
135
app/delivery/send_to_providers.py
Normal file
@@ -0,0 +1,135 @@
|
||||
from datetime import datetime
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.recipients import (
|
||||
validate_and_format_phone_number
|
||||
)
|
||||
from notifications_utils.template import Template, get_sms_fragment_count
|
||||
from notifications_utils.renderers import HTMLEmail, PlainTextEmail, SMSMessage
|
||||
|
||||
from app import clients, statsd_client, create_uuid
|
||||
from app.dao.notifications_dao import dao_update_notification
|
||||
from app.dao.provider_details_dao import get_provider_details_by_notification_type
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
|
||||
from app.models import SMS_TYPE, KEY_TYPE_TEST, BRANDING_ORG, EMAIL_TYPE
|
||||
|
||||
|
||||
def send_sms_to_provider(notification):
|
||||
service = dao_fetch_service_by_id(notification.service_id)
|
||||
provider = provider_to_use(SMS_TYPE, notification.id)
|
||||
if notification.status == 'created':
|
||||
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
|
||||
template = Template(
|
||||
template_model.__dict__,
|
||||
values={} if not notification.personalisation else notification.personalisation,
|
||||
renderer=SMSMessage(prefix=service.name, sender=service.sms_sender)
|
||||
)
|
||||
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
||||
send_sms_response.apply_async(
|
||||
(provider.get_name(), str(notification.id), notification.to), queue='research-mode'
|
||||
)
|
||||
notification.billable_units = 0
|
||||
else:
|
||||
provider.send_sms(
|
||||
to=validate_and_format_phone_number(notification.to),
|
||||
content=template.replaced,
|
||||
reference=str(notification.id),
|
||||
sender=service.sms_sender
|
||||
)
|
||||
notification.billable_units = get_sms_fragment_count(template.replaced_content_count)
|
||||
|
||||
notification.sent_at = datetime.utcnow()
|
||||
notification.sent_by = provider.get_name()
|
||||
notification.status = 'sending'
|
||||
dao_update_notification(notification)
|
||||
|
||||
current_app.logger.info(
|
||||
"SMS {} sent to provider at {}".format(notification.id, notification.sent_at)
|
||||
)
|
||||
delta_milliseconds = (datetime.utcnow() - notification.created_at).total_seconds() * 1000
|
||||
statsd_client.timing("sms.total-time", delta_milliseconds)
|
||||
|
||||
|
||||
def send_email_to_provider(notification):
|
||||
service = dao_fetch_service_by_id(notification.service_id)
|
||||
provider = provider_to_use(EMAIL_TYPE, notification.id)
|
||||
if notification.status == 'created':
|
||||
template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__
|
||||
|
||||
html_email = Template(
|
||||
template_dict,
|
||||
values=notification.personalisation,
|
||||
renderer=get_html_email_renderer(service)
|
||||
)
|
||||
|
||||
plain_text_email = Template(
|
||||
template_dict,
|
||||
values=notification.personalisation,
|
||||
renderer=PlainTextEmail()
|
||||
)
|
||||
|
||||
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
|
||||
reference = str(create_uuid())
|
||||
send_email_response.apply_async(
|
||||
(provider.get_name(), reference, notification.to), queue='research-mode'
|
||||
)
|
||||
notification.billable_units = 0
|
||||
else:
|
||||
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from,
|
||||
current_app.config['NOTIFY_EMAIL_DOMAIN'])
|
||||
reference = provider.send_email(
|
||||
from_address,
|
||||
notification.to,
|
||||
plain_text_email.replaced_subject,
|
||||
body=plain_text_email.replaced,
|
||||
html_body=html_email.replaced,
|
||||
reply_to_address=service.reply_to_email_address,
|
||||
)
|
||||
|
||||
notification.reference = reference
|
||||
notification.sent_at = datetime.utcnow()
|
||||
notification.sent_by = provider.get_name(),
|
||||
notification.status = 'sending'
|
||||
dao_update_notification(notification)
|
||||
|
||||
current_app.logger.info(
|
||||
"Email {} sent to provider at {}".format(notification.id, notification.sent_at)
|
||||
)
|
||||
delta_milliseconds = (datetime.utcnow() - notification.created_at).total_seconds() * 1000
|
||||
statsd_client.timing("email.total-time", delta_milliseconds)
|
||||
|
||||
|
||||
def provider_to_use(notification_type, notification_id):
|
||||
active_providers_in_order = [
|
||||
provider for provider in get_provider_details_by_notification_type(notification_type) if provider.active
|
||||
]
|
||||
|
||||
if not active_providers_in_order:
|
||||
current_app.logger.error(
|
||||
"{} {} failed as no active providers".format(notification_type, notification_id)
|
||||
)
|
||||
raise Exception("No active {} providers".format(notification_type))
|
||||
|
||||
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
|
||||
|
||||
|
||||
def get_html_email_renderer(service):
|
||||
govuk_banner = service.branding != BRANDING_ORG
|
||||
if service.organisation:
|
||||
logo = '{}{}{}'.format(
|
||||
current_app.config['ADMIN_BASE_URL'],
|
||||
current_app.config['BRANDING_PATH'],
|
||||
service.organisation.logo
|
||||
)
|
||||
branding = {
|
||||
'brand_colour': service.organisation.colour,
|
||||
'brand_logo': logo,
|
||||
'brand_name': service.organisation.name,
|
||||
}
|
||||
else:
|
||||
branding = {}
|
||||
|
||||
return HTMLEmail(govuk_banner=govuk_banner, **branding)
|
||||
@@ -96,20 +96,16 @@ def get_jobs_by_service(service_id):
|
||||
if request.args.get('limit_days'):
|
||||
try:
|
||||
limit_days = int(request.args['limit_days'])
|
||||
except ValueError as e:
|
||||
except ValueError:
|
||||
errors = {'limit_days': ['{} is not an integer'.format(request.args['limit_days'])]}
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
else:
|
||||
limit_days = None
|
||||
|
||||
jobs = dao_get_jobs_by_service_id(service_id, limit_days)
|
||||
data = job_schema.dump(jobs, many=True).data
|
||||
statuses = [x.strip() for x in request.args.get('statuses', '').split(',')]
|
||||
|
||||
for job_data in data:
|
||||
statistics = dao_get_notification_outcomes_for_job(service_id, job_data['id'])
|
||||
job_data['statistics'] = [{'status': statistic[1], 'count': statistic[0]} for statistic in statistics]
|
||||
|
||||
return jsonify(data=data)
|
||||
page = int(request.args.get('page', 1))
|
||||
return jsonify(**get_paginated_jobs(service_id, limit_days, statuses, page))
|
||||
|
||||
|
||||
@job.route('', methods=['POST'])
|
||||
@@ -144,3 +140,28 @@ def create_job(service_id):
|
||||
job_json['statistics'] = []
|
||||
|
||||
return jsonify(data=job_json), 201
|
||||
|
||||
|
||||
def get_paginated_jobs(service_id, limit_days, statuses, page):
|
||||
pagination = dao_get_jobs_by_service_id(
|
||||
service_id,
|
||||
limit_days=limit_days,
|
||||
page=page,
|
||||
page_size=current_app.config['PAGE_SIZE'],
|
||||
statuses=statuses
|
||||
)
|
||||
data = job_schema.dump(pagination.items, many=True).data
|
||||
for job_data in data:
|
||||
statistics = dao_get_notification_outcomes_for_job(service_id, job_data['id'])
|
||||
job_data['statistics'] = [{'status': statistic[1], 'count': statistic[0]} for statistic in statistics]
|
||||
|
||||
return {
|
||||
'data': data,
|
||||
'page_size': pagination.per_page,
|
||||
'total': pagination.total,
|
||||
'links': pagination_links(
|
||||
pagination,
|
||||
'.get_jobs_by_service',
|
||||
service_id=service_id
|
||||
)
|
||||
}
|
||||
|
||||
@@ -352,6 +352,14 @@ JOB_STATUS_FINISHED = 'finished'
|
||||
JOB_STATUS_SENDING_LIMITS_EXCEEDED = 'sending limits exceeded'
|
||||
JOB_STATUS_SCHEDULED = 'scheduled'
|
||||
JOB_STATUS_CANCELLED = 'cancelled'
|
||||
JOB_STATUS_TYPES = [
|
||||
JOB_STATUS_PENDING,
|
||||
JOB_STATUS_IN_PROGRESS,
|
||||
JOB_STATUS_FINISHED,
|
||||
JOB_STATUS_SENDING_LIMITS_EXCEEDED,
|
||||
JOB_STATUS_SCHEDULED,
|
||||
JOB_STATUS_CANCELLED
|
||||
]
|
||||
|
||||
|
||||
class JobStatus(db.Model):
|
||||
|
||||
@@ -205,6 +205,8 @@ def send_notification(notification_type):
|
||||
notification, errors = (
|
||||
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
||||
).load(request.get_json())
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
if all((api_user.key_type != KEY_TYPE_TEST, service.restricted)):
|
||||
service_stats = sum(row.count for row in dao_fetch_todays_stats_for_service(service.id))
|
||||
@@ -212,13 +214,16 @@ def send_notification(notification_type):
|
||||
error = 'Exceeded send limits ({}) for today'.format(service.message_limit)
|
||||
raise InvalidRequest(error, status_code=429)
|
||||
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=notification['template'],
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
if notification_type != template.template_type:
|
||||
raise InvalidRequest("{0} template is not suitable for {1} notification".format(template.template_type,
|
||||
notification_type),
|
||||
status_code=400)
|
||||
|
||||
errors = unarchived_template_schema.validate({'archived': template.archived})
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
@@ -315,6 +315,16 @@ class NotificationWithTemplateSchema(BaseSchema):
|
||||
)
|
||||
job = fields.Nested(JobSchema, only=["id", "original_file_name"], dump_only=True)
|
||||
personalisation = fields.Dict(required=False)
|
||||
key_type = field_for(models.Notification, 'key_type', required=True)
|
||||
key_name = fields.String()
|
||||
|
||||
@pre_dump
|
||||
def add_api_key_name(self, in_data):
|
||||
if in_data.api_key:
|
||||
in_data.key_name = in_data.api_key.name
|
||||
else:
|
||||
in_data.key_name = None
|
||||
return in_data
|
||||
|
||||
|
||||
class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema):
|
||||
@@ -422,6 +432,7 @@ class NotificationsFilterSchema(ma.Schema):
|
||||
page_size = fields.Int(required=False)
|
||||
limit_days = fields.Int(required=False)
|
||||
include_jobs = fields.Boolean(required=False)
|
||||
include_from_test_key = fields.Boolean(required=False)
|
||||
|
||||
@pre_load
|
||||
def handle_multidict(self, in_data):
|
||||
|
||||
@@ -224,6 +224,7 @@ def get_all_notifications_for_service(service_id):
|
||||
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
||||
limit_days = data.get('limit_days')
|
||||
include_jobs = data.get('include_jobs', True)
|
||||
include_from_test_key = data.get('include_from_test_key', False)
|
||||
|
||||
pagination = notifications_dao.get_notifications_for_service(
|
||||
service_id,
|
||||
@@ -231,7 +232,9 @@ def get_all_notifications_for_service(service_id):
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
limit_days=limit_days,
|
||||
include_jobs=include_jobs)
|
||||
include_jobs=include_jobs,
|
||||
include_from_test_key=include_from_test_key
|
||||
)
|
||||
kwargs = request.args.to_dict()
|
||||
kwargs['service_id'] = service_id
|
||||
return jsonify(
|
||||
|
||||
@@ -4,7 +4,7 @@ from flask import url_for
|
||||
def pagination_links(pagination, endpoint, **kwargs):
|
||||
if 'page' in kwargs:
|
||||
kwargs.pop('page', None)
|
||||
links = dict()
|
||||
links = {}
|
||||
if pagination.has_prev:
|
||||
links['prev'] = url_for(endpoint, page=pagination.prev_num, **kwargs)
|
||||
if pagination.has_next:
|
||||
|
||||
Reference in New Issue
Block a user