KaruMediaServer/api/karumedia/tools.py

53 lines
1.4 KiB
Python
Raw Normal View History

2016-11-01 22:26:29 +02:00
import json
from falcon import Request as FalconRequest
from falcon import Response as FalconResponse
from falcon.errors import HTTPBadRequest, HTTPMissingParam, HTTPError
import falcon.status_codes as status
class JsonRequest(FalconRequest):
2016-11-01 23:17:23 +02:00
#__slots__ = set(FalconRequest.__slots__ + ("_json", "_args"))
2016-11-01 22:26:29 +02:00
@property
def json(self):
if not hasattr(self, "_json"):
if not self.client_accepts_json:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports the JSON formated data')
try:
self._json = json.loads(self.stream.read().decode('utf8'))
except json.decoder.JSONDecodeError as err:
raise HTTPBadRequest("JSONDecodeError", str(err))
return self._json
class JsonResponse(FalconResponse):
2016-11-01 23:17:23 +02:00
#__slots__ = set(FalconRequest.__slots__ + ("_json",))
2016-11-01 22:26:29 +02:00
@property
def json(self):
return self._json
@json.setter
def json(self, value):
self._json = value
self.body = json.dumps(value, indent=4)
def error_handler(ex, req, resp, params):
raise HTTPBadRequest(type(ex).__name__, str(ex))
class TODOException(HTTPError):
def __init__(self, **kwargs):
super(TODOException, self).__init__(status.HTTP_NOT_IMPLEMENTED, **kwargs)
@property
def has_representation(self):
return False