From 0304af08df7314e20118c5f2ef6ba2fae7d33f21 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 29 Nov 2017 16:28:01 +0000 Subject: [PATCH] move service_inbound_api endpoints to their own blueprint try and reduce the size of the service blueprint :) --- app/__init__.py | 4 + app/service/callback_rest.py | 79 +++++++++++++++++++ app/service/rest.py | 68 +--------------- ...hema.py => service_callback_api_schema.py} | 12 +-- 4 files changed, 90 insertions(+), 73 deletions(-) create mode 100644 app/service/callback_rest.py rename app/service/{service_inbound_api_schema.py => service_callback_api_schema.py} (66%) diff --git a/app/__init__.py b/app/__init__.py index de0f50aad..870972817 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -82,6 +82,7 @@ def create_app(application): def register_blueprint(application): from app.service.rest import service_blueprint + from app.service.callback_rest import service_callback_blueprint from app.user.rest import user_blueprint from app.template.rest import template_blueprint from app.status.healthcheck import status as status_blueprint @@ -171,6 +172,9 @@ def register_blueprint(application): billing_blueprint.before_request(requires_admin_auth) application.register_blueprint(billing_blueprint) + service_callback_blueprint.before_request(requires_admin_auth) + application.register_blueprint(service_callback_blueprint) + def register_v2_blueprints(application): from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint as get_inbound_sms diff --git a/app/service/callback_rest.py b/app/service/callback_rest.py new file mode 100644 index 000000000..95098e989 --- /dev/null +++ b/app/service/callback_rest.py @@ -0,0 +1,79 @@ +from flask import ( + Blueprint, + jsonify, + request, +) +from sqlalchemy.exc import SQLAlchemyError + +from app.dao.service_inbound_api_dao import ( + save_service_inbound_api, + reset_service_inbound_api, + get_service_inbound_api +) +from app.errors import ( + register_errors +) +from app.models import ( + ServiceInboundApi, +) +from app.schema_validation import validate +from app.service.service_callback_api_schema import ( + create_service_callback_api_schema, + update_service_callback_api_schema +) + +service_callback_blueprint = Blueprint('service_callback', __name__, url_prefix='/service/') + +register_errors(service_callback_blueprint) + + +@service_callback_blueprint.route('/inbound-api', methods=['POST']) +def create_service_inbound_api(service_id): + data = request.get_json() + validate(data, create_service_callback_api_schema) + data["service_id"] = service_id + inbound_api = ServiceInboundApi(**data) + try: + save_service_inbound_api(inbound_api) + except SQLAlchemyError as e: + return handle_sql_error(e) + + return jsonify(data=inbound_api.serialize()), 201 + + +@service_callback_blueprint.route('/inbound-api/', methods=['POST']) +def update_service_inbound_api(service_id, inbound_api_id): + data = request.get_json() + validate(data, update_service_callback_api_schema) + + to_update = get_service_inbound_api(inbound_api_id, service_id) + + reset_service_inbound_api(service_inbound_api=to_update, + updated_by_id=data["updated_by_id"], + url=data.get("url", None), + bearer_token=data.get("bearer_token", None)) + return jsonify(data=to_update.serialize()), 200 + + +@service_callback_blueprint.route('/inbound-api/', methods=["GET"]) +def fetch_service_inbound_api(service_id, inbound_api_id): + inbound_api = get_service_inbound_api(inbound_api_id, service_id) + + return jsonify(data=inbound_api.serialize()), 200 + + +def handle_sql_error(e): + if hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror \ + and ('duplicate key value violates unique constraint "ix_service_inbound_api_service_id"' + in e.orig.pgerror): + return jsonify( + result='error', + message={'name': ["You can only have one URL and bearer token for your service."]} + ), 400 + elif hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror \ + and ('insert or update on table "service_inbound_api" violates ' + 'foreign key constraint "service_inbound_api_service_id_fkey"' + in e.orig.pgerror): + return jsonify(result='error', message="No result found"), 404 + else: + raise e diff --git a/app/service/rest.py b/app/service/rest.py index a662b8526..efb7044a3 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -7,7 +7,6 @@ from flask import ( current_app, Blueprint ) -from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm.exc import NoResultFound from app.dao import notifications_dao @@ -18,11 +17,6 @@ from app.dao.api_key_dao import ( get_unsigned_secret, expire_api_key) from app.dao.inbound_numbers_dao import dao_allocate_number_for_service -from app.dao.service_inbound_api_dao import ( - save_service_inbound_api, - reset_service_inbound_api, - get_service_inbound_api -) from app.dao.service_sms_sender_dao import ( dao_add_sms_sender_for_service, dao_update_service_sms_sender, @@ -72,17 +66,9 @@ from app.errors import ( InvalidRequest, register_errors ) - -from app.models import ( - Service, - ServiceInboundApi -) +from app.models import Service from app.schema_validation import validate from app.service import statistics -from app.service.service_inbound_api_schema import ( - service_inbound_api, - update_service_inbound_api_schema -) from app.service.service_senders_schema import ( add_service_email_reply_to_request, add_service_letter_contact_block_request, @@ -540,58 +526,6 @@ def get_monthly_template_usage(service_id): raise InvalidRequest('Year must be a number', status_code=400) -@service_blueprint.route('//inbound-api', methods=['POST']) -def create_service_inbound_api(service_id): - data = request.get_json() - validate(data, service_inbound_api) - data["service_id"] = service_id - inbound_api = ServiceInboundApi(**data) - try: - save_service_inbound_api(inbound_api) - except SQLAlchemyError as e: - return handle_sql_errror(e) - - return jsonify(data=inbound_api.serialize()), 201 - - -@service_blueprint.route('//inbound-api/', methods=['POST']) -def update_service_inbound_api(service_id, inbound_api_id): - data = request.get_json() - validate(data, update_service_inbound_api_schema) - - to_update = get_service_inbound_api(inbound_api_id, service_id) - - reset_service_inbound_api(service_inbound_api=to_update, - updated_by_id=data["updated_by_id"], - url=data.get("url", None), - bearer_token=data.get("bearer_token", None)) - return jsonify(data=to_update.serialize()), 200 - - -@service_blueprint.route('//inbound-api/', methods=["GET"]) -def fetch_service_inbound_api(service_id, inbound_api_id): - inbound_api = get_service_inbound_api(inbound_api_id, service_id) - - return jsonify(data=inbound_api.serialize()), 200 - - -def handle_sql_errror(e): - if hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror \ - and ('duplicate key value violates unique constraint "ix_service_inbound_api_service_id"' - in e.orig.pgerror): - return jsonify( - result='error', - message={'name': ["You can only have one URL and bearer token for your service."]} - ), 400 - elif hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror \ - and ('insert or update on table "service_inbound_api" violates ' - 'foreign key constraint "service_inbound_api_service_id_fkey"' - in e.orig.pgerror): - return jsonify(result='error', message="No result found"), 404 - else: - raise e - - @service_blueprint.route('//send-notification', methods=['POST']) def create_one_off_notification(service_id): resp = send_one_off_notification(service_id, request.get_json()) diff --git a/app/service/service_inbound_api_schema.py b/app/service/service_callback_api_schema.py similarity index 66% rename from app/service/service_inbound_api_schema.py rename to app/service/service_callback_api_schema.py index 88978c949..138dc3cee 100644 --- a/app/service/service_inbound_api_schema.py +++ b/app/service/service_callback_api_schema.py @@ -1,10 +1,10 @@ from app.schema_validation.definitions import uuid, https_url -service_inbound_api = { +create_service_callback_api_schema = { "$schema": "http://json-schema.org/draft-04/schema#", - "description": "POST service inbound api schema", + "description": "POST service callback/inbound api schema", "type": "object", - "title": "Create service inbound api", + "title": "Create service callback/inbound api", "properties": { "url": https_url, "bearer_token": {"type": "string", "minLength": 10}, @@ -13,11 +13,11 @@ service_inbound_api = { "required": ["url", "bearer_token", "updated_by_id"] } -update_service_inbound_api_schema = { +update_service_callback_api_schema = { "$schema": "http://json-schema.org/draft-04/schema#", - "description": "POST service inbound api schema", + "description": "POST service callback/inbound api schema", "type": "object", - "title": "Create service inbound api", + "title": "Create service callback/inbound api", "properties": { "url": https_url, "bearer_token": {"type": "string", "minLength": 10},