A JSON-RPC websocket client library for asyncio
Project description
jsonrpc-websocket: a compact JSON-RPC websocket client library for asyncio
=======================================================================================================
.. image:: https://travis-ci.org/armills/jsonrpc-websocket.svg
:target: https://travis-ci.org/armills/jsonrpc-websocket
.. image:: https://coveralls.io/repos/armills/jsonrpc-websocket/badge.svg
:target: https://coveralls.io/r/armills/jsonrpc-websocket
This is a compact and simple JSON-RPC websocket client implementation for asyncio python code. This code is forked from https://github.com/gciotta/jsonrpc-requests
Main Features
-------------
* Python 3.5 & 3.6 compatible
* Supports nested namespaces (eg. `app.users.getUsers()`)
* 100% test coverage
Usage
-----
It is recommended to manage the aiohttp ClientSession object externally and pass it to the Server constructor. `(See the aiohttp documentation.) <https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession>`_ If not passed to Server, a ClientSession object will be created automatically.
Execute remote JSON-RPC functions
.. code-block:: python
import asyncio
from jsonrpc_websocket import Server
async def routine():
server = Server('ws://localhost:9090')
try:
await server.ws_connect()
await server.foo(1, 2)
await server.foo(bar=1, baz=2)
await server.foo({'foo': 'bar'})
await server.foo.bar(baz=1, qux=2)
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
A notification
.. code-block:: python
import asyncio
from jsonrpc_websocket import Server
async def routine():
server = Server('ws://localhost:9090')
try:
await server.ws_connect()
await server.foo(bar=1, _notification=True)
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Handle requests from server to client
.. code-block:: python
import asyncio
from jsonrpc_websocket import Server
def client_method(arg1, arg2):
return arg1 + arg2
async def routine():
server = Server('ws://localhost:9090')
# client_method is called when server requests method 'namespace.client_method'
server.namespace.client_method = client_method
try:
await server.ws_connect()
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Pass through arguments to aiohttp (see also `aiohttp documentation <http://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession.request>`_)
.. code-block:: python
import asyncio
import aiohttp
from jsonrpc_websocket import Server
async def routine():
server = Server(
'ws://localhost:9090',
auth=aiohttp.BasicAuth('user', 'pass'),
headers={'x-test2': 'true'})
try:
await server.ws_connect()
await server.foo()
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Pass through aiohttp exceptions
.. code-block:: python
import asyncio
import aiohttp
from jsonrpc_websocket import Server
async def routine():
server = Server('ws://unknown-host')
try:
await server.ws_connect()
await server.foo()
except TransportError as transport_error:
print(transport_error.args[1]) # this will hold a aiohttp exception instance
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Tests
-----
Install the Python tox package and run ``tox``, it'll test this package with various versions of Python.
Changelog
---------
1.0.0 (July 6, 2018)
~~~~~~~~~~~~~~
- Bumped jsonrpc-base to version 1.0.1
0.6 (March 11, 2018)
~~~~~~~~~~~~~~
- Minimum required version of aiohttp is now 3.0.
- Support for Python 3.4 is now dropped.
Credits
-------
`@gciotta <https://github.com/gciotta>`_ for creating the base project `jsonrpc-requests <https://github.com/gciotta/jsonrpc-requests>`_.
`@mbroadst <https://github.com/mbroadst>`_ for providing full support for nested method calls, JSON-RPC RFC
compliance and other improvements.
`@vaab <https://github.com/vaab>`_ for providing api and tests improvements, better RFC compliance.
=======================================================================================================
.. image:: https://travis-ci.org/armills/jsonrpc-websocket.svg
:target: https://travis-ci.org/armills/jsonrpc-websocket
.. image:: https://coveralls.io/repos/armills/jsonrpc-websocket/badge.svg
:target: https://coveralls.io/r/armills/jsonrpc-websocket
This is a compact and simple JSON-RPC websocket client implementation for asyncio python code. This code is forked from https://github.com/gciotta/jsonrpc-requests
Main Features
-------------
* Python 3.5 & 3.6 compatible
* Supports nested namespaces (eg. `app.users.getUsers()`)
* 100% test coverage
Usage
-----
It is recommended to manage the aiohttp ClientSession object externally and pass it to the Server constructor. `(See the aiohttp documentation.) <https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession>`_ If not passed to Server, a ClientSession object will be created automatically.
Execute remote JSON-RPC functions
.. code-block:: python
import asyncio
from jsonrpc_websocket import Server
async def routine():
server = Server('ws://localhost:9090')
try:
await server.ws_connect()
await server.foo(1, 2)
await server.foo(bar=1, baz=2)
await server.foo({'foo': 'bar'})
await server.foo.bar(baz=1, qux=2)
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
A notification
.. code-block:: python
import asyncio
from jsonrpc_websocket import Server
async def routine():
server = Server('ws://localhost:9090')
try:
await server.ws_connect()
await server.foo(bar=1, _notification=True)
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Handle requests from server to client
.. code-block:: python
import asyncio
from jsonrpc_websocket import Server
def client_method(arg1, arg2):
return arg1 + arg2
async def routine():
server = Server('ws://localhost:9090')
# client_method is called when server requests method 'namespace.client_method'
server.namespace.client_method = client_method
try:
await server.ws_connect()
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Pass through arguments to aiohttp (see also `aiohttp documentation <http://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession.request>`_)
.. code-block:: python
import asyncio
import aiohttp
from jsonrpc_websocket import Server
async def routine():
server = Server(
'ws://localhost:9090',
auth=aiohttp.BasicAuth('user', 'pass'),
headers={'x-test2': 'true'})
try:
await server.ws_connect()
await server.foo()
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Pass through aiohttp exceptions
.. code-block:: python
import asyncio
import aiohttp
from jsonrpc_websocket import Server
async def routine():
server = Server('ws://unknown-host')
try:
await server.ws_connect()
await server.foo()
except TransportError as transport_error:
print(transport_error.args[1]) # this will hold a aiohttp exception instance
finally:
await server.close()
await server.session.close()
asyncio.get_event_loop().run_until_complete(routine())
Tests
-----
Install the Python tox package and run ``tox``, it'll test this package with various versions of Python.
Changelog
---------
1.0.0 (July 6, 2018)
~~~~~~~~~~~~~~
- Bumped jsonrpc-base to version 1.0.1
0.6 (March 11, 2018)
~~~~~~~~~~~~~~
- Minimum required version of aiohttp is now 3.0.
- Support for Python 3.4 is now dropped.
Credits
-------
`@gciotta <https://github.com/gciotta>`_ for creating the base project `jsonrpc-requests <https://github.com/gciotta/jsonrpc-requests>`_.
`@mbroadst <https://github.com/mbroadst>`_ for providing full support for nested method calls, JSON-RPC RFC
compliance and other improvements.
`@vaab <https://github.com/vaab>`_ for providing api and tests improvements, better RFC compliance.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file jsonrpc-websocket-1.0.0.tar.gz.
File metadata
- Download URL: jsonrpc-websocket-1.0.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40949836996c0a8104e7878997d3f68bda4561e9d3af64e5cd178127ec3c2778
|
|
| MD5 |
84b206d511a9b0b790cc0631311d21c3
|
|
| BLAKE2b-256 |
76ffc9328b57b01a124af77809ec53d17759c245830953fe49df3f3809b191c3
|