FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fixed bugs and did some restructuring. · python-webuntis/python-webuntis@99c74f4 · GitHub

Repository navigation

Commit 99c74f4

Browse files
committed
Fixed bugs and did some restructuring.
1 parent f71c6a8 commit 99c74f4

1 file changed

Lines changed: 33 additions & 20 deletions

File tree

‎webuntis/session.py‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ def __init__(self, session, method, params=None):
4040
self._params = params or {}
4141

4242
def request(self):
43-
data = None
44-
i = 0
45-
while data is None:
46-
i += 1
47-
try:
48-
data = self._make_request()
49-
except errors.NotLoggedInError as e:
50-
if self._session.options['login_repeat'] >= i:
51-
self._session.logout(suppress_errors=True)
52-
self._session.login()
53-
else:
54-
raise e
55-
56-
return data
57-
58-
def _make_request(self):
5943
'''
6044
A method for sending a JSON-RPC request.
6145
@@ -266,19 +250,48 @@ def login(self):
266250

267251
return self
268252

253+
def _make_cache_key(self, method, params):
254+
'''A helper method for _request that generates a hashable object out of
255+
a string and a dictionary.
256+
257+
It doesn't use ``hash()`` or similar methods because it's neat that the
258+
keys are human-readable and enable us to trace back the origin of the
259+
key. Python does that anyway under the hood when using it as a
260+
dictionary key.
261+
'''
262+
263+
return (method, frozenset((params or {}).items()))
264+
269265
def _request(self, method, params=None):
270266
'''A wrapper for :py:class:`JSONRPCRequest` using the LRU Cache'''
271267

272268
if self._cache is None:
273269
return self._make_request(method, params)
274270
else:
275-
key = (method, hash(tuple(params or {})))
271+
key = self._make_cache_key(method, params)
276272
if key not in self._cache:
277-
self._cache[key] = self._make_request(method, params)
273+
self._cache[key] = self._make_request(method, params, use_login_repeat=True)
278274
return self._cache[key]
279275

280-
def _make_request(self, method, params=None):
281-
return JSONRPCRequest(self, method, params).request()
276+
def _make_request(self, method, params=None, use_login_repeat=False):
277+
attempts_left = self.options['login_repeat'] if use_login_repeat else 1
278+
279+
data = None
280+
281+
while data is None:
282+
try:
283+
data = JSONRPCRequest(self, method, params).request()
284+
except errors.NotLoggedInError as e:
285+
if attempts_left > 0:
286+
self.logout(suppress_errors=True)
287+
self.login()
288+
else:
289+
raise errors.NotLoggedInError('Tried to login several times, failed. Original method was ' + method)
290+
else:
291+
return data
292+
293+
attempts_left -= 1 # new round!
294+
282295

283296

284297
class Session(JSONRPCSession):

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL