This commit is contained in:
@@ -18,7 +18,7 @@ class Response:
|
||||
|
||||
def __json__(self):
|
||||
response = {
|
||||
"jsonrpc": "2.0",
|
||||
"jsonrpc": "3.0",
|
||||
"result": self.result,
|
||||
"id": self.id,
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class Response:
|
||||
|
||||
|
||||
class Method:
|
||||
def __init__(self, function, middlewares=[], debug: bool = False):
|
||||
def __init__(self, function, middlewares=None, debug: bool = False):
|
||||
self.function = function
|
||||
self.middlewares = middlewares
|
||||
self.debug = debug
|
||||
@@ -126,51 +126,48 @@ class JSONRPC:
|
||||
method = self.methods[name]
|
||||
sig = signature(method.function)
|
||||
|
||||
result = {
|
||||
"jsonrpc": "3.0",
|
||||
"method": name,
|
||||
"id": '00000000-0000-0000-0000-000000000000'
|
||||
}
|
||||
if len(sig.parameters) == 0:
|
||||
result = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": name,
|
||||
"id": 1
|
||||
}
|
||||
else:
|
||||
params = {}
|
||||
for key in sig.parameters:
|
||||
params[key] = ''
|
||||
result = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": name,
|
||||
"params": params,
|
||||
"id": 1
|
||||
}
|
||||
result["params"] = params
|
||||
|
||||
return result
|
||||
|
||||
def validate(self, query):
|
||||
"""Валидация запроса
|
||||
"""
|
||||
if 'method' not in query:
|
||||
required = [
|
||||
'id', 'method', 'jsonrpc'
|
||||
]
|
||||
for field in required:
|
||||
if field not in query:
|
||||
result = InvalidRequestError(
|
||||
message=f'Некорректный запрос: {query}'
|
||||
)
|
||||
return result
|
||||
if query['jsonrpc'] not in ['2.0', '3.0']:
|
||||
result = InvalidRequestError(
|
||||
message=f'Некорректный запрос: {query}'
|
||||
)
|
||||
if 'id' in query:
|
||||
result.id = query['id']
|
||||
return result
|
||||
|
||||
def process(self, query):
|
||||
"""Выполнение метода
|
||||
"""
|
||||
result = self.validate(query)
|
||||
if isinstance(result, JSONRPCError):
|
||||
if result:
|
||||
return result
|
||||
name = query['method']
|
||||
if name not in self.methods:
|
||||
if 'id' in query:
|
||||
__id = query['id']
|
||||
else:
|
||||
__id = None
|
||||
result = MethodNotFoundError(
|
||||
message=f'Метод не найден: {name}',
|
||||
id=__id
|
||||
id=query['id']
|
||||
)
|
||||
return result
|
||||
|
||||
@@ -195,13 +192,10 @@ class JSONRPC:
|
||||
# message=traceback.format_exc()
|
||||
)
|
||||
else:
|
||||
if 'id' in query:
|
||||
response = Response(
|
||||
id=query['id'],
|
||||
result=response
|
||||
)
|
||||
else:
|
||||
return
|
||||
response = Response(
|
||||
id=query['id'],
|
||||
result=response
|
||||
)
|
||||
result = response
|
||||
return result
|
||||
|
||||
|
||||
@@ -31,13 +31,14 @@ class Client:
|
||||
data=payload,
|
||||
headers=self.headers
|
||||
)
|
||||
print('content', response.content)
|
||||
if self.debug:
|
||||
log.debug('content', response.content)
|
||||
result = response.json()
|
||||
|
||||
assert 'jsonrpc' in result
|
||||
assert 'id' in result
|
||||
assert result["jsonrpc"] in ['2.0', '3.0']
|
||||
if '3.0' in result["jsonrpc"]:
|
||||
assert 'meta' in result
|
||||
# if '3.0' in result["jsonrpc"]:
|
||||
# assert 'meta' in result
|
||||
|
||||
return result
|
||||
|
||||
@@ -1,54 +1,60 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
ID = '00000000-0000-0000-0000-000000000000'
|
||||
|
||||
|
||||
class JSONRPCError(Exception):
|
||||
def __init__(self, id: int, message):
|
||||
pass
|
||||
def __init__(self, message: str = "jsonrpc.error", id: str = ID):
|
||||
# pass
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = self.CODE
|
||||
|
||||
def __json__(self):
|
||||
result = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": self.id,
|
||||
"jsonrpc": "3.0",
|
||||
"error": {
|
||||
"code": self.CODE,
|
||||
"message": self.message
|
||||
}
|
||||
},
|
||||
"id": self.id
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
class InvalidRequestError(JSONRPCError):
|
||||
CODE = -32600
|
||||
CODE = "request.invalid"
|
||||
|
||||
def __init__(self, id: int, message: str = 'Invalid Request'):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = CODE
|
||||
|
||||
|
||||
class ParseError(JSONRPCError):
|
||||
CODE = -32700
|
||||
|
||||
def __init__(self, id: int, message: str = 'Parse error'):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = CODE
|
||||
|
||||
|
||||
class MethodNotFoundError(JSONRPCError):
|
||||
CODE = -32601
|
||||
|
||||
def __init__(self, id: int, message: str = 'Method not found'):
|
||||
def __init__(self, message: str = 'Invalid Request', id: str = ID):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = self.CODE
|
||||
|
||||
|
||||
class InvalidParamsError(JSONRPCError):
|
||||
CODE = -32602
|
||||
class ParseError(JSONRPCError):
|
||||
CODE = "parse.error"
|
||||
|
||||
def __init__(self, id: int, message: str = 'Invalid params'):
|
||||
def __init__(self, message: str = 'Parse error', id = ID):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = self.CODE
|
||||
|
||||
|
||||
class MethodNotFoundError(JSONRPCError):
|
||||
CODE = "method.notfound"
|
||||
|
||||
def __init__(self, message: str = 'Method not found', id = ID):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = self.CODE
|
||||
|
||||
|
||||
|
||||
class InvalidParamsError(JSONRPCError):
|
||||
CODE = "params.invalid"
|
||||
|
||||
def __init__(self, message: str = 'Invalid params', id = ID):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = self.CODE
|
||||
@@ -57,9 +63,9 @@ class InvalidParamsError(JSONRPCError):
|
||||
class InternalError(JSONRPCError):
|
||||
"""Internal JSON-RPC error
|
||||
"""
|
||||
CODE = -32603
|
||||
CODE = "internal.error"
|
||||
|
||||
def __init__(self, id: int, message: str = 'Internal error'):
|
||||
def __init__(self, message: str = 'Internal error', id = ID):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = self.CODE
|
||||
@@ -67,12 +73,10 @@ class InternalError(JSONRPCError):
|
||||
|
||||
class ServerError(JSONRPCError):
|
||||
"""Reserved for implementation-defined server-errors.
|
||||
|
||||
code: -32000 to -32099 Server error.
|
||||
"""
|
||||
CODE = -32000
|
||||
CODE = "server.error"
|
||||
|
||||
def __init__(self, id: int, message: str = 'Server error'):
|
||||
def __init__(self, message: str = 'Server error', id = ID):
|
||||
self.id = id
|
||||
self.message = message
|
||||
self.code = self.CODE
|
||||
|
||||
Reference in New Issue
Block a user