Skip to content

Commit 91aa639

Browse files
committed
make login more robust against server errors
1 parent d7a9f0a commit 91aa639

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

weconnect/weconnect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ def login(self) -> None: # noqa: C901 # pylint: disable=R0914, R0912, too-many-
286286

287287
tryLoginResponse: requests.Response = self.__session.get(tryLoginUrl, allow_redirects=False)
288288
if tryLoginResponse.status_code != requests.codes['see_other']:
289+
if tryLoginResponse.status_code == requests.codes['internal_server_error']:
290+
raise RetrievalError('Temporary server error during login')
289291
raise APICompatibilityError('Forwarding to login page expected (status code 303),'
290292
f' but got status code {tryLoginResponse.status_code}')
291293
if 'Location' not in tryLoginResponse.headers:
@@ -314,6 +316,8 @@ def login(self) -> None: # noqa: C901 # pylint: disable=R0914, R0912, too-many-
314316

315317
if params is None:
316318
if loginResponse.status_code != requests.codes['ok']:
319+
if loginResponse.status_code == requests.codes['internal_server_error']:
320+
raise RetrievalError('Temporary server error during login')
317321
raise APICompatibilityError('Retrieving login page was not successfull,'
318322
f' status code: {loginResponse.status_code}')
319323

@@ -347,6 +351,8 @@ def login(self) -> None: # noqa: C901 # pylint: disable=R0914, R0912, too-many-
347351
# Post form content and retrieve credentials page
348352
login2Response: requests.Response = self.__session.post(login2Url, headers=loginHeadersForm, data=formData, allow_redirects=True)
349353
if login2Response.status_code != requests.codes['ok']: # pylint: disable=E1101
354+
if login2Response.status_code == requests.codes['internal_server_error']:
355+
raise RetrievalError('Temporary server error during login')
350356
raise APICompatibilityError('Retrieving credentials page was not successfull,'
351357
f' status code: {login2Response.status_code}')
352358

@@ -387,6 +393,8 @@ def login(self) -> None: # noqa: C901 # pylint: disable=R0914, R0912, too-many-
387393
# Post form content and retrieve userId in forwarding Location
388394
login3Response: requests.Response = self.__session.post(login3Url, headers=loginHeadersForm, data=form2Data, allow_redirects=False)
389395
if login3Response.status_code not in (requests.codes['found'], requests.codes['see_other']):
396+
if login3Response.status_code == requests.codes['internal_server_error']:
397+
raise RetrievalError('Temporary server error during login')
390398
raise APICompatibilityError('Forwarding expected (status code 302),'
391399
f' but got status code {login3Response.status_code}')
392400
if 'Location' not in login3Response.headers:
@@ -479,6 +487,8 @@ def login(self) -> None: # noqa: C901 # pylint: disable=R0914, R0912, too-many-
479487
self.__rToken['token'] = data['refreshToken']
480488
self.__rToken['expires'] = datetime.utcnow().replace(tzinfo=timezone.utc) + timedelta(seconds=3600)
481489
else:
490+
if tokenResponse.status_code == requests.codes['internal_server_error']:
491+
raise RetrievalError('Temporary server error during login')
482492
raise AuthentificationError(f'There was a problem fetching tokens during login. Status code was {tokenResponse.status_code}')
483493

484494
LOG.info('Login successful')

0 commit comments

Comments
 (0)