mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
[WIP] Added endpoints under /job for creating, updating and reading
notification status.
This commit is contained in:
@@ -8,7 +8,6 @@ from flask import (
|
||||
current_app
|
||||
)
|
||||
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.exc import DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
@@ -18,10 +17,19 @@ from app.dao.jobs_dao import (
|
||||
get_jobs_by_service
|
||||
)
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
save_notification,
|
||||
get_notification,
|
||||
get_notifications
|
||||
)
|
||||
|
||||
from app.schemas import (
|
||||
job_schema,
|
||||
jobs_schema,
|
||||
job_schema_load_json
|
||||
job_schema_load_json,
|
||||
notification_status_schema,
|
||||
notifications_status_schema,
|
||||
notification_status_schema_load_json
|
||||
)
|
||||
|
||||
job = Blueprint('job', __name__, url_prefix='/service/<service_id>/job')
|
||||
@@ -72,6 +80,55 @@ def update_job(service_id, job_id):
|
||||
return jsonify(data=job_schema.dump(job).data), 200
|
||||
|
||||
|
||||
@job.route('/<job_id>/notification', methods=['POST'])
|
||||
def create_notification_for_job(service_id, job_id):
|
||||
|
||||
# TODO assert service_id == payload service id
|
||||
# and same for job id
|
||||
notification, errors = notification_status_schema.load(request.get_json())
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
try:
|
||||
save_notification(notification)
|
||||
except Exception as e:
|
||||
return jsonify(result="error", message=str(e)), 500
|
||||
return jsonify(data=notification_status_schema.dump(notification).data), 201
|
||||
|
||||
|
||||
@job.route('/<job_id>/notification', methods=['GET'])
|
||||
@job.route('/<job_id>/notification/<notification_id>')
|
||||
def get_notification_for_job(service_id, job_id, notification_id=None):
|
||||
if notification_id:
|
||||
try:
|
||||
notification = get_notification(service_id, job_id, notification_id)
|
||||
data, errors = notification_status_schema.dump(notification)
|
||||
return jsonify(data=data)
|
||||
except DataError:
|
||||
return jsonify(result="error", message="Invalid notification id"), 400
|
||||
except NoResultFound:
|
||||
return jsonify(result="error", message="Notification not found"), 404
|
||||
else:
|
||||
notifications = get_notifications(service_id, job_id)
|
||||
data, errors = notifications_status_schema.dump(notifications)
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@job.route('/<job_id>/notification/<notification_id>', methods=['PUT'])
|
||||
def update_notification_for_job(service_id, job_id, notification_id):
|
||||
|
||||
notification = get_notification(service_id, job_id, notification_id)
|
||||
update_dict, errors = notification_status_schema_load_json.load(request.get_json())
|
||||
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
try:
|
||||
save_notification(notification, update_dict=update_dict)
|
||||
except Exception as e:
|
||||
return jsonify(result="error", message=str(e)), 400
|
||||
|
||||
return jsonify(data=job_schema.dump(notification).data), 200
|
||||
|
||||
|
||||
def _enqueue_job(job):
|
||||
aws_region = current_app.config['AWS_REGION']
|
||||
queue_name = current_app.config['NOTIFY_JOB_QUEUE']
|
||||
|
||||
Reference in New Issue
Block a user