jsonrpc2-pyclient


Namejsonrpc2-pyclient JSON
Version 5.2.0 PyPI version JSON
download
home_pagehttps://gitlab.com/mburkard/jsonrpc2-pyclient
SummaryPython JSON-RPC 2.0 client library.
upload_time2024-01-22 21:37:02
maintainer
docs_urlNone
authorMatthew Burkard
requires_python>=3.9,<4.0
licenseAGPL-3.0-or-later
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">
<!-- Title: -->
  <h1>JSON RPC PyClient</h1>
<!-- Labels: -->
  <!-- First row: -->
  <img src="https://img.shields.io/badge/License-AGPL%20v3-blue.svg"
   height="20"
   alt="License: AGPL v3">
  <img src="https://img.shields.io/badge/code%20style-black-000000.svg"
   height="20"
   alt="Code style: black">
  <img src="https://img.shields.io/pypi/v/jsonrpc2-pyclient.svg"
   height="20"
   alt="PyPI version">
  <a href="https://gitlab.com/mburkard/jsonrpc-pyclient/-/blob/main/CONTRIBUTING.md">
    <img src="https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=00b250"
     height="20"
     alt="Contributions Welcome">
  </a>
  <h3>A library for creating JSON RPC 2.0 clients in Python with async support</h3>
</div>

## Install

```shell
poetry add jsonrpc2-pyclient
```

```shell
pip install jsonrpc2-pyclient
```

## Example

Example of the client decorator implementing rpc methods from reading
method signatures.

```python
import asyncio

from pydantic import BaseModel

from jsonrpc2pyclient.decorator import rpc_client
from jsonrpc2pyclient.httpclient import AsyncRPCHTTPClient

transport = AsyncRPCHTTPClient("http://127.0.0.1:8000/api/v1")


class Vector3(BaseModel):
    x: float = 1.0
    y: float = 1.0
    z: float = 1.0


@rpc_client(transport=transport)
class TestClient:
    async def add(self, a: int, b: int) -> int: ...
    async def get_distance(self, a: Vector3, b: Vector3) -> Vector3: ...


async def main() -> None:
    client = TestClient()
    assert await client.add(3, 4) == 7
    assert await client.get_distance(Vector3(), Vector3()) == Vector3(x=0, y=0, z=0)


if __name__ == "__main__":
    asyncio.run(main())
```

### RPCClient Abstract Class

JSON-RPC 2.0 is transport agnostic. This library provides an abstract
class that can be extended to create clients for different transports.

### Transports

To make client for a transport, extend the `RPCClient` class and
implement the `_send_and_get_json` which takes a request as a str and is
expected to return a JSON-RPC 2.0 response as a str or byte string.
`RPCClient` has a `call` method that uses this internally.

A default HTTP and Websocket implementation is provided.

### Usage

The `RPCClient` will handle forming requests and parsing responses.
To call a JSON-RPC 2.0 method with an implementation of `RPCClient`,
call the `call` method, passing it the name of the method to call and
the params.

If the response is JSON-RPC 2.0 result object, only the result will be
returned, none of the wrapper.

If the response is JSON-RPC 2.0 error response, and exception will be
thrown for the error.

```python
from jsonrpc2pyclient.httpclient import RPCHTTPClient
from jsonrpcobjects.errors import JSONRPCError

client = RPCHTTPClient("http://localhost:5000/api/v1/")
try:
    res = client.call("divide", [0, 0])
    print(f"JSON-RPC Result: {res}")
except JSONRPCError as e:
    print(f"JSON-RPC Error: {e}")
```

## Client Decorator

The `rpc_client` decorator can be used to quickly put together a client
with typed methods. When a class is decorated, each method defined in
that class will make RPC requests using the provided transport and parse
the result. The name of the method will be used in the RPC request.

The method body must end with `...` for the decorator to implement it.

