Add decorator for renew expired session
This commit is contained in:
parent
1f0c12930d
commit
fe57fa18d1
1 changed files with 13 additions and 8 deletions
|
|
@ -2688,6 +2688,7 @@ class ClientSession(object):
|
|||
sinfo = None
|
||||
finally:
|
||||
self.opts = old_opts
|
||||
|
||||
if not sinfo:
|
||||
err = 'unable to obtain a session'
|
||||
if e_str:
|
||||
|
|
@ -2894,6 +2895,7 @@ class ClientSession(object):
|
|||
return result
|
||||
|
||||
def _renew_session(self):
|
||||
"""Renew expirated session or subsession."""
|
||||
if not hasattr(self, 'auth_method'):
|
||||
raise GenericError("Missing info for reauthentication")
|
||||
# will be deleted by setSession
|
||||
|
|
@ -2906,9 +2908,19 @@ class ClientSession(object):
|
|||
if self.exclusive:
|
||||
self.exclusiveSession()
|
||||
|
||||
def renew_expired_session(func):
|
||||
"""Decorator to renew expirated session or subsession."""
|
||||
def _renew_expired_session(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except AuthExpired:
|
||||
args[0]._renew_session()
|
||||
return func(*args, **kwargs)
|
||||
return _renew_expired_session
|
||||
|
||||
@renew_expired_session
|
||||
def _callMethod(self, name, args, kwargs=None, retry=True):
|
||||
"""Make a call to the hub with retries and other niceties"""
|
||||
|
||||
if self.multicall:
|
||||
if kwargs is None:
|
||||
kwargs = {}
|
||||
|
|
@ -2944,13 +2956,6 @@ class ClientSession(object):
|
|||
# server correctly reporting an outage
|
||||
tries = 0
|
||||
continue
|
||||
elif isinstance(err, AuthExpired):
|
||||
if self.logged_in:
|
||||
self._renew_session()
|
||||
return self._callMethod(name, args, kwargs, retry)
|
||||
else:
|
||||
raise AuthError("Session ID %s is unlogged and expired." %
|
||||
self.sinfo['session-id'])
|
||||
else:
|
||||
raise err
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue