feedback from job
This commit is contained in:
parent
f45a2cba32
commit
908c9a3bfa
1 changed files with 24 additions and 16 deletions
|
@ -6,7 +6,7 @@ from twisted.internet import defer
|
||||||
from twisted.python.failure import Failure
|
from twisted.python.failure import Failure
|
||||||
|
|
||||||
from txjsonrpc import jsonrpclib
|
from txjsonrpc import jsonrpclib
|
||||||
|
from lbrynet.core import log_support
|
||||||
from lbrynet.core.Error import InvalidAuthenticationToken, InvalidHeaderError, SubhandlerError
|
from lbrynet.core.Error import InvalidAuthenticationToken, InvalidHeaderError, SubhandlerError
|
||||||
from lbrynet.conf import settings
|
from lbrynet.conf import settings
|
||||||
from lbrynet.lbrynet_daemon.auth.util import APIKey, get_auth_message
|
from lbrynet.lbrynet_daemon.auth.util import APIKey, get_auth_message
|
||||||
|
@ -23,7 +23,11 @@ def default_decimal(obj):
|
||||||
class JSONRPCException(Exception):
|
class JSONRPCException(Exception):
|
||||||
def __init__(self, err, code):
|
def __init__(self, err, code):
|
||||||
self.faultCode = code
|
self.faultCode = code
|
||||||
self.faultString = err.getTraceback()
|
self.err = err
|
||||||
|
|
||||||
|
@property
|
||||||
|
def faultString(self):
|
||||||
|
return self.err.getTraceback()
|
||||||
|
|
||||||
|
|
||||||
class AuthorizedBase(object):
|
class AuthorizedBase(object):
|
||||||
|
@ -98,7 +102,8 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
return NotImplementedError()
|
return NotImplementedError()
|
||||||
|
|
||||||
def _render_error(self, request, failure, version=jsonrpclib.VERSION_1, response_code=FAILURE):
|
def _render_error(self, request, failure, version=jsonrpclib.VERSION_1, response_code=FAILURE):
|
||||||
fault = jsonrpclib.dumps(JSONRPCException(Failure(failure), response_code), version=version)
|
err = JSONRPCException(Failure(failure), response_code)
|
||||||
|
fault = jsonrpclib.dumps(err, version=version)
|
||||||
self._set_headers(request, fault)
|
self._set_headers(request, fault)
|
||||||
if response_code != AuthJSONRPCServer.FAILURE:
|
if response_code != AuthJSONRPCServer.FAILURE:
|
||||||
request.setResponseCode(response_code)
|
request.setResponseCode(response_code)
|
||||||
|
@ -108,12 +113,12 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
def render(self, request):
|
def render(self, request):
|
||||||
notify_finish = request.notifyFinish()
|
notify_finish = request.notifyFinish()
|
||||||
assert self._check_headers(request), InvalidHeaderError
|
assert self._check_headers(request), InvalidHeaderError
|
||||||
|
|
||||||
session = request.getSession()
|
session = request.getSession()
|
||||||
session_id = session.uid
|
session_id = session.uid
|
||||||
|
|
||||||
if self._use_authentication:
|
if self._use_authentication:
|
||||||
# if this is a new session, send a new secret and set the expiration, otherwise, session.touch()
|
# if this is a new session, send a new secret and set the expiration
|
||||||
|
# otherwise, session.touch()
|
||||||
if self._initialize_session(session_id):
|
if self._initialize_session(session_id):
|
||||||
def expire_session():
|
def expire_session():
|
||||||
self._unregister_user_session(session_id)
|
self._unregister_user_session(session_id)
|
||||||
|
@ -131,7 +136,7 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
try:
|
try:
|
||||||
parsed = jsonrpclib.loads(content)
|
parsed = jsonrpclib.loads(content)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
log.error("Unable to decode request json")
|
log.warning("Unable to decode request json")
|
||||||
self._render_error(request, err)
|
self._render_error(request, err)
|
||||||
return server.NOT_DONE_YET
|
return server.NOT_DONE_YET
|
||||||
|
|
||||||
|
@ -153,8 +158,9 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
try:
|
try:
|
||||||
self._verify_token(session_id, parsed, token)
|
self._verify_token(session_id, parsed, token)
|
||||||
except InvalidAuthenticationToken as err:
|
except InvalidAuthenticationToken as err:
|
||||||
log.error("API validation failed")
|
log.warning("API validation failed")
|
||||||
self._render_error(request, err, version, response_code=AuthJSONRPCServer.UNAUTHORIZED)
|
self._render_error(request, err, version,
|
||||||
|
response_code=AuthJSONRPCServer.UNAUTHORIZED)
|
||||||
return server.NOT_DONE_YET
|
return server.NOT_DONE_YET
|
||||||
self._update_session_secret(session_id)
|
self._update_session_secret(session_id)
|
||||||
reply_with_next_secret = True
|
reply_with_next_secret = True
|
||||||
|
@ -162,11 +168,14 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
try:
|
try:
|
||||||
function = self._get_jsonrpc_method(function_name)
|
function = self._get_jsonrpc_method(function_name)
|
||||||
except AttributeError as err:
|
except AttributeError as err:
|
||||||
log.error("Unknown method: %s", function_name)
|
log.warning("Unknown method: %s", function_name)
|
||||||
self._render_error(request, err, version)
|
self._render_error(request, err, version)
|
||||||
return server.NOT_DONE_YET
|
return server.NOT_DONE_YET
|
||||||
|
|
||||||
d = defer.maybeDeferred(function) if args == [{}] else defer.maybeDeferred(function, *args)
|
if args == [{}]:
|
||||||
|
d = defer.maybeDeferred(function)
|
||||||
|
else:
|
||||||
|
d = defer.maybeDeferred(function, *args)
|
||||||
|
|
||||||
# cancel the response if the connection is broken
|
# cancel the response if the connection is broken
|
||||||
notify_finish.addErrback(self._response_failed, d)
|
notify_finish.addErrback(self._response_failed, d)
|
||||||
|
@ -226,7 +235,8 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _get_jsonrpc_method(self, function_path):
|
def _get_jsonrpc_method(self, function_path):
|
||||||
assert self._check_function_path(function_path), AttributeError(function_path)
|
if not self._check_function_path(function_path):
|
||||||
|
raise AttributeError(function_path)
|
||||||
return self.callable_methods.get(function_path)
|
return self.callable_methods.get(function_path)
|
||||||
|
|
||||||
def _initialize_session(self, session_id):
|
def _initialize_session(self, session_id):
|
||||||
|
@ -255,11 +265,8 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
|
|
||||||
def _run_subhandlers(self, request):
|
def _run_subhandlers(self, request):
|
||||||
for handler in self.subhandlers:
|
for handler in self.subhandlers:
|
||||||
try:
|
if not handler(request):
|
||||||
assert handler(request)
|
raise SubhandlerError("Subhandler error processing request: %s", request)
|
||||||
except Exception as err:
|
|
||||||
log.error(err.message)
|
|
||||||
raise SubhandlerError(err.message)
|
|
||||||
|
|
||||||
def _callback_render(self, result, request, version, auth_required=False):
|
def _callback_render(self, result, request, version, auth_required=False):
|
||||||
result_for_return = result if not isinstance(result, dict) else result['result']
|
result_for_return = result if not isinstance(result, dict) else result['result']
|
||||||
|
@ -273,6 +280,7 @@ class AuthJSONRPCServer(AuthorizedBase):
|
||||||
self._set_headers(request, encoded_message, auth_required)
|
self._set_headers(request, encoded_message, auth_required)
|
||||||
self._render_message(request, encoded_message)
|
self._render_message(request, encoded_message)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
log.exception(err.message)
|
||||||
self._render_error(request, err, response_code=self.FAILURE, version=version)
|
self._render_error(request, err, response_code=self.FAILURE, version=version)
|
||||||
|
|
||||||
def _render_response(self, result, code):
|
def _render_response(self, result, code):
|
||||||
|
|
Loading…
Reference in a new issue