Name | pyjsonrpc2 JSON |
Version |
1.0.1
JSON |
| download |
home_page | None |
Summary | A flexible Python implementation of the JSON-RPC 2.0 protocol. |
upload_time | 2024-11-10 07:33:52 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | MIT License Copyright (c) 2024 Crimson-Crow <github@crimsoncrow.dev> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
jsonrpc
json-rpc
rpc
json
server
client
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# pyjsonrpc2
[![PyPI](https://img.shields.io/pypi/v/pyjsonrpc2)](https://pypi.org/project/pyjsonrpc2/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyjsonrpc2)](https://pypi.org/project/pyjsonrpc2/)
[![GitHub](https://img.shields.io/github/license/Crimson-Crow/pyjsonrpc2)](https://github.com/Crimson-Crow/pyjsonrpc2/blob/main/LICENSE.txt)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://makeapullrequest.com)
A flexible Python implementation of the JSON-RPC 2.0 protocol (currently server-side only).
## Key features
- Full compliance with the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification)
- No transport functionality
- Multiple method registration patterns (class-based, individual methods, lambda, etc.)
- Automatic & custom error handling capabilities
- Support for both string and bytes input
- Complete type hints (passes `mypy --strict`)
- Extensive unit tests (full coverage)
- [Semantic versioning](https://semver.org/) adherence
## Installation
To install the package, use [pip](https://pip.pypa.io/en/stable/):
```bash
pip install pyjsonrpc2
```
## Usage
For more info, check the `/examples` directory.
### Basic Server Creation
```python
from pyjsonrpc2.server import JsonRpcServer, rpc_method, JsonRpcError
# Create a basic server
server = JsonRpcServer()
```
### Method Registration Patterns
These are the main patterns for registering RPC methods. `/examples/registering_methods.py` contains a few more.
1. Class-based approach with decorators:
```python
class MathServer(JsonRpcServer):
@rpc_method
def square(self, x):
return x**2
@rpc_method(name="cube")
def calculate_cube(self, x):
return x**3
server = MathServer()
```
2. Adding individual methods using decorators:
```python
@server.add_method
def add(a, b):
return a + b
```
3. Adding methods with custom names:
```python
def sub(a, b):
return a - b
server.add_method(sub, name="substract")
```
4. Adding lambda functions:
```python
server.add_method(lambda a, b: a % b, name="modulo")
```
### Error Handling
Error handling features:
- Custom error codes for implementation-defined & application-defined errors through the `JsonRpcError` class
- Automatic conversion of Python exceptions to JSON-RPC Internal error responses
- Support for additional error data in a structured format
- Built-in handling of protocol-level errors (invalid JSON, missing required fields, etc.)
- Error logging for debugging purposes
1. Custom Implementation-Defined Errors:
```python
class AdvancedMathServer(JsonRpcServer):
@rpc_method
def divide(self, a, b):
if b == 0:
raise JsonRpcError(
code=-32000,
message="Division by zero",
data={"numerator": a, "denominator": b}
)
return a / b
```
2. Multiple Error Conditions:
```python
@rpc_method
def factorial(self, n):
if not isinstance(n, int):
# Regular exceptions are caught and converted to Internal error responses
raise TypeError("n must be an integer")
if n < 0:
# Custom JSON-RPC errors with additional data
raise JsonRpcError(
code=-32001,
message="Invalid input for factorial",
data={"input": n, "reason": "Must be non-negative"}
)
# ... implementation ...
```
### Request execution
```python
result = server.call('{"jsonrpc": "2.0", "method": "add", "params": [5, 3], "id": 1}')
result = server.call(b'{"jsonrpc": "2.0", "method": "subtract", "params": [5, 3], "id": 2}')
```
## Tests
The simplest way to run tests is:
```bash
python -m unittest
```
As a more robust alternative, you can install [`tox`](https://tox.wiki) to automatically test across the supported python versions, then run:
```bash
tox -p
```
## Issue tracker
Please report any bugs or enhancement ideas using the [issue tracker](https://github.com/Crimson-Crow/pyjsonrpc2/issues).
## License
`pyjsonrpc2` is licensed under the terms of the [MIT License](https://github.com/Crimson-Crow/pyjsonrpc2/blob/main/LICENSE.txt).
Raw data
{
"_id": null,
"home_page": null,
"name": "pyjsonrpc2",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "Crimson-Crow <github@crimsoncrow.dev>",
"keywords": "jsonrpc, json-rpc, rpc, json, server, client",
"author": null,
"author_email": "Crimson-Crow <github@crimsoncrow.dev>",
"download_url": "https://files.pythonhosted.org/packages/f9/9e/8c63d9ab8a4b87121ef66bf55a1bbb2a330f454d5fb8c6daafa6bed23a6a/pyjsonrpc2-1.0.1.tar.gz",
"platform": null,
"description": "# pyjsonrpc2\r\n\r\n[![PyPI](https://img.shields.io/pypi/v/pyjsonrpc2)](https://pypi.org/project/pyjsonrpc2/)\r\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyjsonrpc2)](https://pypi.org/project/pyjsonrpc2/)\r\n[![GitHub](https://img.shields.io/github/license/Crimson-Crow/pyjsonrpc2)](https://github.com/Crimson-Crow/pyjsonrpc2/blob/main/LICENSE.txt)\r\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://makeapullrequest.com)\r\n\r\nA flexible Python implementation of the JSON-RPC 2.0 protocol (currently server-side only).\r\n\r\n## Key features\r\n- Full compliance with the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification)\r\n- No transport functionality\r\n- Multiple method registration patterns (class-based, individual methods, lambda, etc.)\r\n- Automatic & custom error handling capabilities\r\n- Support for both string and bytes input\r\n- Complete type hints (passes `mypy --strict`)\r\n- Extensive unit tests (full coverage)\r\n- [Semantic versioning](https://semver.org/) adherence\r\n\r\n## Installation\r\n\r\nTo install the package, use [pip](https://pip.pypa.io/en/stable/):\r\n\r\n```bash\r\npip install pyjsonrpc2\r\n```\r\n\r\n## Usage\r\n\r\nFor more info, check the `/examples` directory.\r\n\r\n### Basic Server Creation\r\n\r\n```python\r\nfrom pyjsonrpc2.server import JsonRpcServer, rpc_method, JsonRpcError\r\n\r\n# Create a basic server\r\nserver = JsonRpcServer()\r\n```\r\n\r\n### Method Registration Patterns\r\n\r\nThese are the main patterns for registering RPC methods. `/examples/registering_methods.py` contains a few more.\r\n1. Class-based approach with decorators:\r\n```python\r\nclass MathServer(JsonRpcServer):\r\n @rpc_method\r\n def square(self, x):\r\n return x**2\r\n\r\n @rpc_method(name=\"cube\")\r\n def calculate_cube(self, x):\r\n return x**3\r\n\r\nserver = MathServer()\r\n```\r\n\r\n2. Adding individual methods using decorators:\r\n```python\r\n@server.add_method\r\ndef add(a, b):\r\n return a + b\r\n```\r\n\r\n3. Adding methods with custom names:\r\n```python\r\ndef sub(a, b):\r\n return a - b\r\n\r\nserver.add_method(sub, name=\"substract\")\r\n```\r\n\r\n4. Adding lambda functions:\r\n```python\r\nserver.add_method(lambda a, b: a % b, name=\"modulo\")\r\n```\r\n\r\n### Error Handling\r\nError handling features:\r\n- Custom error codes for implementation-defined & application-defined errors through the `JsonRpcError` class\r\n- Automatic conversion of Python exceptions to JSON-RPC Internal error responses\r\n- Support for additional error data in a structured format\r\n- Built-in handling of protocol-level errors (invalid JSON, missing required fields, etc.)\r\n- Error logging for debugging purposes\r\n\r\n1. Custom Implementation-Defined Errors:\r\n```python\r\nclass AdvancedMathServer(JsonRpcServer):\r\n @rpc_method\r\n def divide(self, a, b):\r\n if b == 0:\r\n raise JsonRpcError(\r\n code=-32000,\r\n message=\"Division by zero\",\r\n data={\"numerator\": a, \"denominator\": b}\r\n )\r\n return a / b\r\n```\r\n\r\n2. Multiple Error Conditions:\r\n```python\r\n@rpc_method\r\ndef factorial(self, n):\r\n if not isinstance(n, int):\r\n # Regular exceptions are caught and converted to Internal error responses\r\n raise TypeError(\"n must be an integer\")\r\n\r\n if n < 0:\r\n # Custom JSON-RPC errors with additional data\r\n raise JsonRpcError(\r\n code=-32001,\r\n message=\"Invalid input for factorial\",\r\n data={\"input\": n, \"reason\": \"Must be non-negative\"}\r\n )\r\n # ... implementation ...\r\n```\r\n\r\n### Request execution\r\n```python\r\nresult = server.call('{\"jsonrpc\": \"2.0\", \"method\": \"add\", \"params\": [5, 3], \"id\": 1}')\r\nresult = server.call(b'{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [5, 3], \"id\": 2}')\r\n```\r\n\r\n## Tests\r\n\r\nThe simplest way to run tests is:\r\n\r\n```bash\r\npython -m unittest\r\n```\r\n\r\nAs a more robust alternative, you can install [`tox`](https://tox.wiki) to automatically test across the supported python versions, then run:\r\n\r\n```bash\r\ntox -p\r\n```\r\n\r\n## Issue tracker\r\n\r\nPlease report any bugs or enhancement ideas using the [issue tracker](https://github.com/Crimson-Crow/pyjsonrpc2/issues).\r\n\r\n## License\r\n\r\n`pyjsonrpc2` is licensed under the terms of the [MIT License](https://github.com/Crimson-Crow/pyjsonrpc2/blob/main/LICENSE.txt).\r\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2024 Crimson-Crow <github@crimsoncrow.dev> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "A flexible Python implementation of the JSON-RPC 2.0 protocol.",
"version": "1.0.1",
"project_urls": {
"Bug Reports": "https://github.com/Crimson-Crow/pyjsonrpc2/issues",
"Homepage": "https://github.com/Crimson-Crow/pyjsonrpc2",
"Source": "https://github.com/Crimson-Crow/pyjsonrpc2"
},
"split_keywords": [
"jsonrpc",
" json-rpc",
" rpc",
" json",
" server",
" client"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "6b4213fdd81a190d082c070c3517422658e8f6b3edc7e9a93ba82b0e2c5cbf1a",
"md5": "a5cd6cd27f7410184d1476e3097242ed",
"sha256": "bf11538fd99febd38be3b3992630dd04a06bf70d7dd73c5f274099db8ddd51ff"
},
"downloads": -1,
"filename": "pyjsonrpc2-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a5cd6cd27f7410184d1476e3097242ed",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 7641,
"upload_time": "2024-11-10T07:33:50",
"upload_time_iso_8601": "2024-11-10T07:33:50.747356Z",
"url": "https://files.pythonhosted.org/packages/6b/42/13fdd81a190d082c070c3517422658e8f6b3edc7e9a93ba82b0e2c5cbf1a/pyjsonrpc2-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "f99e8c63d9ab8a4b87121ef66bf55a1bbb2a330f454d5fb8c6daafa6bed23a6a",
"md5": "ddda48a6ebc5f203f7e954bbb0acf413",
"sha256": "ecc90eb901efde01d250c7c43fcd88af0f2571e8e45df39562603727b781ce93"
},
"downloads": -1,
"filename": "pyjsonrpc2-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "ddda48a6ebc5f203f7e954bbb0acf413",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 14585,
"upload_time": "2024-11-10T07:33:52",
"upload_time_iso_8601": "2024-11-10T07:33:52.185320Z",
"url": "https://files.pythonhosted.org/packages/f9/9e/8c63d9ab8a4b87121ef66bf55a1bbb2a330f454d5fb8c6daafa6bed23a6a/pyjsonrpc2-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-10 07:33:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Crimson-Crow",
"github_project": "pyjsonrpc2",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pyjsonrpc2"
}