make method instead of property for alternative json decoding of request

This commit is contained in:
Hugh McNamara 2017-09-19 18:12:53 +01:00
parent 1d719252cb
commit a8f764c161

View File

@ -69,10 +69,10 @@ class Request(dict):
self.stream = None self.stream = None
@property @property
def json(self, loads=json_loads): def json(self):
if self.parsed_json is None: if self.parsed_json is None:
try: try:
self.parsed_json = loads(self.body) self.parsed_json = json_loads(self.body)
except Exception: except Exception:
if not self.body: if not self.body:
return None return None
@ -80,6 +80,16 @@ class Request(dict):
return self.parsed_json return self.parsed_json
def load_json(self, loads=json_loads):
try:
self.parsed_json = loads(self.body)
except Exception:
if not self.body:
return None
raise InvalidUsage("Failed when parsing body as json")
return self.parsed_json
@property @property
def token(self): def token(self):
"""Attempt to return the auth header token. """Attempt to return the auth header token.