import json
class JSONMixin:
MIMETYPE = 'application/json'
CORS_ORIGIN = None
CORS_METHODS = None
CORS_HEADERS = None
CORS_CREDENTIALS = None
def headers (self):
if self.CORS_ORIGIN:
yield 'Access-Control-Allow-Origin', self.CORS_ORIGIN
if self.CORS_METHODS:
yield 'Access-Control-Allow-Methods', ', '.join(self.CORS_METHODS)
if self.CORS_HEADERS:
yield 'Access-Control-Allow-Headers', ', '.join(self.CORS_HEADERS)
if self.CORS_CREDENTIALS:
yield 'Access-Control-Allow-Credentials', 'true'
def render_json(self, **parms):
raise NotImplementedError()
def render_response(self, **params):
data = self.render_json(**params)
return json.dumps(data)