move service_inbound_api endpoints to their own blueprint

try and reduce the size of the service blueprint :)
This commit is contained in:
Leo Hemsted
2017-11-29 16:28:01 +00:00
parent c07e804319
commit 0304af08df
4 changed files with 90 additions and 73 deletions

View File

@@ -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('/<uuid:service_id>/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('/<uuid:service_id>/inbound-api/<uuid:inbound_api_id>', 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('/<uuid:service_id>/inbound-api/<uuid:inbound_api_id>', 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('/<uuid:service_id>/send-notification', methods=['POST'])
def create_one_off_notification(service_id):
resp = send_one_off_notification(service_id, request.get_json())