chainlib/chainlib/jsonrpc.py

44 lines
869 B
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# standard imports
import uuid
# local imports
from .error import JSONRPCException
class DefaultErrorParser:
def translate(self, error):
return JSONRPCException('default parser code {}'.format(error))
def jsonrpc_template():
return {
'jsonrpc': '2.0',
'id': str(uuid.uuid4()),
'method': None,
'params': [],
}
def jsonrpc_result(o, ep):
if o.get('error') != None:
raise ep.translate(o)
return o['result']
def jsonrpc_response(request_id, result):
return {
'jsonrpc': '2.0',
'id': request_id,
'result': result,
}
def jsonrpc_error(request_id, code=-32000, message='Server error'):
return {
'jsonrpc': '2.0',
'id': request_id,
'error': {
'code': code,
'message': message,
},
}