-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathcontent.py
54 lines (42 loc) · 1.44 KB
/
content.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import json
from flask import make_response
from flask.wrappers import Response as FlaskResponse
from werkzeug.wrappers import Response
from flask_rest_jsonapi.utils import JSONEncoder
def parse_json(request):
"""
Default content parser for JSON
"""
return request.json
def render_json(response):
"""
Default content renderer for JSON
"""
headers = {'Content-Type': 'application/vnd.api+json'}
if isinstance(response, Response):
response.headers.add('Content-Type', 'application/vnd.api+json')
return response
if not isinstance(response, tuple):
if isinstance(response, dict):
response.update({'jsonapi': {'version': '1.0'}})
return make_response(json.dumps(response, cls=JSONEncoder), 200, headers)
try:
data, status_code, headers = response
headers.update({'Content-Type': 'application/vnd.api+json'})
except ValueError:
pass
try:
data, status_code = response
except ValueError:
pass
if isinstance(data, dict):
data.update({'jsonapi': {'version': '1.0'}})
if isinstance(data, FlaskResponse):
data.headers.add('Content-Type', 'application/vnd.api+json')
data.status_code = status_code
return data
elif isinstance(data, str):
json_reponse = data
else:
json_reponse = json.dumps(data, cls=JSONEncoder)
return make_response(json_reponse, status_code, headers)