Skip to main content

A JSON-RPC client library for asyncio

Project description

https://img.shields.io/pypi/v/jsonrpc-async.svg https://github.com/emlove/jsonrpc-async/workflows/tests/badge.svg https://coveralls.io/repos/emlove/jsonrpc-async/badge.svg

This is a compact and simple JSON-RPC client implementation for asyncio python code. This code is forked from https://github.com/gciotta/jsonrpc-requests

Main Features

  • Python 3.6, 3.7, 3.8 & 3.9 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_async import Server

async def routine():
    server = Server('http://localhost:8080')
    try:
        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.session.close()

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

A notification

import asyncio
from jsonrpc_async import Server

async def routine():
    server = Server('http://localhost:8080')
    try:
        await server.foo(bar=1, _notification=True)
    finally:
        await 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_async import Server

async def routine():
    server = Server(
        'http://localhost:8080',
        auth=aiohttp.BasicAuth('user', 'pass'),
        headers={'x-test2': 'true'})
    try:
        await server.foo()
    finally:
        await server.session.close()

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

Pass through aiohttp exceptions

import asyncio
import aiohttp
from jsonrpc_async import Server

async def routine():
    server = Server('http://unknown-host')
    try:
        await server.foo()
    except TransportError as transport_error:
        print(transport_error.args[1]) # this will hold a aiohttp exception instance
    finally:
        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

2.0.0 (2021-03-16)

  • 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}))

1.1.1 (2019-11-12)

  • Bumped jsonrpc-base to version 1.0.3

1.1.0 (2018-09-04)

  • Added support for using a custom json.loads method (#1) @tdivis

1.0.1 (2018-08-23)

  • Bumped jsonrpc-base to version 1.0.2

1.0.0 (2018-07-06)

  • Bumped minimum aiohttp version to 3.0.0

  • Bumped jsonrpc-base to version 1.0.1

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-async-2.0.0.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file jsonrpc-async-2.0.0.tar.gz.

File metadata

  • Download URL: jsonrpc-async-2.0.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.7

File hashes

Hashes for jsonrpc-async-2.0.0.tar.gz
Algorithm Hash digest
SHA256 52b1cfe584df8bc2d19930fc18c5e982378690c825ba6916103724195ba52099
MD5 3cc9353ede40668bfd35ac72c26fa050
BLAKE2b-256 d414ac815769138a664a0b4b15c7b2f9fa5bcf6a432895858f213bf0c31c02e8

See more details on using hashes here.

Supported by

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