about summary refs log tree commit diff
path: root/gn3/responses
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/responses')
-rw-r--r--gn3/responses/__init__.py0
-rw-r--r--gn3/responses/pcorrs_responses.py24
2 files changed, 24 insertions, 0 deletions
diff --git a/gn3/responses/__init__.py b/gn3/responses/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gn3/responses/__init__.py
diff --git a/gn3/responses/pcorrs_responses.py b/gn3/responses/pcorrs_responses.py
new file mode 100644
index 0000000..d6fd9d7
--- /dev/null
+++ b/gn3/responses/pcorrs_responses.py
@@ -0,0 +1,24 @@
+"""Functions and classes that deal with responses and conversion to JSON."""
+import json
+
+from flask import make_response
+
+class OutputEncoder(json.JSONEncoder):
+    """
+    Class to encode output into JSON, for objects which the default
+    json.JSONEncoder class does not have default encoding for.
+    """
+    def default(self, o):
+        if isinstance(o, bytes):
+            return str(o, encoding="utf-8")
+        return json.JSONEncoder.default(self, o)
+
+def build_response(data):
+    """Build the responses for the API"""
+    status_codes = {
+        "error": 400, "not-found": 404, "success": 200, "exception": 500}
+    response = make_response(
+            json.dumps(data, cls=OutputEncoder),
+            status_codes[data["status"]])
+    response.headers["Content-Type"] = "application/json"
+    return response