From 92b94b59ad80329a2c99471edbf5bbdc9af1e525 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Fri, 26 Jun 2020 12:17:26 +0200 Subject: Revamp error responses --- opendc/api/v2/simulations/endpoint.py | 5 +-- opendc/api/v2/simulations/simulationId/endpoint.py | 40 +++++++--------------- .../simulationId/topologies/endpoint.py | 5 +-- opendc/api/v2/topologies/topologyId/endpoint.py | 5 +-- opendc/api/v2/users/endpoint.py | 10 ++---- opendc/api/v2/users/userId/endpoint.py | 23 ++++--------- 6 files changed, 24 insertions(+), 64 deletions(-) (limited to 'opendc/api') diff --git a/opendc/api/v2/simulations/endpoint.py b/opendc/api/v2/simulations/endpoint.py index dfeb8d5e..62257d40 100644 --- a/opendc/api/v2/simulations/endpoint.py +++ b/opendc/api/v2/simulations/endpoint.py @@ -11,10 +11,7 @@ from opendc.util.rest import Response def POST(request): """Create a new simulation, and return that new simulation.""" - try: - request.check_required_parameters(body={'simulation': {'name': 'string'}}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(body={'simulation': {'name': 'string'}}) topology = Topology({'name': 'Default topology'}) topology.insert() diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py index b08cf8be..b8ae9a38 100644 --- a/opendc/api/v2/simulations/simulationId/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/endpoint.py @@ -10,10 +10,7 @@ from opendc.util.rest import Response def GET(request): """Get this Simulation.""" - try: - request.check_required_parameters(path={'simulationId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(path={'simulationId': 'string'}) simulation = Simulation.from_id(request.params_path['simulationId']) validation_error = simulation.validate() @@ -30,10 +27,7 @@ def GET(request): def PUT(request): """Update a simulation's name.""" - try: - request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'}) simulation = Simulation.from_id(request.params_path['simulationId']) @@ -55,30 +49,20 @@ def PUT(request): def DELETE(request): """Delete this Simulation.""" - # Make sure required parameters are there + request.check_required_parameters(path={'simulationId': 'string'}) - try: - request.check_required_parameters(path={'simulationId': 'string'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate a Simulation and make sure it exists - - simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) + simulation = Simulation.from_id(request.params_path['simulationId']) - # Make sure this User is allowed to delete this Simulation + validation_error = simulation.validate() + if validation_error is not None: + return validation_error - if not simulation.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from deleting {}.'.format(simulation)) + access_error = simulation.validate_user_access(request.google_id, True) + if access_error is not None: + return access_error - # Delete this Simulation from the database + # FIXME cascading simulation.delete() - # Return this Simulation - - return Response(200, 'Successfully deleted {}.'.format(simulation), simulation.to_JSON()) + return Response(200, f'Successfully deleted simulation.', simulation.obj) diff --git a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py index 4104c4dd..d8467b40 100644 --- a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py @@ -12,10 +12,7 @@ def POST(request): # Make sure required parameters are there - try: - request.check_required_parameters(body={'topology': {'name': 'string'}}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(body={'topology': {'name': 'string'}}) # Instantiate a Topology object from our request, and add some metadata diff --git a/opendc/api/v2/topologies/topologyId/endpoint.py b/opendc/api/v2/topologies/topologyId/endpoint.py index ef541daa..719048c4 100644 --- a/opendc/api/v2/topologies/topologyId/endpoint.py +++ b/opendc/api/v2/topologies/topologyId/endpoint.py @@ -8,10 +8,7 @@ def GET(request): # Make sure required parameters are there - try: - request.check_required_parameters(path={'topologyId': 'int'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(path={'topologyId': 'int'}) # Instantiate a Topology from the database diff --git a/opendc/api/v2/users/endpoint.py b/opendc/api/v2/users/endpoint.py index b1a3675d..4b0a883a 100644 --- a/opendc/api/v2/users/endpoint.py +++ b/opendc/api/v2/users/endpoint.py @@ -7,10 +7,7 @@ from opendc.util.rest import Response def GET(request): """Search for a User using their email address.""" - try: - request.check_required_parameters(query={'email': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(query={'email': 'string'}) user = User.from_email(request.params_query['email']) @@ -24,10 +21,7 @@ def GET(request): def POST(request): """Add a new User.""" - try: - request.check_required_parameters(body={'user': {'email': 'string'}}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(body={'user': {'email': 'string'}}) user = User(request.params_body['user']) user.set_property('googleId', request.google_id) diff --git a/opendc/api/v2/users/userId/endpoint.py b/opendc/api/v2/users/userId/endpoint.py index 3fb2ecc8..578080b7 100644 --- a/opendc/api/v2/users/userId/endpoint.py +++ b/opendc/api/v2/users/userId/endpoint.py @@ -6,10 +6,7 @@ from opendc.util.rest import Response def GET(request): """Get this User.""" - try: - request.check_required_parameters(path={'userId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(path={'userId': 'string'}) user = User.from_id(request.params_path['userId']) @@ -23,14 +20,11 @@ def GET(request): def PUT(request): """Update this User's given name and/or family name.""" - try: - request.check_required_parameters(body={'user': { - 'givenName': 'string', - 'familyName': 'string' - }}, - path={'userId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(body={'user': { + 'givenName': 'string', + 'familyName': 'string' + }}, + path={'userId': 'string'}) user = User.from_id(request.params_path['userId']) @@ -49,10 +43,7 @@ def PUT(request): def DELETE(request): """Delete this User.""" - try: - request.check_required_parameters(path={'userId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(path={'userId': 'string'}) user = User.from_id(request.params_path['userId']) -- cgit v1.2.3 From 19bede4fc7f7320bb4eb16c3fe1a211b19ab4714 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Fri, 26 Jun 2020 12:27:51 +0200 Subject: Revamp error responses everywhere --- opendc/api/v2/simulations/simulationId/endpoint.py | 26 +++++----------------- opendc/api/v2/topologies/topologyId/endpoint.py | 18 ++------------- opendc/api/v2/users/endpoint.py | 8 ++----- opendc/api/v2/users/userId/endpoint.py | 14 +++++------- 4 files changed, 15 insertions(+), 51 deletions(-) (limited to 'opendc/api') diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py index b8ae9a38..8d29202d 100644 --- a/opendc/api/v2/simulations/simulationId/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/endpoint.py @@ -13,13 +13,9 @@ def GET(request): request.check_required_parameters(path={'simulationId': 'string'}) simulation = Simulation.from_id(request.params_path['simulationId']) - validation_error = simulation.validate() - if validation_error is not None: - return validation_error - access_error = simulation.validate_user_access(request.google_id, False) - if access_error is not None: - return access_error + simulation.check_exists() + simulation.check_user_access(request.google_id, False) return Response(200, 'Successfully retrieved simulation', simulation.obj) @@ -31,13 +27,8 @@ def PUT(request): simulation = Simulation.from_id(request.params_path['simulationId']) - validation_error = simulation.validate() - if validation_error is not None: - return validation_error - - access_error = simulation.validate_user_access(request.google_id, True) - if access_error is not None: - return access_error + simulation.check_exists() + simulation.check_user_access(request.google_id, True) simulation.set_property('name', request.params_body['simulation']['name']) simulation.set_property('datetime_last_edited', Database.datetime_to_string(datetime.now())) @@ -53,13 +44,8 @@ def DELETE(request): simulation = Simulation.from_id(request.params_path['simulationId']) - validation_error = simulation.validate() - if validation_error is not None: - return validation_error - - access_error = simulation.validate_user_access(request.google_id, True) - if access_error is not None: - return access_error + simulation.check_exists() + simulation.check_user_access(request.google_id, True) # FIXME cascading diff --git a/opendc/api/v2/topologies/topologyId/endpoint.py b/opendc/api/v2/topologies/topologyId/endpoint.py index 719048c4..3470ad94 100644 --- a/opendc/api/v2/topologies/topologyId/endpoint.py +++ b/opendc/api/v2/topologies/topologyId/endpoint.py @@ -6,26 +6,12 @@ from opendc.util.rest import Response def GET(request): """Get this Topology.""" - # Make sure required parameters are there - request.check_required_parameters(path={'topologyId': 'int'}) - # Instantiate a Topology from the database - topology = Topology.from_id(request.params_path['topologyId']) - # Make sure this Topology exists - - validation_error = topology.validate() - if validation_error is not None: - return validation_error - - # Make sure this user is authorized to view this Topology - - access_error = topology.validate_user_access(request.google_id, False) - if access_error is not None: - return access_error + topology.check_exists() - # Return this Topology + topology.check_user_access(request.google_id, False) return Response(200, 'Successfully retrieved topology.', topology.obj) diff --git a/opendc/api/v2/users/endpoint.py b/opendc/api/v2/users/endpoint.py index 4b0a883a..c6041756 100644 --- a/opendc/api/v2/users/endpoint.py +++ b/opendc/api/v2/users/endpoint.py @@ -11,9 +11,7 @@ def GET(request): user = User.from_email(request.params_query['email']) - validation_error = user.validate() - if validation_error is not None: - return validation_error + user.check_exists() return Response(200, f'Successfully retrieved user.', user.obj) @@ -27,9 +25,7 @@ def POST(request): user.set_property('googleId', request.google_id) user.set_property('authorizations', []) - validation_error = user.validate_insertion() - if validation_error is not None: - return validation_error + user.check_already_exists() user.insert() return Response(200, f'Successfully created user.', user.obj) diff --git a/opendc/api/v2/users/userId/endpoint.py b/opendc/api/v2/users/userId/endpoint.py index 578080b7..e68a2bb3 100644 --- a/opendc/api/v2/users/userId/endpoint.py +++ b/opendc/api/v2/users/userId/endpoint.py @@ -10,9 +10,7 @@ def GET(request): user = User.from_id(request.params_path['userId']) - validation_error = user.validate() - if validation_error is not None: - return validation_error + user.check_exists() return Response(200, f'Successfully retrieved user.', user.obj) @@ -28,9 +26,8 @@ def PUT(request): user = User.from_id(request.params_path['userId']) - validation_error = user.validate(request.google_id) - if validation_error is not None: - return validation_error + user.check_exists() + user.check_correct_user(request.google_id) user.set_property('givenName', request.params_body['user']['givenName']) user.set_property('familyName', request.params_body['user']['familyName']) @@ -47,9 +44,8 @@ def DELETE(request): user = User.from_id(request.params_path['userId']) - validation_error = user.validate(request.google_id) - if validation_error is not None: - return validation_error + user.check_exists() + user.check_correct_user(request.google_id) user.delete() -- cgit v1.2.3