Commits

Cmed Technology  committed 9111044

refactor

  • Participants
  • Parent commits dea1a21

Comments (0)

Files changed (1)

File mockserver.py

 # Log of HTTP invocations since last reset.
 log = []
 
-# Queue of responses to be used (FIFO) on next non-control HTTP invocation. The
+# FIFO queue of responses to be used on next (non-control) HTTP invocation. The
 # responses are data templates and not real flask responses because they must be
 # made at the time they are needed.
 responses = []
         return jsonify(status='ok', log=log)
 
 
+def make_JSON_str(obj):
+    """Convert into JSON str if not already"""
+    if isinstance(obj, basestring):
+        return obj
+    else:
+        return json.dumps(obj)
+
+
 @app.errorhandler(404)
 def catch_all(e):
     try:
         response_template = responses.pop(0)
     except IndexError:
         response_template = dict()
-    data = response_template.get('data', '')
-    if not isinstance(data, basestring):
-        data = json.dumps(data)
-    status = response_template.get('status', 200)
-    if not isinstance(status, int):
-        status = int(status)
+    data = make_JSON_str(response_template.get('data', ''))
+    status = int(response_template.get('status', 200))
     response = make_response(data, status)
     for k, v in response_template.get('headers', {}).items():
         response.headers[k] = v