Skip to main content

A JSON-RPC websocket client library for asyncio

Project description

https://travis-ci.org/armills/jsonrpc-websocket.svg https://coveralls.io/repos/armills/jsonrpc-websocket/badge.svg

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.4 & 3.5 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.) If not passed to Server, a ClientSession object will be created automatically.

Execute remote JSON-RPC functions

import asyncio
from jsonrpc_websocket import Server

@asyncio.coroutine
def routine():
    server = Server('ws://localhost:9090')
    try:
        yield from server.ws_connect()

        yield from server.foo(1, 2)
        yield from server.foo(bar=1, baz=2)
        yield from server.foo({'foo': 'bar'})
        yield from server.foo.bar(baz=1, qux=2)
    finally:
        yield from server.close()
        yield from server.session.close()

asyncio.get_event_loop().run_until_complete(routine())

A notification

import asyncio
from jsonrpc_websocket import Server

@asyncio.coroutine
def routine():
    server = Server('ws://localhost:9090')
    try:
        yield from server.ws_connect()

        yield from server.foo(bar=1, _notification=True)
    finally:
        yield from server.close()
        yield from server.session.close()

asyncio.get_event_loop().run_until_complete(routine())

Handle requests from server to client

import asyncio
from jsonrpc_websocket import Server

def client_method(arg1, arg2):
    return arg1 + arg2

@asyncio.coroutine
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:
        yield from server.ws_connect()
    finally:
        yield from server.close()
        yield from server.session.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

@asyncio.coroutine
def routine():
    server = Server(
        'ws://localhost:9090',
        auth=aiohttp.BasicAuth('user', 'pass'),
        headers={'x-test2': 'true'})
    try:
        yield from server.ws_connect()

        yield from server.foo()
    finally:
        yield from server.close()
        yield from server.session.close()

asyncio.get_event_loop().run_until_complete(routine())

Pass through aiohttp exceptions

import asyncio
import aiohttp
from jsonrpc_websocket import Server

@asyncio.coroutine
def routine():
    server = Server('ws://unknown-host')
    try:
        yield from server.ws_connect()

        yield from server.foo()
    except TransportError as transport_error:
        print(transport_error.args[1]) # this will hold a aiohttp exception instance
    finally:
        yield from server.close()
        yield from 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.

Credits

@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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonrpc-websocket-0.5.tar.gz (4.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page