From e336a9d452a7a226fc31b72b4f92adebdde5747e Mon Sep 17 00:00:00 2001 From: RemiZOffAlex Date: Wed, 3 Sep 2025 17:39:24 +0300 Subject: [PATCH] Update --- pyproject.toml | 2 +- src/jsonrpc/__init__.py | 30 +++++------------------------- src/jsonrpc/backend/aiohttp.py | 11 +++++++++-- src/jsonrpc/client.py | 9 ++++----- tests/test_jsonrpc.py | 22 +++++++++++++++------- 5 files changed, 34 insertions(+), 40 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c3572c6..93a15a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "jsonrpc" -version = "2.0" +version = "3.0" authors = [ { name="RemiZOffAlex", email="remizoffalex@gmail.com" }, ] diff --git a/src/jsonrpc/__init__.py b/src/jsonrpc/__init__.py index 4e6a2d5..070680d 100644 --- a/src/jsonrpc/__init__.py +++ b/src/jsonrpc/__init__.py @@ -11,20 +11,6 @@ from .exceptions import * log = logging.getLogger(__name__) -class Response: - def __init__(self, id: int, result): - self.id = id - self.result = result - - def __json__(self): - response = { - "jsonrpc": "3.0", - "result": self.result, - "id": self.id, - } - return response - - class Method: def __init__(self, function, middlewares=None, debug: bool = False): self.function = function @@ -71,11 +57,6 @@ class Method: return '<{}>'.format(self.function) -class Wrapper: - def __init__(self, func): - self.func = func - - class JSONRPC: """Основной класс JSON-RPC """ @@ -177,8 +158,6 @@ class JSONRPC: response = method(query) except JSONRPCError as e: log.error(traceback.format_exc()) - # print(traceback.format_exc()) - # response = traceback.format_exc() response = InternalError( id=query['id'], message=str(e) @@ -192,10 +171,11 @@ class JSONRPC: # message=traceback.format_exc() ) else: - response = Response( - id=query['id'], - result=response - ) + response = { + "jsonrpc": query['jsonrpc'], + "id": query['id'], + "result": response + } result = response return result diff --git a/src/jsonrpc/backend/aiohttp.py b/src/jsonrpc/backend/aiohttp.py index 1609aad..d1d820b 100644 --- a/src/jsonrpc/backend/aiohttp.py +++ b/src/jsonrpc/backend/aiohttp.py @@ -6,10 +6,11 @@ import logging import pathlib import aiohttp_jinja2 -from aiohttp.web import Response, json_response +from aiohttp.web import Response from .. import JSONRPC from ..exceptions import ParseError +from ..serialize import JSONRPCEncoder log = logging.getLogger(__name__) @@ -39,7 +40,13 @@ class APIHandler: try: json_data = await request.json() result = self.jsonrpc(json_data) - return json_response(result) + body = json.dumps(result, cls=JSONRPCEncoder) + response = Response( + text=body, + status=200, + content_type='application/json' + ) + return response except ValueError as e: log.error('invalid json: %s', request_data) log.exception(e) diff --git a/src/jsonrpc/client.py b/src/jsonrpc/client.py index cdf11e9..7331448 100644 --- a/src/jsonrpc/client.py +++ b/src/jsonrpc/client.py @@ -16,22 +16,21 @@ class Client: debug: bool = False ): self.url = url - self.headers = {'content-type': 'application/json'} self.debug = debug def __call__(self, queries): """Вызов метода """ if isinstance(queries, str): - payload = queries - elif isinstance(queries, dict | list): payload = json.dumps(queries) + elif isinstance(queries, dict | list): + payload = queries response = requests.post( self.url, - data=payload, - headers=self.headers + json=payload ) if self.debug: + log.debug('status_code', response.status_code) log.debug('content', response.content) result = response.json() diff --git a/tests/test_jsonrpc.py b/tests/test_jsonrpc.py index 2cc9fe4..370b4ff 100644 --- a/tests/test_jsonrpc.py +++ b/tests/test_jsonrpc.py @@ -2,6 +2,7 @@ __author__ = 'RemiZOffAlex' __email__ = 'remizoffalex@mail.ru' import unittest +from uuid import uuid4 from jsonrpc import JSONRPC @@ -19,29 +20,36 @@ class Calc(unittest.TestCase): def test_example(self): self.assertEqual( jsonrpc.example('boo'), - {'jsonrpc': '2.0', 'method': 'boo', 'id': 1} + { + 'jsonrpc': '3.0', + 'method': 'boo', + 'id': '00000000-0000-0000-0000-000000000000' + } ) def test_evaluate(self): - request = {'jsonrpc': '2.0', 'method': 'boo', 'id': 1} + id = str(uuid4()) + request = {'jsonrpc': '3.0', 'method': 'boo', 'id': id} response = jsonrpc(request) self.assertEqual( response, - {'jsonrpc': '2.0', 'result': 'Welcome to JSON-RPC', 'id': 1} + {'jsonrpc': '3.0', 'result': 'Welcome to JSON-RPC', 'id': id} ) def test_error(self): - request = {'jsonrpc': '2.0', 'method': 'bla-bla', 'id': 1} + id = str(uuid4()) + request = {'jsonrpc': '3.0', 'method': 'bla-bla', 'id': id} response = jsonrpc(request) + print('test_error.response', response) print(response) self.assertEqual( response, { - 'jsonrpc': '2.0', + 'jsonrpc': '3.0', 'error': { - 'code': -32601, + 'code': 'internal.error', 'message': 'Метод не найден: bla-bla' }, - 'id': 1 + 'id': id } )