Added endpoints for the proxy to notifications.

- this uses alpha API for delivery
- no DB model included as just proving
- all notifications for same service at the moment (!)
This commit is contained in:
Martyn Inglis
2016-01-19 11:23:09 +00:00
parent 6c7fb42272
commit d275ba83a2
8 changed files with 385 additions and 0 deletions

View File

@@ -7,10 +7,12 @@ from flask_marshmallow import Marshmallow
from werkzeug.local import LocalProxy
from config import configs
from utils import logging
from notify_client import NotifyAPIClient
db = SQLAlchemy()
ma = Marshmallow()
notify_alpha_client = NotifyAPIClient()
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
@@ -25,17 +27,20 @@ def create_app(config_name):
ma.init_app(application)
init_app(application)
logging.init_app(application)
notify_alpha_client.init_app(application)
from app.service.rest import service as service_blueprint
from app.user.rest import user as user_blueprint
from app.template.rest import template as template_blueprint
from app.status.healthcheck import status as status_blueprint
from app.job.rest import job as job_blueprint
from app.notifications.rest import notifications as notifications_blueprint
application.register_blueprint(service_blueprint, url_prefix='/service')
application.register_blueprint(user_blueprint, url_prefix='/user')
application.register_blueprint(template_blueprint, url_prefix="/template")
application.register_blueprint(status_blueprint, url_prefix='/status')
application.register_blueprint(notifications_blueprint, url_prefix='/notifications')
application.register_blueprint(job_blueprint)
return application

View File

73
app/notifications/rest.py Normal file
View File

@@ -0,0 +1,73 @@
from flask import (
Blueprint,
jsonify,
request
)
from app import notify_alpha_client
import re
mobile_regex = re.compile("^\\+44[\\d]{10}$")
notifications = Blueprint('notifications', __name__)
@notifications.route('/', methods=['GET'])
def get_notifications():
return jsonify(notify_alpha_client.fetch_notifications()), 200
@notifications.route('/sms', methods=['POST'])
def create_sms_notification():
notification = request.get_json()['notification']
errors = {}
to_errors = validate_to(notification)
message_errors = validate_message(notification)
if to_errors:
errors.update(to_errors)
if message_errors:
errors.update(message_errors)
if errors:
return jsonify(result="error", message=errors), 400
return jsonify(notify_alpha_client.send_sms(mobile_number=notification['to'], message=notification['message'])), 200
@notifications.route('/email', methods=['POST'])
def create_email_notification():
return jsonify(id=123)
def validate_to(json_body):
errors = []
if 'to' not in json_body:
errors.append('required')
else:
if not mobile_regex.match(json_body['to']):
errors.append('invalid phone number, must be of format +441234123123')
if errors:
return {
"to": errors
}
return None
def validate_message(json_body):
errors = []
if 'message' not in json_body:
errors.append('required')
else:
message_length = len(json_body['message'])
if message_length < 1 or message_length > 160:
errors.append('Invalid length. [1 - 160]')
if errors:
return {
"message": errors
}
return None