Async Network Clients¶
Asynchronous network clients for interacting with the XRPL.
- class xrpl.asyncio.clients.AsyncJsonRpcClient(url: str)¶
An async client for interacting with the rippled JSON RPC.
- class xrpl.asyncio.clients.AsyncWebsocketClient(url: str)¶
An async client for interacting with the rippled WebSocket API.
Instead of calling
open
andclose
yourself, you can use a context like so:async with AsyncWebsocketClient(url) as client: # inside the context the client is open # after exiting the context, the client is closed
Doing this will open and close the client for you and is preferred.
NOTE: if you are not using subscriptions or other WebSocket-only features of rippled, you may not need to do anything other than open the client and make requests:
from xrpl.asyncio.clients import AsyncWebsocketClient from xrpl.asyncio.ledger import get_fee from xrpl.models import Fee async with AsyncWebsocketClient(url) as client: # using helper functions print(await get_fee(client)) # using raw requests yourself print(await client.request(Fee())
However, if you are using some functionality that makes use of subscriptions or other “websocket-y” things, you can iterate over the client like so to read incoming messages:
async with AsyncWebsocketClient(url) as client: # inside the context the client is open async for message in client: # do something with a message # after exiting the context, the client is closed
The recommended way to use this client is to set up a Task using the
asyncio
library to listen to incoming messages and do something with them, but the above will work fine if you want to listen indefinitely. This is how you can use a Task to listen to messages without blocking:import asyncio from xrpl.asyncio.clients import AsyncWebsocketClient from xrpl.models import Subscribe, Unsubscribe, StreamParameter URL = "wss://some-url-to-connect-to" async def listener(client): async for message in client: # do something with a message async def main(): async with AsyncWebsocketClient(URL) as client: # set up the `listener` function as a Task # so that it doesn't wait for a response, but # will "awaken" whenever the `asyncio` event # loop toggles to it. asyncio.create_task(listener(client)) # now, the `listener` function will run as if # it were "in the background", doing whatever you # want as soon as it has a message. # now let's subscribe to something. in this case, # we can just use `send` instead of `request` # because we don't really care about the response # since the listener will also get it. await client.send(Subscribe( streams=[StreamParameter.LEDGER], )) # in the meantime, you can continue to do whatever # you want and the python `asyncio` event loop # will toggle between your code and the listener # as messages are ready. let's just sleep. note, # you need to use `asyncio.sleep` within # async code instead of `time.sleep`, otherwise # you will block all the waiting tasks instead of # just this code path. await asyncio.sleep(5) # now that we're done, we can unsubscribe if # we like await client.send(Unsubscribe( streams=[StreamParameter.LEDGER], )) # now, outside of the context, the client is closed. # the listener task will now never receive a new message. if __name__ == "__main__": # remember to run your entire program within a # `asyncio.run` call. asyncio.run(main())
- async close() None ¶
Closes the connection.
- is_open() bool ¶
Returns whether the client is currently open.
- Returns:
True if the client is currently open, False otherwise.
- async open() None ¶
Connects the client to the Web Socket API at the given URL.
- async request(request: Request) Response ¶
Makes a request with this client and returns the response.
- Parameters:
request – The Request to send.
- Returns:
The Response for the given Request.
- async send(request: Request) None ¶
Submit the request represented by the request to the rippled node specified by this client’s URL. Unlike
request
,send
does not wait for this request’s response. In many cases it may be more convenient to userequest
.- Parameters:
request – A Request object representing information about a rippled request.
- Raises:
XRPLWebsocketException – If there is already an open request by the request’s ID, or if this WebsocketBase is not open.
- exception xrpl.asyncio.clients.XRPLRequestFailureException(result: Dict[str, Any])¶
XRPL Request Exception, when the request fails.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- xrpl.asyncio.clients.json_to_response(json: Dict[str, Any]) Response ¶
Converts a JSON response from the rippled server into a Response object.
- Parameters:
json – A dictionary representing the contents of the json response from the rippled server.
- Returns:
A Response object containing the information in the rippled server’s response.
- xrpl.asyncio.clients.request_to_json_rpc(request_object: Request) Dict[str, Any] ¶
Converts a request model object to the appropriate JSON format for interacting with the rippled API.
- Parameters:
request_object – A Request object representing the parameters of a request to the rippled JSON RPC.
- Returns:
A dictionary containing the attributes of this Request object formatted for submission to the rippled JSON RPC.
- xrpl.asyncio.clients.request_to_websocket(request_object: Request) Dict[str, Any] ¶
Converts a request model object to the appropriate format for interacting with the rippled API via the WebSocket API.
- Parameters:
request_object – A Request object representing the parameters of a request to the rippled JSON RPC.
- Returns:
A dictionary containing the attributes of this Request object formatted for submission to the rippled WebSocket API.
- xrpl.asyncio.clients.websocket_to_response(response_dict: Dict[str, Any]) Response ¶
Converts a WebSocket API response from the rippled server into a Response object.
- Parameters:
response_dict – A dictionary representing the contents of the WebSocket API response from the rippled server.
- Returns:
A Response object containing the information in the rippled server’s response.