| 1 |
.. currentmodule:: aiorpcx |
| 2 |
|
| 3 |
JSON RPC |
| 4 |
======== |
| 5 |
|
| 6 |
The :mod:`aiorpcx` module provides classes to interpret and construct |
| 7 |
JSON RPC protocol messages. Class instances are not used; all methods |
| 8 |
are class methods. Just call methods on the classes directly. |
| 9 |
|
| 10 |
.. class:: JSONRPC |
| 11 |
|
| 12 |
An abstract base class for concrete protocol classes. |
| 13 |
:class:`JSONRPCv1` and :class:`JSONRPCv2` are derived protocol classes |
| 14 |
implementing JSON RPC versions 1.0 and 2.0 in a strict way. |
| 15 |
|
| 16 |
.. class:: JSONRPCv1 |
| 17 |
|
| 18 |
A derived class of :class:`JSONRPC` implementing version 1.0 of the |
| 19 |
specification. |
| 20 |
|
| 21 |
.. class:: JSONRPCv2 |
| 22 |
|
| 23 |
A derived class of :class:`JSONRPC` implementing version 2.0 of the |
| 24 |
specification. |
| 25 |
|
| 26 |
.. class:: JSONRPCLoose |
| 27 |
|
| 28 |
A derived class of :class:`JSONRPC`. It accepts messages that |
| 29 |
conform to either version 1.0 or version 2.0. As it is loose, it |
| 30 |
will also accept messages that conform strictly to neither version. |
| 31 |
|
| 32 |
Unfortunately it is not possible to send messages that are |
| 33 |
acceptable to strict implementations of both versions 1.0 and 2.0, |
| 34 |
so it sends version 2.0 messages. |
| 35 |
|
| 36 |
.. class:: JSONRPCAutoDetect |
| 37 |
|
| 38 |
Auto-detects the JSON RPC protocol version spoken by the remote side |
| 39 |
based on the first incoming message, from :class:`JSONRPCv1`, |
| 40 |
:class:`JSONRPCv2` and :class:`JSONRPCLoose`. The RPC processor |
| 41 |
will then switch to that protocol version. |
| 42 |
|
| 43 |
|
| 44 |
Message interpretation |
| 45 |
---------------------- |
| 46 |
|
| 47 |
.. classmethod:: JSONRPC.message_to_item(message) |
| 48 |
|
| 49 |
Convert a binary message into an RPC object describing the message |
| 50 |
and return it. |
| 51 |
|
| 52 |
:param bytes message: the message to interpret |
| 53 |
:return: the RPC object |
| 54 |
:rtype: :class:`RPCRequest`, :class:`RPCResponse` or |
| 55 |
:class:`RPCBatch`. |
| 56 |
|
| 57 |
If the message is ill-formed, return an :class:`RPCRequest` object |
| 58 |
with its :attr:`method` set to an :class:`RPCError` instance |
| 59 |
describing the error. |
| 60 |
|
| 61 |
|
| 62 |
Message construction |
| 63 |
-------------------- |
| 64 |
|
| 65 |
These functions convert an RPC item into a binary message that can be |
| 66 |
passed over the network after framing. |
| 67 |
|
| 68 |
.. classmethod:: JSONRPC.request_message(item) |
| 69 |
|
| 70 |
Convert a request item to a message. |
| 71 |
|
| 72 |
:param item: an :class:`RPCRequest` item |
| 73 |
:return: the message |
| 74 |
:rtype: bytes |
| 75 |
|
| 76 |
.. classmethod:: JSONRPC.response_message(item) |
| 77 |
|
| 78 |
Convert a response item to a message. |
| 79 |
|
| 80 |
:param item: an :class:`RPCResponse` item |
| 81 |
:return: the message |
| 82 |
:rtype: bytes |
| 83 |
|
| 84 |
.. classmethod:: JSONRPC.error_message(item) |
| 85 |
|
| 86 |
Convert an error item to a message. |
| 87 |
|
| 88 |
:param item: an :class:`RPCError` item |
| 89 |
:return: the message |
| 90 |
:rtype: bytes |
| 91 |
|
| 92 |
.. classmethod:: JSONRPC.batch_message(item) |
| 93 |
|
| 94 |
Convert a batch item to a message. |
| 95 |
|
| 96 |
:param item: an :class:`RPCBatch` item |
| 97 |
:return: the message |
| 98 |
:rtype: bytes |
| 99 |
|
| 100 |
.. classmethod:: JSONRPC.encode_payload(payload) |
| 101 |
|
| 102 |
Encode a Python object as a JSON string and convert it to bytes. |
| 103 |
If the object cannot be encoded as JSON, a JSON "internal error" |
| 104 |
error message is returned instead, with ID equal to the "id" member |
| 105 |
of `payload` if that is a dictionary, otherwise :const:`None`. |
| 106 |
|
| 107 |
:param payload: a Python object that can be represented as JSON. |
| 108 |
Numbers, strings, lists, dictionaries, |
| 109 |
:const:`True`, :const:`False` and :const:`None` are |
| 110 |
all valid. |
| 111 |
:return: a JSON message |
| 112 |
:rtype: bytes |