summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-api/opendc/util/rest.py
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-04-25 21:53:42 +0200
committerGitHub <noreply@github.com>2021-04-25 21:53:42 +0200
commit128f76f7fd7c8abb41a3bbbd9f1980cbc20ae7a5 (patch)
treeadd513890005233a7784466797bfe6f5052e9eeb /opendc-web/opendc-web-api/opendc/util/rest.py
parent128a1db017545597a5c035b7960eb3fd36b5f987 (diff)
parent57b54b59ed74ec37338ae26b3864d051255aba49 (diff)
build: Flatten project structure
This change updates the project structure to become flattened. Previously, the simulator, frontend and API each lived into their own directory. With this change, all modules of the project live in the top-level directory of the repository.
Diffstat (limited to 'opendc-web/opendc-web-api/opendc/util/rest.py')
-rw-r--r--opendc-web/opendc-web-api/opendc/util/rest.py141
1 files changed, 141 insertions, 0 deletions
diff --git a/opendc-web/opendc-web-api/opendc/util/rest.py b/opendc-web/opendc-web-api/opendc/util/rest.py
new file mode 100644
index 00000000..c9e98295
--- /dev/null
+++ b/opendc-web/opendc-web-api/opendc/util/rest.py
@@ -0,0 +1,141 @@
+import importlib
+import json
+import os
+
+from oauth2client import client, crypt
+
+from opendc.util import exceptions, parameter_checker
+from opendc.util.exceptions import ClientError
+
+
+class Request:
+ """WebSocket message to REST request mapping."""
+ def __init__(self, message=None):
+ """"Initialize a Request from a socket message."""
+
+ # Get the Request parameters from the message
+
+ if message is None:
+ return
+
+ try:
+ self.message = message
+
+ self.id = message['id']
+
+ self.path = message['path']
+ self.method = message['method']
+
+ self.params_body = message['parameters']['body']
+ self.params_path = message['parameters']['path']
+ self.params_query = message['parameters']['query']
+
+ self.token = message['token']
+
+ except KeyError as exception:
+ raise exceptions.MissingRequestParameterError(exception)
+
+ # Parse the path and import the appropriate module
+
+ try:
+ self.path = message['path'].strip('/')
+
+ module_base = 'opendc.api.{}.endpoint'
+ module_path = self.path.replace('{', '').replace('}', '').replace('/', '.')
+
+ self.module = importlib.import_module(module_base.format(module_path))
+ except ImportError as e:
+ print(e)
+ raise exceptions.UnimplementedEndpointError('Unimplemented endpoint: {}.'.format(self.path))
+
+ # Check the method
+
+ if self.method not in ['POST', 'GET', 'PUT', 'PATCH', 'DELETE']:
+ raise exceptions.UnsupportedMethodError('Non-rest method: {}'.format(self.method))
+
+ if not hasattr(self.module, self.method):
+ raise exceptions.UnsupportedMethodError('Unimplemented method at endpoint {}: {}'.format(
+ self.path, self.method))
+
+ # Verify the user
+
+ if "OPENDC_FLASK_TESTING" in os.environ:
+ self.google_id = 'test'
+ return
+
+ try:
+ self.google_id = self._verify_token(self.token)
+ except crypt.AppIdentityError as e:
+ raise exceptions.AuthorizationTokenError(e)
+
+ def check_required_parameters(self, **kwargs):
+ """Raise an error if a parameter is missing or of the wrong type."""
+
+ try:
+ parameter_checker.check(self, **kwargs)
+ except exceptions.ParameterError as e:
+ raise ClientError(Response(400, str(e)))
+
+ def process(self):
+ """Process the Request and return a Response."""
+
+ method = getattr(self.module, self.method)
+
+ try:
+ response = method(self)
+ except ClientError as e:
+ e.response.id = self.id
+ return e.response
+
+ response.id = self.id
+
+ return response
+
+ def to_JSON(self):
+ """Return a JSON representation of this Request"""
+
+ self.message['id'] = 0
+ self.message['token'] = None
+
+ return json.dumps(self.message)
+
+ @staticmethod
+ def _verify_token(token):
+ """Return the ID of the signed-in user.
+
+ Or throw an Exception if the token is invalid.
+ """
+
+ try:
+ id_info = client.verify_id_token(token, os.environ['OPENDC_OAUTH_CLIENT_ID'])
+ except Exception as e:
+ print(e)
+ raise crypt.AppIdentityError('Exception caught trying to verify ID token: {}'.format(e))
+
+ if id_info['aud'] != os.environ['OPENDC_OAUTH_CLIENT_ID']:
+ raise crypt.AppIdentityError('Unrecognized client.')
+
+ if id_info['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
+ raise crypt.AppIdentityError('Wrong issuer.')
+
+ return id_info['sub']
+
+
+class Response:
+ """Response to websocket mapping"""
+ def __init__(self, status_code, status_description, content=None):
+ """Initialize a new Response."""
+
+ self.id = 0
+ self.status = {'code': status_code, 'description': status_description}
+ self.content = content
+
+ def to_JSON(self):
+ """"Return a JSON representation of this Response"""
+
+ data = {'id': self.id, 'status': self.status}
+
+ if self.content is not None:
+ data['content'] = self.content
+
+ return json.dumps(data, default=str)