Skip to content

Commit

Permalink
Merge pull request #646 from playpauseandstop/master
Browse files Browse the repository at this point in the history
Rename `loader` keyword argument in `web.Request.json` method.
  • Loading branch information
asvetlov committed Jan 30, 2016
2 parents c443d50 + 78fa7c3 commit 22a00ea
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 7 additions & 2 deletions aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,15 @@ def text(self):
return bytes_body.decode(encoding)

@asyncio.coroutine
def json(self, *, loader=json.loads):
def json(self, *, loads=json.loads, loader=None):
"""Return BODY as JSON."""
if loader:
warnings.warn(
'Using `loader` is deprecated, use `loads` instead',
DeprecationWarning)
loads = loader
body = yield from self.text()
return loader(body)
return loads(body)

@asyncio.coroutine
def post(self):
Expand Down
4 changes: 2 additions & 2 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ like one using :meth:`Request.copy`.
The method **does** store read data internally, subsequent
:meth:`~Request.text` call will return the same value.

.. coroutinemethod:: json(*, loader=json.loads)
.. coroutinemethod:: json(*, loads=json.loads)

Read request body decoded as *json*.

The method is just a boilerplate :ref:`coroutine <coroutine>`
implemented as::

async def json(self, *, loader=json.loads):
async def json(self, *, loads=json.loads):
body = await self.text()
return loader(body)

Expand Down
4 changes: 3 additions & 1 deletion tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ def test_post_json(self):
def handler(request):
data = yield from request.json()
self.assertEqual(dct, data)
data2 = yield from request.json()
data2 = yield from request.json(loads=json.loads)
self.assertEqual(data, data2)
data3 = yield from request.json(loader=json.loads)
self.assertEqual(data, data3)
resp = web.Response()
resp.content_type = 'application/json'
resp.body = json.dumps(data).encode('utf8')
Expand Down

0 comments on commit 22a00ea

Please sign in to comment.