Files
notifications-api/app/notifications/notifications_ses_callback.py

26 lines
1.0 KiB
Python
Raw Normal View History

2022-10-04 16:01:30 -07:00
from flask import Blueprint, jsonify, request
2022-10-03 17:16:59 -07:00
from app.celery.process_ses_receipts_tasks import process_ses_results
from app.config import QueueNames
2022-10-03 17:16:59 -07:00
from app.errors import InvalidRequest
from app.notifications.sns_handlers import sns_notification_handler
2022-09-15 14:59:13 -07:00
2023-08-29 14:54:30 -07:00
ses_callback_blueprint = Blueprint("notifications_ses_callback", __name__)
2022-09-15 14:59:13 -07:00
2022-10-14 14:45:27 +00:00
2022-10-03 17:16:59 -07:00
# 400 counts as a permanent failure so SNS will not retry.
# 500 counts as a failed delivery attempt so SNS will retry.
# See https://docs.aws.amazon.com/sns/latest/dg/DeliveryPolicies.html#DeliveryPolicies
2023-08-29 14:54:30 -07:00
@ses_callback_blueprint.route("/notifications/email/ses", methods=["POST"])
2022-10-03 17:16:59 -07:00
def email_ses_callback_handler():
try:
2022-10-03 17:16:59 -07:00
data = sns_notification_handler(request.data, request.headers)
2022-10-03 20:07:42 -07:00
except InvalidRequest as e:
2023-08-29 14:54:30 -07:00
return jsonify(result="error", message=str(e.message)), e.status_code
2022-10-14 14:45:27 +00:00
2022-10-03 17:16:59 -07:00
message = data.get("Message")
if "mail" in message:
process_ses_results.apply_async([{"Message": message}], queue=QueueNames.NOTIFY)
2023-08-29 14:54:30 -07:00
return jsonify(result="success", message="SES-SNS callback succeeded"), 200