From 783cc05ac9c15fee2c4fe2a8e41225a918fe0ade Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Wed, 9 Jan 2019 16:18:49 +0000 Subject: [PATCH] Added type converter to JsonPayload.param --- openflexure_microscope/api/utilities.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/openflexure_microscope/api/utilities.py b/openflexure_microscope/api/utilities.py index 1db387b3..e8fba8b4 100644 --- a/openflexure_microscope/api/utilities.py +++ b/openflexure_microscope/api/utilities.py @@ -2,7 +2,7 @@ import pprint import copy -class JsonResponse: +class JsonPayload: def __init__(self, request): """ Object to wrap up simple functionality for parsing a JSON response. @@ -12,16 +12,21 @@ class JsonResponse: # Store raw response data self.data = request.get_data() - def param(self, key, default=None): + def param(self, key, default=None, convert=None): """Check if a key exists in a JSON/dictionary payload, and returns it.""" # If no JSON payload exists, return early if not self.json: return None if key in self.json: - return self.json[key] + val = self.json[key] else: - return default + val = default + + if convert: + val = convert(val) + + return val def axes_to_array(coordinate_dictionary, axis_keys=['x', 'y', 'z'], base_array=None):