diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-06-26 12:17:26 +0200 |
|---|---|---|
| committer | Georgios Andreadis <info@gandreadis.com> | 2020-06-26 12:17:26 +0200 |
| commit | 92b94b59ad80329a2c99471edbf5bbdc9af1e525 (patch) | |
| tree | d9b71de4473ffe5d5decc8631f82429694860dcc /opendc/api/v2 | |
| parent | c15448fb6f3aacc8939f2714fc20304dde98cd28 (diff) | |
Revamp error responses
Diffstat (limited to 'opendc/api/v2')
| -rw-r--r-- | opendc/api/v2/simulations/endpoint.py | 5 | ||||
| -rw-r--r-- | opendc/api/v2/simulations/simulationId/endpoint.py | 40 | ||||
| -rw-r--r-- | opendc/api/v2/simulations/simulationId/topologies/endpoint.py | 5 | ||||
| -rw-r--r-- | opendc/api/v2/topologies/topologyId/endpoint.py | 5 | ||||
| -rw-r--r-- | opendc/api/v2/users/endpoint.py | 10 | ||||
| -rw-r--r-- | opendc/api/v2/users/userId/endpoint.py | 23 |
6 files changed, 24 insertions, 64 deletions
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']) |
