From dc286afc0a01bebbfa84ada4e6a0d2b9082afbe6 Mon Sep 17 00:00:00 2001 From: sbasan Date: Thu, 7 Mar 2024 14:26:55 +0100 Subject: [PATCH] api and endpoints containers as cached properties --- catalystwan/session.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/catalystwan/session.py b/catalystwan/session.py index ac6f74a2..a30c9e1d 100644 --- a/catalystwan/session.py +++ b/catalystwan/session.py @@ -4,9 +4,10 @@ import logging from enum import Enum +from functools import cached_property from pathlib import Path from time import monotonic, sleep -from typing import Any, Callable, ClassVar, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Callable, ClassVar, Dict, List, Optional, Union from urllib.parse import urljoin, urlparse, urlunparse from packaging.version import Version # type: ignore @@ -15,10 +16,8 @@ from requests.exceptions import ConnectionError, HTTPError, RequestException from catalystwan import USER_AGENT -from catalystwan.api.api_container import APIContainer from catalystwan.endpoints import APIEndpointClient from catalystwan.endpoints.client import AboutInfo, ServerInfo -from catalystwan.endpoints.endpoints_container import APIEndpointContainter from catalystwan.exceptions import ( DefaultPasswordError, ManagerHTTPError, @@ -35,6 +34,10 @@ JSON = Union[Dict[str, "JSON"], List["JSON"], str, int, float, bool, None] +if TYPE_CHECKING: + from catalystwan.api.api_container import APIContainer + from catalystwan.endpoints.endpoints_container import APIEndpointContainter + class UserMode(str, Enum): PROVIDER = "provider" @@ -171,8 +174,6 @@ def __init__( super(ManagerSession, self).__init__() self.headers.update({"User-Agent": USER_AGENT}) self.__prepare_session(verify, auth) - self.api = APIContainer(self) - self.endpoints = APIEndpointContainter(self) self._platform_version: str = "" self._api_version: Version self._state: ManagerSessionState = ManagerSessionState.OPERATIVE @@ -180,6 +181,20 @@ def __init__( self.polling_requests_timeout: int = 10 self._validate_responses = validate_responses + @cached_property + def api(self) -> APIContainer: + from catalystwan.api.api_container import APIContainer + + self._api = APIContainer(self) + return self._api + + @cached_property + def endpoints(self) -> APIEndpointContainter: + from catalystwan.endpoints.endpoints_container import APIEndpointContainter + + self._endpoints = APIEndpointContainter(self) + return self._endpoints + @property def state(self) -> ManagerSessionState: return self._state