From 2f329c74b99e28ad98a712483f3750ce7e368f79 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 17 Jul 2024 21:44:27 +0530 Subject: [PATCH] chore: oauth sdk implementation --- twilio/base/client_base.py | 14 +++++++++++--- twilio/base/domain.py | 2 ++ twilio/base/oauth_token_base.py | 7 +++---- twilio/base/version.py | 15 +++++++++++++++ twilio/http/bearer_token_http_client.py | 2 +- twilio/twilio_bearer_token_auth.py | 5 ++--- twilio/twilio_no_auth.py | 2 +- 7 files changed, 35 insertions(+), 12 deletions(-) diff --git a/twilio/base/client_base.py b/twilio/base/client_base.py index c2fc14af6..700e420ad 100644 --- a/twilio/base/client_base.py +++ b/twilio/base/client_base.py @@ -93,7 +93,9 @@ def request( uri = self.get_hostname(uri) if is_oauth: OauthTokenBase = dynamic_import("twilio.base.oauth_token_base", "OauthTokenBase") - headers['Authorization'] = f'Bearer {OauthTokenBase().get_oauth_token(domain, "v1", self.username, self.password)}' + token = OauthTokenBase().get_oauth_token(domain, "v1", self.username, self.password) + headers['Authorization'] = f'Bearer {token}' + headers.get('Authorization') return self.http_client.request( method, @@ -116,6 +118,7 @@ async def request_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Response: """ Asynchronously makes a request to the Twilio API using the configured http client @@ -137,10 +140,15 @@ async def request_async( raise RuntimeError( "http_client must be asynchronous to support async API requests" ) - - auth = self.get_auth(auth) + if not is_oauth: + auth = self.get_auth(auth) headers = self.get_headers(method, headers) uri = self.get_hostname(uri) + if is_oauth: + OauthTokenBase = dynamic_import("twilio.base.oauth_token_base", "OauthTokenBase") + token = OauthTokenBase().get_oauth_token(domain, "v1", self.username, self.password) + headers['Authorization'] = f'Bearer {token}' + headers.get('Authorization') return await self.http_client.request( method, diff --git a/twilio/base/domain.py b/twilio/base/domain.py index 51375e55c..02f498685 100644 --- a/twilio/base/domain.py +++ b/twilio/base/domain.py @@ -70,6 +70,7 @@ async def request_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Response: """ Makes an asynchronous HTTP request to this domain. @@ -93,4 +94,5 @@ async def request_async( auth=auth, timeout=timeout, allow_redirects=allow_redirects, + is_oauth=is_oauth ) diff --git a/twilio/base/oauth_token_base.py b/twilio/base/oauth_token_base.py index 3576246a8..91e6d5191 100644 --- a/twilio/base/oauth_token_base.py +++ b/twilio/base/oauth_token_base.py @@ -15,11 +15,10 @@ def get_oauth_token(self, domain: str, version: str, username: str, password: st Client = dynamic_import("twilio.rest", "Client") try: orgs_token_manager = TokenManagerInitializer.get_token_manager() - BearerTokenHTTPClient(orgs_token_manager).get_headers(Version(Domain(Client(username, password), domain), version)) - return orgs_token_manager.fetch_access_token(version=version) + return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version)) except Exception: orgs_token_manager = OrgTokenManager(grant_type='client_credentials', client_id=username, client_secret=password) - BearerTokenHTTPClient(orgs_token_manager).get_headers(Version(Domain(Client(username, password), domain), version)) - return orgs_token_manager.fetch_access_token(version=Version(Domain(Client(username, password), domain), version)) \ No newline at end of file + TokenManagerInitializer().set_token_manager(orgs_token_manager) + return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version)) \ No newline at end of file diff --git a/twilio/base/version.py b/twilio/base/version.py index 9177a18ed..e7ddaff6c 100644 --- a/twilio/base/version.py +++ b/twilio/base/version.py @@ -67,6 +67,7 @@ async def request_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Response: """ Make an asynchronous HTTP request @@ -81,6 +82,7 @@ async def request_async( auth=auth, timeout=timeout, allow_redirects=allow_redirects, + is_oauth=is_oauth ) @classmethod @@ -154,6 +156,7 @@ async def fetch_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Any: """ Asynchronously fetch a resource instance. @@ -167,6 +170,7 @@ async def fetch_async( auth=auth, timeout=timeout, allow_redirects=allow_redirects, + is_oauth=is_oauth ) return self._parse_fetch(method, uri, response) @@ -190,6 +194,7 @@ def update( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Any: """ Update a resource instance. @@ -217,6 +222,7 @@ async def update_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Any: """ Asynchronously update a resource instance. @@ -230,6 +236,7 @@ async def update_async( auth=auth, timeout=timeout, allow_redirects=allow_redirects, + is_oauth=is_oauth ) return self._parse_update(method, uri, response) @@ -253,6 +260,7 @@ def delete( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> bool: """ Delete a resource. @@ -280,6 +288,7 @@ async def delete_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> bool: """ Asynchronously delete a resource. @@ -293,6 +302,7 @@ async def delete_async( auth=auth, timeout=timeout, allow_redirects=allow_redirects, + is_oauth=is_oauth ) return self._parse_delete(method, uri, response) @@ -351,6 +361,7 @@ async def page_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Response: """ Makes an asynchronous HTTP request. @@ -364,6 +375,7 @@ async def page_async( auth=auth, timeout=timeout, allow_redirects=allow_redirects, + is_oauth=is_oauth ) def stream( @@ -451,6 +463,7 @@ def create( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Any: """ Create a resource instance. @@ -478,6 +491,7 @@ async def create_async( auth: Optional[Tuple[str, str]] = None, timeout: Optional[float] = None, allow_redirects: bool = False, + is_oauth: bool = False, ) -> Any: """ Asynchronously create a resource instance. @@ -491,6 +505,7 @@ async def create_async( auth=auth, timeout=timeout, allow_redirects=allow_redirects, + is_oauth=is_oauth ) return self._parse_create(method, uri, response) diff --git a/twilio/http/bearer_token_http_client.py b/twilio/http/bearer_token_http_client.py index 784210c83..eb5758db9 100644 --- a/twilio/http/bearer_token_http_client.py +++ b/twilio/http/bearer_token_http_client.py @@ -10,7 +10,7 @@ class BearerTokenHTTPClient: def __init__(self, orgs_token_manager: TokenManager): self.orgs_token_manager = orgs_token_manager - def get_headers(self, version: Version): + def get_access_token(self, version: Version): if TwilioBearerTokenAuth.get_access_token() is None or self.is_token_expired( TwilioBearerTokenAuth.get_access_token() ): diff --git a/twilio/twilio_bearer_token_auth.py b/twilio/twilio_bearer_token_auth.py index 90c8b9c9f..ffc8814cd 100644 --- a/twilio/twilio_bearer_token_auth.py +++ b/twilio/twilio_bearer_token_auth.py @@ -1,5 +1,4 @@ from threading import Lock -from concurrent.futures import ThreadPoolExecutor class BearerTokenTwilioRestClient: @@ -13,7 +12,7 @@ class TwilioBearerTokenAuth: user_agent_extensions = None region = None edge = None - + @classmethod def init(cls, access_token): with cls._lock: @@ -31,4 +30,4 @@ def get_access_token(cls): @classmethod def get_header_param(cls): with cls._lock: - return {"Authorization": "Bearer {token}".format(token=cls.access_token)} \ No newline at end of file + return {"Authorization": "Bearer {token}".format(token=cls.access_token)} diff --git a/twilio/twilio_no_auth.py b/twilio/twilio_no_auth.py index 92ab2c509..9ea2fc907 100644 --- a/twilio/twilio_no_auth.py +++ b/twilio/twilio_no_auth.py @@ -30,4 +30,4 @@ def build_oauth_rest_client(cls): builder.region(cls.region) builder.edge(cls.edge) - return builder.build() \ No newline at end of file + return builder.build()