mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-16 21:37:59 +08:00
41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
import json
|
|
|
|
from numpy import float32, integer
|
|
from flask import make_response, jsonify, Response
|
|
|
|
|
|
class Float32JSONEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, float32):
|
|
return float(obj)
|
|
elif isinstance(obj, integer):
|
|
return int(obj)
|
|
return json.JSONEncoder.default(self, obj)
|
|
|
|
|
|
def make_payload(data, errormessage="", errorcode=200):
|
|
"""
|
|
Creates JSON respons for requests
|
|
:param data: json data
|
|
:param errormessage: error message
|
|
:param errorcode: http error code
|
|
:return: flask json repsonse
|
|
"""
|
|
error = False
|
|
if errormessage:
|
|
error = True
|
|
# Questionable
|
|
data = json.loads(json.dumps(data, cls=Float32JSONEncoder))
|
|
return make_response(jsonify({
|
|
"data": data,
|
|
"status": {
|
|
"error": error,
|
|
"errormessage": errormessage,
|
|
}
|
|
}), errorcode)
|
|
|
|
|
|
def make_streaming_response(data_generator, errorcode=200, content_type="application/json"):
|
|
# TODO headers
|
|
return Response(data_generator, status=errorcode, content_type=content_type)
|