Skip to content

Commit

Permalink
Asyncronous validation
Browse files Browse the repository at this point in the history
  • Loading branch information
juntatalor committed Dec 28, 2016
1 parent b863e00 commit f906060
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tcrudge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
Supports JSON and MessagePack responses.
"""

__version__ = "0.7.0"
__version__ = "0.8.0"

23 changes: 10 additions & 13 deletions tcrudge/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def write_error(self, status_code, **kwargs):
self.write(getattr(exc_info[1], 'body', None))
self.finish()

def validate(self, data, schema):
async def validate(self, data, schema):
"""
Method to validate parameters.
Raises HTTPError(400) with error info for invalid data.
Expand Down Expand Up @@ -617,9 +617,8 @@ async def get(self):
:raises: In case of bad query parameters - HTTP 400.
"""
self.validate({k: self.get_argument(k) for k in
self.request.query_arguments.keys()},
self.get_schema_input)
await self.validate({k: self.get_argument(k) for k in self.request.query_arguments.keys()},
self.get_schema_input)
try:
qs = self.get_queryset()
except AttributeError as e:
Expand Down Expand Up @@ -652,9 +651,8 @@ async def head(self):
:raises: In case of bad query parameters - HTTPError 400.
"""
self.validate({k: self.get_argument(k) for k in
self.request.query_arguments.keys()},
self.get_schema_input)
await self.validate({k: self.get_argument(k) for k in self.request.query_arguments.keys()},
self.get_schema_input)
try:
qs = self.get_queryset(paginate=False)
except AttributeError as e:
Expand Down Expand Up @@ -686,7 +684,7 @@ async def post(self):
:raises: HTTPError 405, 400
"""
data = self.validate(self.request.body, self.post_schema_input)
data = await self.validate(self.request.body, self.post_schema_input)
try:
item = await self.model_cls._create(self.application, data)
except AttributeError as e:
Expand Down Expand Up @@ -814,9 +812,8 @@ async def get(self, item_id):
1. Validates request.
2. Writes serialized object of ORM model instance to response.
"""
self.validate({k: self.get_argument(k) for k in
self.request.query_arguments.keys()},
self.get_schema_input)
await self.validate({k: self.get_argument(k) for k in self.request.query_arguments.keys()},
self.get_schema_input)
item = await self.get_item(item_id)

self.response(result=await self.serialize(item))
Expand All @@ -838,7 +835,7 @@ async def put(self, item_id):
"""
item = await self.get_item(item_id)

data = self.validate(self.request.body, self.put_schema_input)
data = await self.validate(self.request.body, self.put_schema_input)
try:
item = await item._update(self.application, data)
except AttributeError as e:
Expand Down Expand Up @@ -884,7 +881,7 @@ async def delete(self, item_id):
:raises: HTTPError 405 if model object is not deletable.
"""
# DELETE usually does not have body to validate.
self.validate(self.request.body or {}, self.delete_schema_input)
await self.validate(self.request.body or {}, self.delete_schema_input)
item = await self.get_item(item_id)
try:
# We can only delete item if model method _delete() is implemented
Expand Down

0 comments on commit f906060

Please sign in to comment.