From f240f3534e2db7d88242e05662fbedda1c2b4306 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 18:19:04 +0200 Subject: Map HTTP endpoint calls to API paths --- opendc/util/path_parser.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 opendc/util/path_parser.py (limited to 'opendc/util') diff --git a/opendc/util/path_parser.py b/opendc/util/path_parser.py new file mode 100644 index 00000000..2bc4e002 --- /dev/null +++ b/opendc/util/path_parser.py @@ -0,0 +1,27 @@ +import json +import sys +import re + +def parse(version, endpoint_path): + """Map an HTTP call to an API path""" + + with open('opendc/api/{}/paths.json'.format(version)) as paths_file: + paths = json.load(paths_file) + + endpoint_path_parts = endpoint_path.split('/') + paths_parts = [x.split('/') for x in paths if len(x.split('/')) == len(endpoint_path_parts)] + + for path_parts in paths_parts: + found = True + + for (endpoint_part, part) in zip(endpoint_path_parts, path_parts): + print endpoint_part, part + if not part.startswith('{') and endpoint_part != part: + found = False + break + + if found: + sys.stdout.flush() + return '{}/{}'.format(version, '/'.join(path_parts)) + + return None -- cgit v1.2.3 From c5671ab2e5115ce9c022a97a088300dc408e2aa4 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 18:25:05 +0200 Subject: Make path parser robust to trailing / --- opendc/util/path_parser.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'opendc/util') diff --git a/opendc/util/path_parser.py b/opendc/util/path_parser.py index 2bc4e002..1bd19ee7 100644 --- a/opendc/util/path_parser.py +++ b/opendc/util/path_parser.py @@ -3,19 +3,18 @@ import sys import re def parse(version, endpoint_path): - """Map an HTTP call to an API path""" + """Map an HTTP endpoint path to an API path""" with open('opendc/api/{}/paths.json'.format(version)) as paths_file: paths = json.load(paths_file) - endpoint_path_parts = endpoint_path.split('/') + endpoint_path_parts = endpoint_path.strip('/').split('/') paths_parts = [x.split('/') for x in paths if len(x.split('/')) == len(endpoint_path_parts)] for path_parts in paths_parts: found = True for (endpoint_part, part) in zip(endpoint_path_parts, path_parts): - print endpoint_part, part if not part.startswith('{') and endpoint_part != part: found = False break -- cgit v1.2.3 From d4494f1da4f4f7fcd4a9550f2e09b633c4191260 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 18:31:22 +0200 Subject: Remove stdout flush from path_parser --- opendc/util/path_parser.py | 1 - 1 file changed, 1 deletion(-) (limited to 'opendc/util') diff --git a/opendc/util/path_parser.py b/opendc/util/path_parser.py index 1bd19ee7..0c2fb493 100644 --- a/opendc/util/path_parser.py +++ b/opendc/util/path_parser.py @@ -20,7 +20,6 @@ def parse(version, endpoint_path): break if found: - sys.stdout.flush() return '{}/{}'.format(version, '/'.join(path_parts)) return None -- cgit v1.2.3 From 0b3a3f4a62744fdc5b2f7282b0bcb8886f1eeacf Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 18:42:43 +0200 Subject: Add path parameter extraction --- opendc/util/path_parser.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'opendc/util') diff --git a/opendc/util/path_parser.py b/opendc/util/path_parser.py index 0c2fb493..2f9528b1 100644 --- a/opendc/util/path_parser.py +++ b/opendc/util/path_parser.py @@ -5,21 +5,35 @@ import re def parse(version, endpoint_path): """Map an HTTP endpoint path to an API path""" + # Get possible paths with open('opendc/api/{}/paths.json'.format(version)) as paths_file: paths = json.load(paths_file) + # Find API path that matches endpoint_path endpoint_path_parts = endpoint_path.strip('/').split('/') paths_parts = [x.split('/') for x in paths if len(x.split('/')) == len(endpoint_path_parts)] + path = None for path_parts in paths_parts: found = True - for (endpoint_part, part) in zip(endpoint_path_parts, path_parts): if not part.startswith('{') and endpoint_part != part: found = False break - if found: - return '{}/{}'.format(version, '/'.join(path_parts)) + path = path_parts + + if path is None: + return None + + # Extract path parameters + parameters = {} + + for (name, value) in zip(path, endpoint_path_parts): + if name.startswith('{'): + try: + parameters[name.strip('{}')] = int(value) + except: + parameters[name.strip('{}')] = value - return None + return ('{}/{}'.format(version, '/'.join(path)), parameters) -- cgit v1.2.3 From 8801bbd2f3d1339f5a5ecad48e8f51f68b146604 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Mon, 3 Apr 2017 09:55:18 +0200 Subject: Fix error handling in HTTP requests Now return the proper status code instead of 500 --- opendc/util/rest.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'opendc/util') diff --git a/opendc/util/rest.py b/opendc/util/rest.py index 65747762..ad53f084 100644 --- a/opendc/util/rest.py +++ b/opendc/util/rest.py @@ -13,11 +13,14 @@ with open(sys.argv[1]) as file: class Request(object): """WebSocket message to REST request mapping.""" - def __init__(self, message): + 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 -- cgit v1.2.3