From f81f697961faa6a6d6648a43f8577bdc5943e0f6 Mon Sep 17 00:00:00 2001 From: Super User Date: Wed, 10 Jul 2024 01:46:54 +0300 Subject: [PATCH] resolve review --- .../endpoints/service.py | 63 ++++++++++++------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/webhook_to_fedora_messaging/endpoints/service.py b/webhook_to_fedora_messaging/endpoints/service.py index 0519a12..190efa5 100644 --- a/webhook_to_fedora_messaging/endpoints/service.py +++ b/webhook_to_fedora_messaging/endpoints/service.py @@ -3,23 +3,25 @@ from ..models.service import Service from ..models.user import User from sqlalchemy_helpers import get_or_create -from .util import not_found, success, bad_request, created, conflict, validate_request, exclude_from_val +from .util import not_found, success, bad_request, created, conflict, validate_request, exclude_from_val, unprocessable_entity + app = Flask(__name__) service_endpoint = Blueprint("service_endpoint", __name__) - @service_endpoint.route("/service", methods=["POST"]) def create_service(): """ - Used for creating a new service by sending a post request to /service/ path. + Used for creating a new service by sending a post request to /service/ path. """ - + if not validate_request(request.json, fields=['username', 'type', 'desc', 'name']): + return unprocessable_entity() + session = db.Session() body = request.json - + user = session.query(User).filter(User.username == body['username']).first() if user is None: return not_found() @@ -34,6 +36,12 @@ def create_service(): @service_endpoint.route("/service/search", methods=["GET"]) def list_services(): + """ + Used for listing all services belong to a user by sending a get request to /service/search path + """ + if not validate_request(request): + return unprocessable_entity() + session = db.Session() user = session.query(User).filter(User.username.like(request.json['username'])).first() if user is None: @@ -45,10 +53,16 @@ def list_services(): @service_endpoint.route("/service", methods=["GET"]) -@exclude_from_val def lookup_service(): + """ + Used for retrieving a service by it's uuid by sending a get request + to the /service path. + + Request Body: + service_uuid: Service UUID + """ if not validate_request(request, ['service_uuid']): - return bad_request() + return unprocessable_entity() session = db.Session() service = session.query(Service).filter(Service.id == request.json['service_uuid']).first() @@ -59,13 +73,17 @@ def lookup_service(): return success({'uuid': service.id, 'name': service.name, 'type': service.type, 'desc': service.desc}) -#TODO make the api key also disabled or what needed here. @service_endpoint.route("/service/revoke", methods=["PUT"]) -@exclude_from_val def revoke_service(): + """ + Used for revoking a service by sending a PUT request to /service/revoke path. + Request Body: + service_uuid: Service UUID + username: Username of the user that servicce belongs to. + """ if not validate_request(request, fields=['username', 'service_uuid']): - return bad_request() + return unprocessable_entity() session = db.Session() user = session.query(User).filter(User.username == request.json['username']).first() @@ -78,18 +96,23 @@ def revoke_service(): service.disabled = True session.commit() - - + return success({'uuid': service.id, 'is_valid': not service.disabled}) - @service_endpoint.route("/service", methods=["PUT"]) -@exclude_from_val def update_service(): + """ + Used for updating a service by sending a PUT request to /service path. - if not validate_request(request, fields=['uuid']): - return bad_request() + Request Body: + uuid: UUID of the service + name: Updated name (optional) + mesg_body: Updated message body (optional) + + """ + if not validate_request(request, fields=['uuid', 'name', 'mesg_body']): + return unprocessable_entity() session = db.Session() service = session.query(Service).filter(Service.id == request.json['uuid']).first() @@ -100,11 +123,3 @@ def update_service(): service.desc = request.json['mesg_body'] if request.json['mesg_body'] is not None and request.json['mesg_body'] != "" else service.desc session.commit() return success({'uuid': service.id, 'name': service.name, 'mesg_body': service.desc, 'is_valid': not service.disabled}) - - -@service_endpoint.before_request -def validation(): - if not validate_request(request): - return bad_request() - - \ No newline at end of file