```python
transport = RPCHTTPClient("http://127.0.0.1:8000/api/v1")

@rpc_client(transport=transport)
class TestClient:
    def add(self, a: int, b: int) -> int: ...


client = TestClient()
assert client.add(3, 4) == 7
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://gitlab.com/mburkard/jsonrpc2-pyclient",
    "name": "jsonrpc2-pyclient",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Matthew Burkard",
    "author_email": "matthewjburkard@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f6/4b/f20daa569129eb97b1124487dbed19f17467552984dd98d42e06e05e5a7e/jsonrpc2-pyclient-5.2.0.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n<!-- Title: -->\n  <h1>JSON RPC PyClient</h1>\n<!-- Labels: -->\n  <!-- First row: -->\n  <img src=\"https://img.shields.io/badge/License-AGPL%20v3-blue.svg\"\n   height=\"20\"\n   alt=\"License: AGPL v3\">\n  <img src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"\n   height=\"20\"\n   alt=\"Code style: black\">\n  <img src=\"https://img.shields.io/pypi/v/jsonrpc2-pyclient.svg\"\n   height=\"20\"\n   alt=\"PyPI version\">\n  <a href=\"https://gitlab.com/mburkard/jsonrpc-pyclient/-/blob/main/CONTRIBUTING.md\">\n    <img src=\"https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=00b250\"\n     height=\"20\"\n     alt=\"Contributions Welcome\">\n  </a>\n  <h3>A library for creating JSON RPC 2.0 clients in Python with async support</h3>\n</div>\n\n## Install\n\n```shell\npoetry add jsonrpc2-pyclient\n```\n\n```shell\npip install jsonrpc2-pyclient\n```\n\n## Example\n\nExample of the client decorator implementing rpc methods from reading\nmethod signatures.\n\n```python\nimport asyncio\n\nfrom pydantic import BaseModel\n\nfrom jsonrpc2pyclient.decorator import rpc_client\nfrom jsonrpc2pyclient.httpclient import AsyncRPCHTTPClient\n\ntransport = AsyncRPCHTTPClient(\"http://127.0.0.1:8000/api/v1\")\n\n\nclass Vector3(BaseModel):\n    x: float = 1.0\n    y: float = 1.0\n    z: float = 1.0\n\n\n@rpc_client(transport=transport)\nclass TestClient:\n    async def add(self, a: int, b: int) -> int: ...\n    async def get_distance(self, a: Vector3, b: Vector3) -> Vector3: ...\n\n\nasync def main() -> None:\n    client = TestClient()\n    assert await client.add(3, 4) == 7\n    assert await client.get_distance(Vector3(), Vector3()) == Vector3(x=0, y=0, z=0)\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n```\n\n### RPCClient Abstract Class\n\nJSON-RPC 2.0 is transport agnostic. This library provides an abstract\nclass that can be extended to create clients for different transports.\n\n### Transports\n\nTo make client for a transport, extend the `RPCClient` class and\nimplement the `_send_and_get_json` which takes a request as a str and is\nexpected to return a JSON-RPC 2.0 response as a str or byte string.\n`RPCClient` has a `call` method that uses this internally.\n\nA default HTTP and Websocket implementation is provided.\n\n### Usage\n\nThe `RPCClient` will handle forming requests and parsing responses.\nTo call a JSON-RPC 2.0 method with an implementation of `RPCClient`,\ncall the `call` method, passing it the name of the method to call and\nthe params.\n\nIf the response is JSON-RPC 2.0 result object, only the result will be\nreturned, none of the wrapper.\n\nIf the response is JSON-RPC 2.0 error response, and exception will be\nthrown for the error.\n\n```python\nfrom jsonrpc2pyclient.httpclient import RPCHTTPClient\nfrom jsonrpcobjects.errors import JSONRPCError\n\nclient = RPCHTTPClient(\"http://localhost:5000/api/v1/\")\ntry:\n    res = client.call(\"divide\", [0, 0])\n    print(f\"JSON-RPC Result: {res}\")\nexcept JSONRPCError as e:\n    print(f\"JSON-RPC Error: {e}\")\n```\n\n## Client Decorator\n\nThe `rpc_client` decorator can be used to quickly put together a client\nwith typed methods. When a class is decorated, each method defined in\nthat class will make RPC requests using the provided transport and parse\nthe result. The name of the method will be used in the RPC request.\n\nThe method body must end with `...` for the decorator to implement it.\n\n```python\ntransport = RPCHTTPClient(\"http://127.0.0.1:8000/api/v1\")\n\n@rpc_client(transport=transport)\nclass TestClient:\n    def add(self, a: int, b: int) -> int: ...\n\n\nclient = TestClient()\nassert client.add(3, 4) == 7\n```\n",
    "bugtrack_url": null,
    "license": "AGPL-3.0-or-later",
    "summary": "Python JSON-RPC 2.0 client library.",
    "version": "5.2.0",
    "project_urls": {
        "Homepage": "https://gitlab.com/mburkard/jsonrpc2-pyclient",
        "Repository": "https://gitlab.com/mburkard/jsonrpc2-pyclient"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "814f28a31fce905678b9e07b208fd3cf434d3498fd29c193c939df237870ae70",
                "md5": "6a2d8b7f9be5449d06a2dcb66f6a513d",
                "sha256": "29b2c5ebaaf8118e1f07fdeb1c90e95de4b14019253c87810f21644d9146d1fe"
            },
            "downloads": -1,
            "filename": "jsonrpc2_pyclient-5.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6a2d8b7f9be5449d06a2dcb66f6a513d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<4.0",
            "size": 21274,
            "upload_time": "2024-01-22T21:37:04",
            "upload_time_iso_8601": "2024-01-22T21:37:04.613273Z",
            "url": "https://files.pythonhosted.org/packages/81/4f/28a31fce905678b9e07b208fd3cf434d3498fd29c193c939df237870ae70/jsonrpc2_pyclient-5.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f64bf20daa569129eb97b1124487dbed19f17467552984dd98d42e06e05e5a7e",
                "md5": "7338993d27f557a9336f0da856e41c1c",
                "sha256": "5ffb953d87e09a4cf0ad85006762d0ccf0e5bec852674091afdaed4d55638b20"
            },
            "downloads": -1,
            "filename": "jsonrpc2-pyclient-5.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "7338993d27f557a9336f0da856e41c1c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<4.0",
            "size": 21058,
            "upload_time": "2024-01-22T21:37:02",
            "upload_time_iso_8601": "2024-01-22T21:37:02.785222Z",
            "url": "https://files.pythonhosted.org/packages/f6/4b/f20daa569129eb97b1124487dbed19f17467552984dd98d42e06e05e5a7e/jsonrpc2-pyclient-5.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-22 21:37:02",
    "github": false,
    "gitlab": true,
    "bitbucket": false,
    "codeberg": false,
    "gitlab_user": "mburkard",
    "gitlab_project": "jsonrpc2-pyclient",
    "lcname": "jsonrpc2-pyclient"
}
        
Elapsed time: 0.55882s