diff options
| author | LV-426 <lv-426@taproot.org.il> | 2015-01-13 18:09:44 +0200 |
|---|---|---|
| committer | LV-426 <lv-426@taproot.org.il> | 2015-01-13 18:09:44 +0200 |
| commit | 1983d480c3f7d818926a38c3419aecc4b69a00fc (patch) | |
| tree | 595e475155fb4f54aaa43b1df6fd9af764def5e7 /src/server/rz_req_handling.py | |
| parent | 233d9de48df7e32701500177203b36c5b27c2a1e (diff) | |
wip: rz_req_handling.py
Diffstat (limited to 'src/server/rz_req_handling.py')
| -rw-r--r-- | src/server/rz_req_handling.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/server/rz_req_handling.py b/src/server/rz_req_handling.py new file mode 100644 index 00000000..f0999e3e --- /dev/null +++ b/src/server/rz_req_handling.py @@ -0,0 +1,42 @@ +from flask import jsonify +from flask import make_response + +def make_json_response(status=200, data='{}'): + """ + Construct a json response with proper content-type header + """ + resp = make_response(data) + resp.headers['Content-Type'] = "application/json" + resp.status = status + return resp + +def common_resp_handle(data=None, error=None): + """ + common response handling: + - add common response headers + - serialize response + + @data must be json serializable + @error will be serialized with str() + """ + + def __response_wrap(data=None, error=None): + """ + wrap response data/errors as dict - this should always be used when returning + data to allow easy return of list objects, assist in error case distinction, etc. + """ + return dict(data=data, error=error) + + if error is None: + error_str = "" + else: + error_str = str(error) # convert any Exception objects to serializable form + + ret_data = __response_wrap(data, error_str) + resp = jsonify(ret_data) # this will create a Flask Response object + + resp.headers['Access-Control-Allow-Origin'] = '*' + + # more response processing + + return resp |
