Add fetch request for service inbound api.

Add unique constraint on service_id for service_inbound_api.
This commit is contained in:
Rebecca Law
2017-06-15 16:19:12 +01:00
parent 828d5cd493
commit effb99dca8
5 changed files with 47 additions and 15 deletions

View File

@@ -23,4 +23,4 @@ def reset_service_inbound_api(service_inbound_api):
def get_service_inbound_api(service_inbound_api_id, service_id):
return ServiceInboundApi.query.filter_by(id=service_inbound_api_id,
service_id=service_id).one()
service_id=service_id).first()

View File

@@ -298,8 +298,8 @@ class ServiceWhitelist(db.Model):
class ServiceInboundApi(db.Model, Versioned):
__tablename__ = 'service_inbound_api'
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False)
service = db.relationship('Service')
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False, unique=True)
service = db.relationship('Service', backref='inbound_api')
url = db.Column(db.String(255), nullable=False)
bearer_token = db.Column(db.String(255), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False)
@@ -313,11 +313,11 @@ class ServiceInboundApi(db.Model, Versioned):
def serialize(self):
return {
"id": self.id,
"service_id": self.service_id,
"id": str(self.id),
"service_id": str(self.service_id),
"url": self.url,
"bearer_token": self.bearer_token,
"updated_by_id": self.updated_by_id,
"updated_by_id": str(self.updated_by_id),
"created_at": self.created_at.strftime(DATETIME_FORMAT),
"updated_at": self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None
}

View File

@@ -8,6 +8,7 @@ from flask import (
current_app,
Blueprint
)
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.exc import NoResultFound
from app import redis_store
@@ -547,17 +548,28 @@ def create_service_inbound_api(service_id):
validate(data, service_inbound_api)
data["service_id"] = service_id
inbound_api = ServiceInboundApi(**data)
save_service_inbound_api(inbound_api)
try:
save_service_inbound_api(inbound_api)
except SQLAlchemyError as 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
else:
raise e
return jsonify(data=inbound_api.serialize()), 201
@service_blueprint.route('/<uuid:service_id>/inbound-api/<uuid:id>', methods=['POST'])
def update_service_inbound_api(service_id, id):
@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(id, service_id)
to_update = get_service_inbound_api(inbound_api_id, service_id)
if data.get("url", None):
to_update.url = data["url"]
@@ -568,3 +580,10 @@ def update_service_inbound_api(service_id, id):
reset_service_inbound_api(to_update)
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