Skip to main content

instant_client

Build Status Coverage Status Supports Python versions 3.7+

A type-safe JSON-RPC client with automatic (de)serialization.

pip install instant-client

For communication over HTTP (like in the example below):

pip install 'instant-client[requests]'

instant_client can be used with any server implementing JSON-RPC, but it's best paired with instant_api. For example, suppose the API server is set up like this:

from dataclasses import dataclass
from flask import Flask
from instant_api import InstantAPI

app = Flask(__name__)

@dataclass
class Point:
    x: int
    y: int

@InstantAPI(app)
class Methods:
    def translate(self, p: Point, dx: int, dy: int) -> Point:
        return Point(p.x + dx, p.y + dy)

    def scale(self, p: Point, factor: int) -> Point:
        return Point(p.x * factor, p.y * factor)

if __name__ == '__main__':
    app.run()

Then using the client is as simple as:

from server import Methods, Point  # the classes we defined above
from instant_client import InstantClient

# The type hint is a lie, but your linter/IDE doesn't know that!
methods: Methods = InstantClient("http://127.0.0.1:5000/api/", Methods()).methods

assert methods.scale(Point(1, 2), factor=3) == Point(3, 6)

That looks a lot like it just called Methods.scale() directly, which is the point (no pun intended), but under the hood it did in fact send an HTTP request to the server! The same code written more manually looks like this:

import requests

response = requests.post(
    'http://127.0.0.1:5000/api/',
    json={
        'id': 0, 
        'jsonrpc': '2.0', 
        'method': 'scale', 
        'params': {
            'p': {'x': 1, 'y': 2}, 
            'factor': 3,
        },
    },
)

assert response.json()['result'] == {'x': 3, 'y': 6}

In general, the InstantClient constructor has two required parameters:

  1. A client from the jsonrpcclient library for your desired transport. For example:

    from jsonrpcclient.clients.zeromq_client import ZeroMQClient
    from instant_client import InstantClient
    
    client = InstantClient(ZeroMQClient("tcp://localhost:5000"), Methods())
    

    As a convenience, you can also just pass a string representing a URL, which will be used to construct an HTTPClient.

  2. An object defining your methods. The method body can be empty, InstantClient just uses the signature and type hints to serialize the arguments and deserialize the result with the help of datafunctions.

The methods attribute of the client is a simple proxy so that this:

client.methods.scale(Point(1, 2), factor=3)

is equivalent to:

client.request("scale", Point(1, 2), factor=3)

which in turn looks up the signature of the original method.

Your IDE/linter/type-checker should think that client.methods is the object you passed at the beginning, so you can get all the usual warnings and autocompletions. Adding your own type hint can help but shouldn't be necessary.

Release files for instant-client 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for instant-client 0.0.1
File Interpreter ABI Platform
instant_client-0.0.1-py3-none-any.whl Python 3 none any Details

Release files / instant_client-0.0.1-py3-none-any.whl

Download URL instant_client-0.0.1-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00f9b676f64ede890f64e3c31bd14102da0f907489e574a2f386c04c788960da
BLAKE2b-256 checksum
How to use checksums
d8735fdc866798276664948213c4d5e16080b191efdd110a82341464fbd8d6d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.11.1 setuptools/36.2.7 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.1

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page