mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-24 08:16:19 -04:00
move service_inbound_api endpoints to their own blueprint
try and reduce the size of the service blueprint :)
This commit is contained in:
@@ -82,6 +82,7 @@ def create_app(application):
|
|||||||
|
|
||||||
def register_blueprint(application):
|
def register_blueprint(application):
|
||||||
from app.service.rest import service_blueprint
|
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.user.rest import user_blueprint
|
||||||
from app.template.rest import template_blueprint
|
from app.template.rest import template_blueprint
|
||||||
from app.status.healthcheck import status as status_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)
|
billing_blueprint.before_request(requires_admin_auth)
|
||||||
application.register_blueprint(billing_blueprint)
|
application.register_blueprint(billing_blueprint)
|
||||||
|
|
||||||
|
service_callback_blueprint.before_request(requires_admin_auth)
|
||||||
|
application.register_blueprint(service_callback_blueprint)
|
||||||
|
|
||||||
|
|
||||||
def register_v2_blueprints(application):
|
def register_v2_blueprints(application):
|
||||||
from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint as get_inbound_sms
|
from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint as get_inbound_sms
|
||||||
|
|||||||
79
app/service/callback_rest.py
Normal file
79
app/service/callback_rest.py
Normal file
@@ -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/<uuid:service_id>')
|
||||||
|
|
||||||
|
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/<uuid:inbound_api_id>', 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/<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_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
|
||||||
@@ -7,7 +7,6 @@ from flask import (
|
|||||||
current_app,
|
current_app,
|
||||||
Blueprint
|
Blueprint
|
||||||
)
|
)
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
from app.dao import notifications_dao
|
from app.dao import notifications_dao
|
||||||
@@ -18,11 +17,6 @@ from app.dao.api_key_dao import (
|
|||||||
get_unsigned_secret,
|
get_unsigned_secret,
|
||||||
expire_api_key)
|
expire_api_key)
|
||||||
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
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 (
|
from app.dao.service_sms_sender_dao import (
|
||||||
dao_add_sms_sender_for_service,
|
dao_add_sms_sender_for_service,
|
||||||
dao_update_service_sms_sender,
|
dao_update_service_sms_sender,
|
||||||
@@ -72,17 +66,9 @@ from app.errors import (
|
|||||||
InvalidRequest,
|
InvalidRequest,
|
||||||
register_errors
|
register_errors
|
||||||
)
|
)
|
||||||
|
from app.models import Service
|
||||||
from app.models import (
|
|
||||||
Service,
|
|
||||||
ServiceInboundApi
|
|
||||||
)
|
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.service import statistics
|
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 (
|
from app.service.service_senders_schema import (
|
||||||
add_service_email_reply_to_request,
|
add_service_email_reply_to_request,
|
||||||
add_service_letter_contact_block_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)
|
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'])
|
@service_blueprint.route('/<uuid:service_id>/send-notification', methods=['POST'])
|
||||||
def create_one_off_notification(service_id):
|
def create_one_off_notification(service_id):
|
||||||
resp = send_one_off_notification(service_id, request.get_json())
|
resp = send_one_off_notification(service_id, request.get_json())
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
from app.schema_validation.definitions import uuid, https_url
|
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#",
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
"description": "POST service inbound api schema",
|
"description": "POST service callback/inbound api schema",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Create service inbound api",
|
"title": "Create service callback/inbound api",
|
||||||
"properties": {
|
"properties": {
|
||||||
"url": https_url,
|
"url": https_url,
|
||||||
"bearer_token": {"type": "string", "minLength": 10},
|
"bearer_token": {"type": "string", "minLength": 10},
|
||||||
@@ -13,11 +13,11 @@ service_inbound_api = {
|
|||||||
"required": ["url", "bearer_token", "updated_by_id"]
|
"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#",
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
"description": "POST service inbound api schema",
|
"description": "POST service callback/inbound api schema",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Create service inbound api",
|
"title": "Create service callback/inbound api",
|
||||||
"properties": {
|
"properties": {
|
||||||
"url": https_url,
|
"url": https_url,
|
||||||
"bearer_token": {"type": "string", "minLength": 10},
|
"bearer_token": {"type": "string", "minLength": 10},
|
||||||
Reference in New Issue
Block a user