Skip to content

Commit

Permalink
Fix for (aio-libs#976): add documentation for websocket send_json and…
Browse files Browse the repository at this point in the history
… receive_json
  • Loading branch information
Vitalie Maldur committed Jul 23, 2016
1 parent 66c2c00 commit 524669e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,22 @@ manually.
:raise TypeError: if data is not :class:`bytes`,
:class:`bytearray` or :class:`memoryview`.

.. method:: send_json(data, *, dumps=json.loads)

Send *data* to peer as JSON string.

:param data: data to send.

:param callable dumps: any :term:`callable` that accepts an object and
returns a JSON string
(:func:`json.dumps` by default).

:raise RuntimeError: if connection is not started or closing

:raise ValueError: if data is not serializable object

:raise TypeError: if value returned by :term:`dumps` is not :class:`str`

.. comethod:: close(*, code=1000, message=b'')

A :ref:`coroutine<coroutine>` that initiates closing handshake by sending
Expand Down Expand Up @@ -1284,6 +1300,41 @@ manually.
:return: :class:`~aiohttp.websocket.Message`, `tp` is types of
`~aiohttp.MsgType`

.. coroutinemethod:: receive_str()

A :ref:`coroutine<coroutine>` that calls :meth:`receive` but
also asserts the message type is
:const:`~aiohttp.websocket.MSG_TEXT`.

:return str: peer's message content.

:raise TypeError: if message is :const:`~aiohttp.websocket.MSG_BINARY`.

.. coroutinemethod:: receive_bytes()

A :ref:`coroutine<coroutine>` that calls :meth:`receive` but
also asserts the message type is
:const:`~aiohttp.websocket.MSG_BINARY`.

:return bytes: peer's message content.

:raise TypeError: if message is :const:`~aiohttp.websocket.MSG_TEXT`.

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

A :ref:`coroutine<coroutine>` that calls :meth:`receive`, asserts the
message type is :const:`~aiohttp.websocket.MSG_TEXT`, and loads the JSON
string to a Python dict.

:param callable loads: any :term:`callable` that accepts
:class:`str` and returns :class:`dict`
with parsed JSON (:func:`json.loads` by
default).

:return dict: loaded JSON content

:raise TypeError: if message is :const:`~aiohttp.websocket.MSG_BINARY`.
:raise ValueError: if message is not valid JSON.

Utilities
---------
Expand Down
16 changes: 16 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,22 @@ WebSocketResponse
:raise TypeError: if data is not :class:`bytes`,
:class:`bytearray` or :class:`memoryview`.

.. method:: send_json(data, *, dumps=json.loads)

Send *data* to peer as JSON string.

:param data: data to send.

:param callable dumps: any :term:`callable` that accepts an object and
returns a JSON string
(:func:`json.dumps` by default).

:raise RuntimeError: if connection is not started or closing

:raise ValueError: if data is not serializable object

:raise TypeError: if value returned by :term:`dumps` is not :class:`str`

.. coroutinemethod:: close(*, code=1000, message=b'')

A :ref:`coroutine<coroutine>` that initiates closing
Expand Down

0 comments on commit 524669e

Please sign in to comment.