2017-04-25 14:56:16 +01:00
|
|
|
from datetime import datetime
|
2017-05-09 14:20:56 +01:00
|
|
|
from functools import wraps
|
2017-04-25 14:56:16 +01:00
|
|
|
|
|
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
|
|
|
|
jsonify,
|
|
|
|
|
request,
|
|
|
|
|
current_app,
|
|
|
|
|
json
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from app import statsd_client
|
2017-05-04 10:33:44 +01:00
|
|
|
from app.celery.tasks import update_letter_notifications_statuses
|
2017-04-25 14:56:16 +01:00
|
|
|
from app.clients.email.aws_ses import get_aws_responses
|
2017-05-09 14:20:56 +01:00
|
|
|
from app.dao import notifications_dao
|
|
|
|
|
from app.v2.errors import register_errors
|
2017-04-25 14:56:16 +01:00
|
|
|
from app.notifications.process_client_response import validate_callback_data
|
2017-05-09 14:20:56 +01:00
|
|
|
from app.notifications.utils import autoconfirm_subscription
|
|
|
|
|
from app.schema_validation import validate
|
|
|
|
|
|
2017-04-25 14:56:16 +01:00
|
|
|
|
|
|
|
|
letter_callback_blueprint = Blueprint('notifications_letter_callback', __name__)
|
2017-05-09 14:20:56 +01:00
|
|
|
register_errors(letter_callback_blueprint)
|
2017-04-25 14:56:16 +01:00
|
|
|
|
|
|
|
|
|
2017-05-09 14:20:56 +01:00
|
|
|
dvla_sns_callback_schema = {
|
|
|
|
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
|
|
|
|
"description": "sns callback received on s3 update",
|
|
|
|
|
"type": "object",
|
|
|
|
|
"title": "dvla internal sns callback",
|
|
|
|
|
"properties": {
|
|
|
|
|
"Type": {"enum": ["Notification", "SubscriptionConfirmation"]},
|
|
|
|
|
"MessageId": {"type": "string"},
|
|
|
|
|
"Message": {"type": ["string", "object"]}
|
|
|
|
|
},
|
|
|
|
|
"required": ["Type", "MessageId", "Message"]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_schema(schema):
|
|
|
|
|
def decorator(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
|
def wrapper(*args, **kw):
|
|
|
|
|
validate(request.json, schema)
|
|
|
|
|
return f(*args, **kw)
|
|
|
|
|
return wrapper
|
|
|
|
|
return decorator
|
2017-04-25 14:56:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@letter_callback_blueprint.route('/notifications/letter/dvla', methods=['POST'])
|
2017-05-09 14:20:56 +01:00
|
|
|
@validate_schema(dvla_sns_callback_schema)
|
2017-04-25 14:56:16 +01:00
|
|
|
def process_letter_response():
|
2017-05-09 14:20:56 +01:00
|
|
|
req_json = request.json
|
|
|
|
|
if not autoconfirm_subscription(req_json):
|
2017-05-04 10:33:44 +01:00
|
|
|
# The callback should have one record for an S3 Put Event.
|
|
|
|
|
filename = req_json['Message']['Records'][0]['s3']['object']['key']
|
2017-05-09 14:20:56 +01:00
|
|
|
current_app.logger.info('Received file from DVLA: {}'.format(filename))
|
2017-05-04 10:33:44 +01:00
|
|
|
current_app.logger.info('DVLA callback: Calling task to update letter notifications')
|
|
|
|
|
update_letter_notifications_statuses.apply_async([filename], queue='notify')
|
|
|
|
|
|
2017-05-09 14:20:56 +01:00
|
|
|
return jsonify(
|
|
|
|
|
result="success", message="DVLA callback succeeded"
|
|
|
|
|
), 200
|