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
- Supports nested namespaces (eg. app.users.getUsers())
- 100% test coverage
It is recommended to manage the aiohttp ClientSession object externally and pass it to the Server constructor. (See the aiohttp documentation.) If not passed to Server, a ClientSession object will be created automatically, and will be closed when the websocket connection is closed. If you pass in an external ClientSession, it is your responsibility to close it when you are finished.
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()
asyncio.get_event_loop().run_until_complete(routine())
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()
asyncio.get_event_loop().run_until_complete(routine())
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()
asyncio.get_event_loop().run_until_complete(routine())
Pass through arguments to aiohttp (see also aiohttp documentation)
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()
asyncio.get_event_loop().run_until_complete(routine())
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()
asyncio.get_event_loop().run_until_complete(routine())
Install the Python tox package and run tox
, it'll test this package with various versions of Python.
- Only reconnect session when the session is managed internally
- Remove deprecated with timeout syntax
- Unpin test dependencies
- Fixed compatibility with async_timeout 4.0
- Bumped jsonrpc-base to version 2.1.0
- Bumped jsonrpc-base to version 2.0.0
- BREAKING CHANGE: Allow single mapping as a positional parameter.
Previously, when calling with a single dict as a parameter (example:
server.foo({'bar': 0})
), the mapping was used as the JSON-RPC keyword parameters. This made it impossible to send a mapping as the first and only positional parameter. If you depended on the old behavior, you can recreate it by spreading the mapping as your method's kwargs. (example:server.foo(**{'bar': 0})
)
- Remove session as a reserved attribute on Server
- Fix loop not closing after client closes
- Support for async server request handlers
- Bumped jsonrpc-base to version 1.0.3
- Bumped jsonrpc-base to version 1.0.2
- Bumped jsonrpc-base to version 1.0.1
- Minimum required version of aiohttp is now 3.0.
- Support for Python 3.4 is now dropped.
@gciotta for creating the base project jsonrpc-requests.
@mbroadst for providing full support for nested method calls, JSON-RPC RFC compliance and other improvements.
@vaab for providing api and tests improvements, better RFC compliance.