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

79 lines
2.6 KiB
Python
Raw Normal View History

import uuid
from flask import (
Blueprint,
jsonify,
request
)
2016-02-09 16:13:48 +00:00
from app import api_user
from app.aws_sqs import add_notification_to_queue
from app.dao import (templates_dao)
from app.schemas import (
email_notification_schema, sms_template_notification_schema)
2016-02-15 11:11:20 +00:00
from app import celery
notifications = Blueprint('notifications', __name__)
@notifications.route('/<notification_id>', methods=['GET'])
def get_notifications(notification_id):
# TODO return notification id details
return jsonify({'id': notification_id}), 200
2016-02-15 11:11:20 +00:00
@celery.task(name="make-sms", bind="True")
def send_sms(self):
print('Executing task id {0.id}, args: {0.args!r} kwargs: {0.kwargs!r}'.format(self.request))
from time import sleep
sleep(0.5)
print('finished')
@notifications.route('/sms', methods=['POST'])
def create_sms_notification():
resp_json = request.get_json()
notification, errors = sms_template_notification_schema.load(resp_json)
if errors:
return jsonify(result="error", message=errors), 400
2016-02-15 11:11:20 +00:00
send_sms.delay()
notification_id = add_notification_to_queue(api_user['client'], notification['template'], 'sms', notification)
return jsonify({'notification_id': notification_id}), 201
@notifications.route('/email', methods=['POST'])
def create_email_notification():
resp_json = request.get_json()
notification, errors = email_notification_schema.load(resp_json)
2016-01-20 13:14:23 +00:00
if errors:
return jsonify(result="error", message=errors), 400
notification_id = add_notification_to_queue(api_user['client'], "admin", 'email', notification)
return jsonify({'notification_id': notification_id}), 201
@notifications.route('/sms/service/<service_id>', methods=['POST'])
def create_sms_for_service(service_id):
resp_json = request.get_json()
notification, errors = sms_template_notification_schema.load(resp_json)
if errors:
return jsonify(result="error", message=errors), 400
template_id = notification['template']
job_id = notification['job']
# TODO: job/job_id is in notification and can used to update job status
# TODO: remove once beta is reading notifications from the queue
template = templates_dao.get_model_templates(template_id)
if template.service.id != uuid.UUID(service_id):
message = "Invalid template: id {} for service id: {}".format(template.id, service_id)
return jsonify(result="error", message=message), 400
notification_id = add_notification_to_queue(service_id, template_id, 'sms', notification)
return jsonify({'notification_id': notification_id}), 201