pavlov99 / ajsonrpc Goto Github PK
View Code? Open in Web Editor NEW๐ Async JSON-RPC 2.0 protocol + server powered by asyncio & py35+. json-rpc successor.
License: MIT License
๐ Async JSON-RPC 2.0 protocol + server powered by asyncio & py35+. json-rpc successor.
License: MIT License
Tested in python 3.11.3
import asyncio
import json
from ajsonrpc import AsyncJSONRPCResponseManager, Dispatcher
class ExampleClass:
@classmethod
def example_one(cls, a, b):
raise Exception("Test error")
return a * b
def example_two(**kwargs):
raise Exception("Test error")
return kwargs['a'] * kwargs['b']
dispatcher = Dispatcher()
dispatcher.add_class(ExampleClass)
dispatcher.add_function(example_two)
manager = AsyncJSONRPCResponseManager(dispatcher=dispatcher,is_server_error_verbose=True)
async def run():
payload = {
'jsonrpc': '2.0',
'method': 'exampleclass.example_one',
'params': [2, 3],
'id': 1
}
response = await manager.get_response_for_payload(json.dumps(payload))
print(response.body)
payload = {
'jsonrpc': '2.0',
'method': 'example_two',
'params': {'a': 2, 'b': 3},
'id': 2
}
response = await manager.get_response_for_payload(json.dumps(payload))
print(response.body)
asyncio.run(run())
Output
{'jsonrpc': '2.0', 'id': 1, 'error': {'code': -32602, 'message': 'Invalid params'}}
{'jsonrpc': '2.0', 'id': 2, 'error': {'code': -32602, 'message': 'Invalid params'}}
This is to remove other service dependencies, keep everything on GitHub.
Example action:
https://github.com/marketplace/actions/sphinx-build
Running the tests on openSUSE Linux.
[ 19s] + python3.6 -m unittest -v
[ 19s] test_init (ajsonrpc.tests.test_core.TestJSONRPC20BatchRequest) ... ok
[ 19s] test_init (ajsonrpc.tests.test_core.TestJSONRPC20BatchResponse) ... ok
[ 19s] test_code_validation_not_number (ajsonrpc.tests.test_core.TestJSONRPC20Error) ... ok
[ 19s] test_code_validation_valid_numeric (ajsonrpc.tests.test_core.TestJSONRPC20Error) ... ok
[ 19s] test_could_not_change_code_message_predefined_errors (ajsonrpc.tests.test_core.TestJSONRPC20Error) ... ok
[ 19s] test_data_validation_valid (ajsonrpc.tests.test_core.TestJSONRPC20Error) ... ok
[ 19s] test_message_validation_not_str (ajsonrpc.tests.test_core.TestJSONRPC20Error) ... ok
[ 19s] test_message_validation_valid_str (ajsonrpc.tests.test_core.TestJSONRPC20Error) ... ok
[ 19s] test_body_validation (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_id_validation_invalid (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_id_validation_valid (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_method_validation_correct (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_method_validation_invalid_rpc_prefix (ajsonrpc.tests.test_core.TestJSONRPC20Request)
[ 19s] Test method SHOULD NOT starts with rpc. ... ok
[ 19s] test_method_validation_not_str (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_notification_conversion (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_notification_init (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_params_validation_dict (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_params_validation_invalid (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_params_validation_iterable (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_params_validation_list (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_params_validation_none (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_params_validation_tuple (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_request_args (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_request_kwargs (ajsonrpc.tests.test_core.TestJSONRPC20Request) ... ok
[ 19s] test_set_body_error_correct_error_class (ajsonrpc.tests.test_core.TestJSONRPC20Response)
[ 19s] Return error class matching pre-defined error codes. ... skipped 'TODO: Implement later'
[ 19s] test_set_body_result_and_error (ajsonrpc.tests.test_core.TestJSONRPC20Response) ... ok
[ 19s] test_set_valid_body (ajsonrpc.tests.test_core.TestJSONRPC20Response) ... ok
[ 19s] test_valid_error (ajsonrpc.tests.test_core.TestJSONRPC20Response) ... ok
[ 19s] test_valid_result (ajsonrpc.tests.test_core.TestJSONRPC20Response) ... ok
[ 19s] test__getattr_function (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_add_dict (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_add_function (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_class (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_class_prefix (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_del_method (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_empty (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_init_from_dict (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_object (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_object_prefix (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] test_to_dict (ajsonrpc.tests.test_dispatcher.TestDispatcher) ... ok
[ 19s] ajsonrpc.tests.test_manager (unittest.loader._FailedTest) ... ERROR
[ 19s]
[ 19s] ======================================================================
[ 19s] ERROR: ajsonrpc.tests.test_manager (unittest.loader._FailedTest)
[ 19s] ----------------------------------------------------------------------
[ 19s] ImportError: Failed to import test module: ajsonrpc.tests.test_manager
[ 19s] Traceback (most recent call last):
[ 19s] File "/usr/lib64/python3.6/unittest/loader.py", line 428, in _find_test_path
[ 19s] module = self._get_module_from_name(name)
[ 19s] File "/usr/lib64/python3.6/unittest/loader.py", line 369, in _get_module_from_name
[ 19s] __import__(name)
[ 19s] File "/home/abuild/rpmbuild/BUILD/ajsonrpc-1.1.0/ajsonrpc/tests/test_manager.py", line 9, in <module>
[ 19s] class TestAsyncJSONRPCResponseManager(unittest.IsolatedAsyncioTestCase):
[ 19s] AttributeError: module 'unittest' has no attribute 'IsolatedAsyncioTestCase'
[ 19s]
[ 19s]
[ 19s] ----------------------------------------------------------------------
[ 19s] Ran 41 tests in 0.006s
Full log can be found here:
https://build.opensuse.org/package/live_build_log/home:musfay:python/python-ajsonrpc/openSUSE_Tumbleweed/x86_64
Perhaps I don't understand the documentation or there is some other obvious issue, but I get an error when executing the example.
What I did.
$ python -V
Python 3.6.4
$ pip install ajsonrpc
...
$ pip freeze | grep ajso
ajsonrpc==0.0.3
$ git clone https://github.com/pavlov99/ajsonrpc.git
...
$ cd ajsonrpc
$ async-json-rpc-server examples.methods
Traceback (most recent call last):
File "/home/feenes/venv/py3/bin/async-json-rpc-server", line 10, in <module>
sys.exit(main())
File "/home/feenes/venv/py3/lib/python3.6/site-packages/ajsonrpc/scripts/server.py", line 66, in main
module = importlib.util.module_from_spec(spec)
File "<frozen importlib._bootstrap>", line 568, in module_from_spec
AttributeError: 'NoneType' object has no attribute 'loader'
$ cat examples/methods.py
import asyncio
def echo(s='pong'):
return s
def mul2(a, b):
return a * b
async def say_after(delay, what):
await asyncio.sleep(delay)
return what
$
Hi,
Thanks for the useful package! We use it for @platformio.
Do you have any plan to complete work on https://github.com/pavlov99/ajsonrpc/blob/master/ajsonrpc/manager.py and publish a stable release?
P.S: We currently use https://github.com/pavlov99/json-rpc with async wrappers. It would be great to use the official version.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.