Jsonrpc.Client¶
- Subclasses:
None
Methods¶
- Inherited:
- Structs:
class |
|
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Virtual Methods¶
- Inherited:
|
|
|
|
|
Properties¶
Name |
Type |
Flags |
Short Description |
---|---|---|---|
w/co |
The stream to communicate over |
||
r/w |
If |
Signals¶
- Inherited:
Name |
Short Description |
---|---|
The “failed” signal is called when the client has failed communication or the connection has been knowingly closed. |
|
This signal is emitted when an RPC has been received from the peer we are connected to. |
|
This signal is emitted when a notification has been received from a peer. |
Fields¶
- Inherited:
Name |
Type |
Access |
Description |
---|---|---|---|
parent_instance |
r |
Class Details¶
- class Jsonrpc.Client(**kwargs)¶
- Bases:
- Abstract:
No
- Structure:
A client for JSON-RPC communication
The
Jsonrpc.Client
class provides a convenient API to coordinate with a JSON-RPC server. You can provide the underlying [class`Gio`.IOStream] to communicate with allowing you to control the negotiation of how you setup your communications channel. One such method might be to use a [class`Gio`.Subprocess] and communicate over stdin and stdout.Because JSON-RPC allows for out-of-band notifications from the server to the client, it is important that the consumer of this API calls [method`Client`.close] or [method`Client`.close_async] when they no longer need the client. This is because
Jsonrpc.Client
contains an asynchronous read-loop to process incoming messages. Until [method`Client`.close] or [method`Client`.close_async] have been called, this read loop will prevent the object from finalizing (being freed).To make an RPC call, use [method`Client`.call] or [method`Client`.call_async] and provide the method name and the parameters as a [struct`GLib`.Variant] for call.
It is a programming error to mix synchronous and asynchronous API calls of the
Jsonrpc.Client
class.For synchronous calls,
Jsonrpc.Client
will use the thread-default [struct`GLib`.MainContext]. If you have special needs here ensure you’ve set the context before calling into anyJsonrpc.Client
API.New in version 3.26.
- classmethod new(io_stream)¶
- Parameters:
io_stream (
Gio.IOStream
) – A [class`Gio`.IOStream]- Returns:
A newly created
Jsonrpc.Client
- Return type:
Creates a new
Jsonrpc.Client
instance.If you want to communicate with a process using stdin/stdout, consider using [class`Gio`.Subprocess] to launch the process and create a [class`Gio`.SimpleIOStream] using the [method`Gio`.Subprocess.get_stdin_pipe] and [method`Gio`.Subprocess.get_stdout_pipe].
New in version 3.26.
- call(method, params, cancellable)¶
- Parameters:
method (
str
) – The name of the method to callparams (
GLib.Variant
orNone
) – A [struct`GLib`.Variant] of parameters orNone
cancellable (
Gio.Cancellable
orNone
) – AGio.Cancellable
orNone
- Raises:
- Returns:
True
on success; otherwiseFalse
and error is set.- return_value:
A location for a [struct`GLib`.Variant]
- Return type:
(
bool
, return_value:GLib.Variant
orNone
)
Synchronously calls method with params on the remote peer.
once a reply has been received, or failure, this function will return. If successful, return_value will be set with the reslut field of the response.
If params is floating then this function consumes the reference.
New in version 3.26.
- call_async(method, params, cancellable, callback, *user_data)¶
- Parameters:
method (
str
) – The name of the method to callparams (
GLib.Variant
orNone
) – A [struct`GLib`.Variant] of parameters orNone
cancellable (
Gio.Cancellable
orNone
) – AGio.Cancellable
orNone
callback (
Gio.AsyncReadyCallback
orNone
) – a callback to executed upon completion
Asynchronously calls method with params on the remote peer.
Upon completion or failure, callback is executed and it should call [method`Client`.call_finish] to complete the request and release any memory held.
If params is floating, the floating reference is consumed.
New in version 3.26.
- call_finish(result)¶
- Parameters:
result (
Gio.AsyncResult
) – AGio.AsyncResult
provided to the callback in [method`Client`.call_async]- Raises:
- Returns:
True
if successful and return_value is set, otherwiseFalse
and error is set.- return_value:
A location for a [struct`GLib`.Variant] or
None
- Return type:
(
bool
, return_value:GLib.Variant
orNone
)
Completes an asynchronous call to [method`Client`.call_async].
New in version 3.26.
- call_with_id_async(method, params, cancellable, callback, *user_data)¶
- Parameters:
method (
str
) – The name of the method to callparams (
GLib.Variant
orNone
) – A [struct`GLib`.Variant] of parameters orNone
cancellable (
Gio.Cancellable
orNone
) – AGio.Cancellable
orNone
callback (
Gio.AsyncReadyCallback
orNone
) – Callback to executed upon completion
- Returns:
A location for a [struct`GLib`.Variant] describing the identifier used for the method call, or
None
.- Return type:
id:
GLib.Variant
Asynchronously calls method with params on the remote peer.
Upon completion or failure, callback is executed and it should call [method`Client`.call_finish] to complete the request and release any memory held.
This function is similar to [method`Client`.call_async] except that it allows the caller to get the id of the command which might be useful in systems where you can cancel the operation (such as the Language Server Protocol).
If params is floating, the floating reference is consumed.
New in version 3.30.
- close(cancellable)¶
- Parameters:
cancellable (
Gio.Cancellable
orNone
) –- Raises:
- Returns:
- Return type:
Closes the underlying streams and cancels any inflight operations of the
Jsonrpc.Client
.This is important to call when you are done with the client so that any outstanding operations that have caused self to hold additional references are cancelled.
Failure to call this method results in a leak of
Jsonrpc.Client
.New in version 3.26.
- close_async(cancellable, callback, *user_data)¶
- Parameters:
cancellable (
Gio.Cancellable
orNone
) –callback (
Gio.AsyncReadyCallback
orNone
) –
Asynchronous version of [method`Client`.close].
Currently this operation is implemented synchronously, but in the future may be converted to using asynchronous operations.
New in version 3.26.
- close_finish(result)¶
- Parameters:
result (
Gio.AsyncResult
) –- Raises:
- Returns:
- Return type:
Completes an asynchronous request of [method`Client`.close_async].
New in version 3.26.
- get_use_gvariant()¶
-
Gets the [property`Client`:py:data::use-gvariant<Jsonrpc.Client.props.use_gvariant>] property.
Indicates if [struct`GLib`.Variant] is being used to communicate with the peer.
New in version 3.26.
- reply(id, result, cancellable)¶
- Parameters:
id (
GLib.Variant
) – The id of the message to replyresult (
GLib.Variant
orNone
) – The return value orNone
cancellable (
Gio.Cancellable
orNone
) – AGio.Cancellable
, orNone
- Raises:
- Return type:
Synchronous variant of [method`Client`.reply_async].
If id or result are floating, there floating references are consumed.
New in version 3.26.
- reply_async(id, result, cancellable, callback, *user_data)¶
- Parameters:
id (
GLib.Variant
) –result (
GLib.Variant
) –cancellable (
Gio.Cancellable
orNone
) –callback (
Gio.AsyncReadyCallback
orNone
) –
- reply_error_async(id, code, message, cancellable, callback, *user_data)¶
- Parameters:
id (
GLib.Variant
) – A [struct`GLib`.Variant] containing the call idcode (
int
) – The error codecancellable (
Gio.Cancellable
orNone
) – AGio.Cancellable
, orNone
callback (
Gio.AsyncReadyCallback
orNone
) – AGio.AsyncReadyCallback
orNone
Asynchronously replies to the peer, sending a JSON-RPC error message.
Call [method`Client`.reply_error_finish] to get the result of this operation.
If id is floating, it’s floating reference is consumed.
New in version 3.28.
- reply_error_finish(result)¶
- Parameters:
result (
Gio.AsyncResult
) –- Raises:
- Return type:
- reply_finish(result)¶
- Parameters:
result (
Gio.AsyncResult
) – AGio.AsyncResult
- Raises:
- Returns:
- Return type:
Completes an asynchronous request to [method`Client`.reply_async].
New in version 3.26.
- send_notification(method, params, cancellable)¶
- Parameters:
method (
str
) – The name of the method to callparams (
GLib.Variant
orNone
) – A [struct`GLib`.Variant] of parameters orNone
cancellable (
Gio.Cancellable
orNone
) – AGio.Cancellable
orNone
- Raises:
- Returns:
- Return type:
Synchronously calls method with params on the remote peer.
This function will not wait or expect a reply from the peer.
If params is floating then the reference is consumed.
New in version 3.26.
- send_notification_async(method, params, cancellable, callback, *user_data)¶
- Parameters:
method (
str
) – The name of the method to callparams (
GLib.Variant
orNone
) – A [struct`GLib`.Variant] of parameters orNone
cancellable (
Gio.Cancellable
orNone
) – AGio.Cancellable
orNone
callback (
Gio.AsyncReadyCallback
orNone
) –
Asynchronously calls method with params on the remote peer.
This function will not wait or expect a reply from the peer.
This function is useful when the caller wants to be notified that the bytes have been delivered to the underlying stream. This does not indicate that the peer has received them.
If params is floating then the reference is consumed.
New in version 3.26.
- send_notification_finish(result)¶
- Parameters:
result (
Gio.AsyncResult
) –- Raises:
- Returns:
True
if the bytes have been flushed to the [class`Gio`.IOStream]; otherwiseFalse
and error is set.- Return type:
Completes an asynchronous call to [method`Client`.send_notification_async].
Successful completion of this function only indicates that the request has been written to the underlying buffer, not that the peer has received the notification.
New in version 3.26.
- set_use_gvariant(use_gvariant)¶
- Parameters:
use_gvariant (
bool
) – If [struct`GLib`.Variant] should be used
Sets the [property`Client`:py:data::use-gvariant<Jsonrpc.Client.props.use_gvariant>] property.
This function sets if [struct`GLib`.Variant] should be used to communicate with the peer. Doing so can allow for more efficient communication by avoiding expensive parsing overhead and memory allocations. However, it requires that the peer also supports [struct`GLib`.Variant] encoding.
New in version 3.26.
- start_listening()¶
This function requests that client start processing incoming messages from the peer.
New in version 3.26.
- do_failed() virtual¶
- do_handle_call(method, id, params) virtual¶
- Parameters:
method (
str
) –id (
GLib.Variant
) –params (
GLib.Variant
) –
- Return type:
- do_notification(method_name, params) virtual¶
- Parameters:
method_name (
str
) –params (
GLib.Variant
) –
Signal Details¶
- Jsonrpc.Client.signals.failed(client)¶
- Signal Name:
failed
- Flags:
- Parameters:
client (
Jsonrpc.Client
) – The object which received the signal
The “failed” signal is called when the client has failed communication or the connection has been knowingly closed.
New in version 3.28.
- Jsonrpc.Client.signals.handle_call(client, method, id, params)¶
- Signal Name:
handle-call
- Flags:
- Parameters:
client (
Jsonrpc.Client
) – The object which received the signalmethod (
str
) – The method nameid (
GLib.Variant
) – The “id” field of the JSONRPC messageparams (
GLib.Variant
orNone
) – The “params” field of the JSONRPC message
- Return type:
This signal is emitted when an RPC has been received from the peer we are connected to. Return
True
if you have handled this message, even asynchronously. If no handler has returnedTrue
an error will be synthesized.If you handle the message, you are responsible for replying to the peer in a timely manner using [method`Client`.reply] or [method`Client`.reply_async].
Additionally, since 3.28 you may connect to the “detail” of this signal to handle a specific method call. Use the method name as the detail of the signal.
New in version 3.26.
- Jsonrpc.Client.signals.notification(client, method, params)¶
- Signal Name:
notification
- Flags:
- Parameters:
client (
Jsonrpc.Client
) – The object which received the signalmethod (
str
) – The method name of the notificationparams (
GLib.Variant
orNone
) – Params for the notification
This signal is emitted when a notification has been received from a peer. Unlike [signal`Client`:py:func:::handle-call<Jsonrpc.Client.signals.handle_call>], this does not have an “id” parameter because notifications do not have ids. They do not round trip.
New in version 3.26.
Property Details¶
- Jsonrpc.Client.props.io_stream¶
- Name:
io-stream
- Type:
- Default Value:
- Flags:
The “io-stream” property is the [class`Gio`.IOStream] to use for communicating with a JSON-RPC peer.
New in version 3.26.
- Jsonrpc.Client.props.use_gvariant¶
-
The “use-gvariant” property denotes if [struct`GLib`.Variant] should be used to communicate with the peer instead of JSON. You should only set this if you know the peer is also a Jsonrpc-GLib based client.
Setting this property allows the peers to communicate using
GLib.Variant
instead of JSON. This means that we can access the messages without expensive memory allocations and parsing costs associated with JSON. [struct`GLib`.Variant] is much more optimal for memory-bassed message passing.New in version 3.26.