Web service API für Green Spider
https://github.com/netzbegruenung/green-spider
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
import six |
|
|
|
from datetime import date, datetime |
|
|
|
from falcon import errors |
|
from falcon.media import BaseHandler |
|
from falcon.util import json |
|
|
|
class ComplexEncoder(json.JSONEncoder): |
|
|
|
"""JSONENcoder that handles date and datetime""" |
|
|
|
def default(self, obj): |
|
if isinstance(obj, date) or isinstance(obj, datetime): |
|
return obj.isoformat() |
|
# Let the base class default method raise the TypeError |
|
return json.JSONEncoder.default(self, obj) |
|
|
|
class JSONHandler(BaseHandler): |
|
"""Handler built using Python's :py:mod:`json` module.""" |
|
|
|
def deserialize(self, raw): |
|
try: |
|
return json.loads(raw.decode('utf-8')) |
|
except ValueError as err: |
|
raise errors.HTTPBadRequest( |
|
'Invalid JSON', |
|
'Could not parse JSON body - {0}'.format(err) |
|
) |
|
|
|
def serialize(self, media): |
|
result = json.dumps(media, |
|
ensure_ascii=False, |
|
cls=ComplexEncoder) |
|
if six.PY3 or not isinstance(result, bytes): |
|
return result.encode('utf-8') |
|
|
|
return result
|
|
|