Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Check for passwordchange.html. (#102)
Browse files Browse the repository at this point in the history
* Add exception handling.

* Add simple versioning.
  • Loading branch information
kagrski authored Jan 25, 2023
1 parent 0d8dfbd commit 63ff5b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "vmngclient"
version = "0.5.2"
version = "0.5.3"
description = "Universal vManage API"
authors = ["kagorski <[email protected]>"]
readme = "README.md"
Expand Down
5 changes: 5 additions & 0 deletions vmngclient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import logging.config
from importlib import metadata
from pathlib import Path
from typing import Final

Expand All @@ -9,3 +10,7 @@

if not vmngclient_logger.handlers:
logging.config.fileConfig(LOGGING_CONF_DIR, disable_existing_loggers=False)

__version__ = metadata.version(__package__)

vmngclient_logger.debug(f"vmngclient {__version__}")
17 changes: 12 additions & 5 deletions vmngclient/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from requests.exceptions import ConnectionError, HTTPError
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed # type: ignore

from vmngclient.exceptions import InvalidOperationError
from vmngclient.utils.response import response_debug
from vmngclient.vmanage_auth import vManageAuth

Expand Down Expand Up @@ -72,7 +73,12 @@ def create_vManageSession(
tenant_id = session.get_tenant_id()
vsession_id = session.get_virtual_session_id(tenant_id)
session.headers.update({"VSessionId": vsession_id})
server_info = session.server()

try:
server_info = session.server()
except InvalidOperationError:
server_info = {}

session.server_name = server_info.get("server")
session.on_session_create_hook()

Expand Down Expand Up @@ -100,10 +106,7 @@ def create_vManageSession(
)
else:
session.session_type = SessionType.NOT_DEFINED
session.logger.warning(
f"Session created with {user_mode.value} user mode and {view_mode.value} view mode.\n"
f"Session type set to not defined"
)
session.logger.warning(f"Session created with {user_mode} and {view_mode}.")

session.logger.info(f"Logged as {username}. The session type is {session.session_type}")
return session
Expand Down Expand Up @@ -151,6 +154,10 @@ def request(self, method, url, *args, **kwargs) -> Any:
full_url = self.get_full_url(url)
response = super(vManageSession, self).request(method, full_url, *args, **kwargs)
self.logger.debug(response_debug(response))

if response.request.url and "passwordReset.html" in response.request.url:
raise InvalidOperationError("Password must be changed to use this session.")

try:
response.raise_for_status()
except HTTPError as error:
Expand Down

0 comments on commit 63ff5b7

Please sign in to comment.