Debian Codegraph @

docs/rpc.rst

raw
1 .. currentmodule:: aiorpcx
2
3 RPC items
4 =========
5
6 The :mod:`aiorpcx` module defines some classes, instances of which
7 will be returned by some of its APIs. You should not need to
8 instantiate these objects directly.
9
10 An instance of one of these classes is called an :dfn:`item`.
11
12
13 .. class:: RPCRequest
14
15 An RPC request or notification that has been received, or an
16 outgoing notification.
17
18 Outgoing requests are represented by :class:`RPCRequestOut` objects.
19
20 .. attribute:: method
21
22 The RPC method being invoked, a string.
23
24 If an incoming request is ill-formed, so that, e.g., its method
25 could not be determined, then this will be an :class:`RPCError`
26 instance that describes the error.
27
28 .. attribute:: args
29
30 The arguments passed to the RPC method. This is a list or a
31 dictionary, a dictionary if the arguments were passed by
32 parameter name.
33
34 .. attribute:: request_id
35
36 The ID given to the request so that responses can be associated
37 with requests. Normally an integer, or :const:`None` if the
38 request is a :dfn:`notification`. Rarely it might be a floating
39 point number or string.
40
41 .. method:: is_notification()
42
43 Returns :const:`True` if the request is a notification (its
44 :attr:`request_id` is :const:`None`), otherwise :const:`False`.
45
46
47 .. class:: RPCRequestOut
48
49 An outgoing RPC request that is not a notification. A subclass of
50 :class:`RPCRequest` and :class:`asyncio.Future
51 <https://docs.python.org/3/library/asyncio-task.html#asyncio.Future>`.
52
53 When an outgoing request is created, typically via the
54 :meth:`send_request` method of a client or server session, you can
55 specify a callback to be called when the request is done. The
56 callback is passed the request object, and the result can be
57 obtained via its :meth:`result` method.
58
59 A request can also be await-ed. Currently the result of await-ing
60 is the same as calling :meth:`result` on the request but this may
61 change in future.
62
63
64 .. class:: RPCResponse
65
66 An incoming or outgoing response. Outgoing response objects are
67 automatically created by the framework when a request handler
68 returns its result.
69
70 .. attribute:: result
71
72 The response result, a Python object. If an error occurred this
73 will be an :class:`RPCError` object describing the error.
74
75 .. attribute:: request_id
76
77 The ID of the request this is a repsonse to. Notifications do
78 not get responses so this will never be :const:`None`.
79
80 If :attr:`result` in an :class:`RPCError` their
81 :attr:`request_id` attributes will match.
82
83
84 .. class:: RPCError
85
86 Represents an error, either in an :class:`RPCResponse` object if an
87 error occurred processing a request, or in a :class:`RPCRequest` if
88 an incoming request was ill-formed.
89
90 .. attribute:: message
91
92 The error message as a string.
93
94 .. attribute:: code
95
96 The error code, an integer.
97
98 .. attribute:: request_id
99
100 The ID of the request that gave an error if it could be
101 determined, otherwise :const:`None`.
102
103
104 .. class:: RPCBatch
105
106 Represents an incoming or outgoing RPC response batch, or an
107 incoming RPC request batch.
108
109 .. attribute:: items
110
111 A list of the items in the batch. The list cannot be empty, and
112 each item will be an :class:`RPCResponse` object for a response
113 batch, and an :class:`RPCRequest` object for a request batch.
114
115 Notifications and requests can be mixed together.
116
117 Batches are iterable through their items, and taking their length
118 returns the length of the items list.
119
120 .. method:: requests
121
122 A generator that yields non-notification items of a request
123 batch, or each item for a response batch.
124
125 .. method:: request_ids
126
127 A *frozenset* of all request IDs in the batch, ignoring
128 notifications.
129
130 .. method:: is_request_batch
131
132 Return :const:`True` if the batch is a request batch.
133
134
135 .. class:: RPCBatchOut
136
137 An outgoing RPC batch. A subclass of :class:`RPCBatch` and
138 :class:`asyncio.Future
139 <https://docs.python.org/3/library/asyncio-task.html#asyncio.Future>`.
140
141 When an outgoing request batch is created, typically via the
142 :meth:`new_batch` method of a client or server session, you can
143 specify a callback to be called when the batch is done. The
144 callback is passed the batch object.
145
146 Each non-notification item in an :class:`RPCBatchOut` object is
147 itself an :class:`RPCRequestOut` object that can be independently
148 waited on or cancelled. Notification items are :class:`RPCRequest`
149 objects. Since batches are responded to as a whole, all member
150 requests will be completed simultaneously. The order of callbacks
151 of member requests, and of the batch itself, is unspecified.
152
153 Cancelling a batch, or calling its :meth:`set_result` or
154 :meth:`set_exception` methods cancels all its requests.
155
156 .. method:: add_request(method, args=None, on_done=None)
157
158 Add a request to the batch. A callback can be specified that will
159 be called when the request completes. Returns the
160 :class:`RPCRequestOut` request that was added to the batch.
161
162 .. method:: add_notification(method, args=None)
163
164 Add a notification to the batch.
165
166
167 RPC Protocol Classes
168 --------------------
169
170 RPC protocol classes should inherit from :class:`RPCProtocolBase`.
171 The base class provides a few utility functions returning
172 :class:`RPCError` objects. The derived class should redefine some
173 constant class attributes.
174
175
176 .. class:: RPCProtocolBase
177
178
179 .. attribute:: INTERNAL_ERROR
180
181 The integer error code to use for an internal error.
182
183 .. attribute:: INVALID_ARGS
184
185 The integer error code to use when an RPC request passes invalid
186 arguments.
187
188 .. attribute:: INVALID_REQUEST
189
190 The integer error code to use when an RPC request is invalid.
191
192 .. attribute:: METHOD_NOT_FOUND
193
194 The integer error code to use when an RPC request is for a
195 non-existent method.
196
197 .. classmethod:: JSONRPC.internal_error(request_id)
198
199 Return an :class:`RPCError` object with error code
200 :attr:`INTERNAL_ERROR` for the given request ID. The error message
201 will be ``"internal error processing request"``.
202
203 :param request_id: the request ID, normally an integer or string
204 :return: the error object
205 :rtype: :class:`RPCError`
206
207 .. classmethod:: JSONRPC.args_error(message)
208
209 Return an :class:`RPCError` object with error code
210 :attr:`INVALID_ARGS` with the given error message and a request ID
211 of :const:`None`.
212
213 :param str message: the error message
214 :return: the error object
215 :rtype: :class:`RPCError`
216
217 .. classmethod:: JSONRPC.invalid_request(message, request_id=None)
218
219 Return an :class:`RPCError` object with error code
220 :attr:`INVALID_REQUEST` with the given error message and
221 request ID.
222
223 :param str message: the error message
224 :param request_id: the request ID, normally an integer or string
225 :return: the error object
226 :rtype: :class:`RPCError`
227
228 .. classmethod:: JSONRPC.method_not_found(message)
229
230 Return an :class:`RPCError` object with error code
231 :attr:`METHOD_NOT_FOUND` with the given error message and a request
232 ID :const:`None`.
233
234 :param str message: the error message
235 :return: the error object
236 :rtype: :class:`RPCError`