From b973065d28328adebdccb71c63a91dd435b08deb Mon Sep 17 00:00:00 2001 From: AsabuHere Date: Tue, 1 Oct 2024 22:36:51 +0530 Subject: [PATCH] Uptake of review comments --- .../{authStrategy => auth_strategy}/__init__.py | 0 .../auth_strategy.py} | 2 +- .../authType.py => auth_strategy/auth_type.py} | 2 +- .../no_auth_strategy.py} | 0 .../token_auth_strategy.py} | 11 +++++++---- twilio/base/client_base.py | 12 +++++------- ...dentialProvider.py => credential_provider.py} | 2 +- ...alProvider.py => orgs_credential_provider.py} | 6 +++--- twilio/http/token_manager_initializer.py | 16 ---------------- .../rest/preview_iam/organizations/__init__.py | 2 +- 10 files changed, 19 insertions(+), 34 deletions(-) rename twilio/{authStrategy => auth_strategy}/__init__.py (100%) rename twilio/{authStrategy/authStrategy.py => auth_strategy/auth_strategy.py} (90%) rename twilio/{authStrategy/authType.py => auth_strategy/auth_type.py} (86%) rename twilio/{authStrategy/noAuthStrategy.py => auth_strategy/no_auth_strategy.py} (100%) rename twilio/{authStrategy/tokenAuthStrategy.py => auth_strategy/token_auth_strategy.py} (78%) rename twilio/credential/{credentialProvider.py => credential_provider.py} (85%) rename twilio/credential/{orgsCredentialProvider.py => orgs_credential_provider.py} (80%) delete mode 100644 twilio/http/token_manager_initializer.py diff --git a/twilio/authStrategy/__init__.py b/twilio/auth_strategy/__init__.py similarity index 100% rename from twilio/authStrategy/__init__.py rename to twilio/auth_strategy/__init__.py diff --git a/twilio/authStrategy/authStrategy.py b/twilio/auth_strategy/auth_strategy.py similarity index 90% rename from twilio/authStrategy/authStrategy.py rename to twilio/auth_strategy/auth_strategy.py index 901a6b4b74..63107ef974 100644 --- a/twilio/authStrategy/authStrategy.py +++ b/twilio/auth_strategy/auth_strategy.py @@ -1,4 +1,4 @@ -from twilio.authStrategy.authType import AuthType +from twilio.auth_strategy.auth_type import AuthType from enum import Enum from abc import abstractmethod diff --git a/twilio/authStrategy/authType.py b/twilio/auth_strategy/auth_type.py similarity index 86% rename from twilio/authStrategy/authType.py rename to twilio/auth_strategy/auth_type.py index fe9f002039..83653b7565 100644 --- a/twilio/authStrategy/authType.py +++ b/twilio/auth_strategy/auth_type.py @@ -1,7 +1,7 @@ from enum import Enum class AuthType(Enum): - TOKEN = 'token' + ORGS_TOKEN = 'orgs_stoken' NO_AUTH = 'noauth' BASIC = 'basic' API_KEY = 'api_key' diff --git a/twilio/authStrategy/noAuthStrategy.py b/twilio/auth_strategy/no_auth_strategy.py similarity index 100% rename from twilio/authStrategy/noAuthStrategy.py rename to twilio/auth_strategy/no_auth_strategy.py diff --git a/twilio/authStrategy/tokenAuthStrategy.py b/twilio/auth_strategy/token_auth_strategy.py similarity index 78% rename from twilio/authStrategy/tokenAuthStrategy.py rename to twilio/auth_strategy/token_auth_strategy.py index ac8acafcb6..88b0d787fc 100644 --- a/twilio/authStrategy/tokenAuthStrategy.py +++ b/twilio/auth_strategy/token_auth_strategy.py @@ -1,18 +1,21 @@ import jwt import threading +import logging from datetime import datetime, timedelta -from twilio.authStrategy.authType import AuthType -from twilio.authStrategy.authStrategy import AuthStrategy +from twilio.auth_strategy.auth_type import AuthType +from twilio.auth_strategy.auth_strategy import AuthStrategy from twilio.http.token_manager import TokenManager class TokenAuthStrategy(AuthStrategy): def __init__(self, token_manager: TokenManager): - super().__init__(AuthType.TOKEN) + super().__init__(AuthType.ORGS_TOKEN) self.token_manager = token_manager self.token = None self.lock = threading.Lock() + logging.basicConfig(level=logging.INFO) + self.logger = logging.getLogger(__name__) def get_auth_string(self) -> str: if self.token is None: @@ -23,7 +26,7 @@ def requires_authentication(self) -> bool: return True def fetch_token(self): - print(f'token is fetch_token {self.token}') + self.logger.info("New token fetched for accessing organization API") if self.token is None or self.token == "" or self.is_token_expired(self.token): with self.lock: if self.token is None or self.token == "" or self.is_token_expired(self.token): diff --git a/twilio/base/client_base.py b/twilio/base/client_base.py index 2d18e88990..1fdc689eae 100644 --- a/twilio/base/client_base.py +++ b/twilio/base/client_base.py @@ -7,8 +7,8 @@ from twilio.http import HttpClient from twilio.http.http_client import TwilioHttpClient from twilio.http.response import Response -from twilio.authStrategy.authType import AuthType -from twilio.credential.credentialProvider import CredentialProvider +from twilio.auth_strategy.auth_type import AuthType +from twilio.credential.credential_provider import CredentialProvider class ClientBase(object): @@ -93,7 +93,6 @@ def request( ##If credential provider is provided by user, get the associated auth strategy ##Using the auth strategy, fetch the auth string and set it to authorization header - auth_strategy = None ##Initialization if self.credential_provider: auth_strategy = self.credential_provider.to_auth_strategy() headers["Authorization"] = auth_strategy.get_auth_string() @@ -151,15 +150,14 @@ async def request_async( ##If credential provider is provided by user, get the associated auth strategy ##Using the auth strategy, fetch the auth string and set it to authorization header - auth_strategy = None ##Initialization if self.credential_provider: auth_strategy = self.credential_provider.to_auth_strategy() - if auth_strategy.auth_type == AuthType.TOKEN: - auth_strategy.fetch_token() - headers["Authorization"] = auth_strategy.get_auth_string() + headers["Authorization"] = auth_strategy.get_auth_string() else: auth = self.get_auth(auth) + uri = self.get_hostname(uri) + return await self.http_client.request( method, uri, diff --git a/twilio/credential/credentialProvider.py b/twilio/credential/credential_provider.py similarity index 85% rename from twilio/credential/credentialProvider.py rename to twilio/credential/credential_provider.py index 2fc103f029..27e6a7eb44 100644 --- a/twilio/credential/credentialProvider.py +++ b/twilio/credential/credential_provider.py @@ -1,4 +1,4 @@ -from twilio.authStrategy.authType import AuthType +from twilio.auth_strategy.auth_type import AuthType class CredentialProvider: def __init__(self, auth_type: AuthType): diff --git a/twilio/credential/orgsCredentialProvider.py b/twilio/credential/orgs_credential_provider.py similarity index 80% rename from twilio/credential/orgsCredentialProvider.py rename to twilio/credential/orgs_credential_provider.py index 6668a18d4f..10d83d32ca 100644 --- a/twilio/credential/orgsCredentialProvider.py +++ b/twilio/credential/orgs_credential_provider.py @@ -2,9 +2,9 @@ from twilio.http.orgs_token_manager import OrgTokenManager from twilio.base.exceptions import TwilioException -from twilio.credential.credentialProvider import CredentialProvider -from twilio.authStrategy.authType import AuthType -from twilio.authStrategy.tokenAuthStrategy import TokenAuthStrategy +from twilio.credential.credential_provider import CredentialProvider +from twilio.auth_strategy.auth_type import AuthType +from twilio.auth_strategy.token_auth_strategy import TokenAuthStrategy class OrgsCredentialProvider(CredentialProvider): diff --git a/twilio/http/token_manager_initializer.py b/twilio/http/token_manager_initializer.py deleted file mode 100644 index 36ee83f1a5..0000000000 --- a/twilio/http/token_manager_initializer.py +++ /dev/null @@ -1,16 +0,0 @@ -from twilio.http.token_manager import TokenManager - - -class TokenManagerInitializer: - - org_token_manager = None - - @classmethod - def set_token_manager(cls, token_manager: TokenManager): - cls.org_token_manager = token_manager - - @classmethod - def get_token_manager(cls): - if cls.org_token_manager is None: - raise Exception("Token Manager not initialized") - return cls.org_token_manager diff --git a/twilio/rest/preview_iam/organizations/__init__.py b/twilio/rest/preview_iam/organizations/__init__.py index 70e1772efd..0dcd287346 100644 --- a/twilio/rest/preview_iam/organizations/__init__.py +++ b/twilio/rest/preview_iam/organizations/__init__.py @@ -35,7 +35,7 @@ def __init__(self, domain: Domain): @property def accounts(self) -> AccountList: if self._accounts is None: - self._accounts = AccountList(self) + self._accounts = AccountList(self, "OR64adedc0f4dc99b9113305f725677b47") return self._accounts