Skip to content

Commit

Permalink
chore: oauth sdk implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
manisha1997 committed Jul 17, 2024
1 parent 67c8697 commit 2f329c7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
14 changes: 11 additions & 3 deletions twilio/base/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions twilio/base/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -93,4 +94,5 @@ async def request_async(
auth=auth,
timeout=timeout,
allow_redirects=allow_redirects,
is_oauth=is_oauth
)
7 changes: 3 additions & 4 deletions twilio/base/oauth_token_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
TokenManagerInitializer().set_token_manager(orgs_token_manager)
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version))
15 changes: 15 additions & 0 deletions twilio/base/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -81,6 +82,7 @@ async def request_async(
auth=auth,
timeout=timeout,
allow_redirects=allow_redirects,
is_oauth=is_oauth
)

@classmethod
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -364,6 +375,7 @@ async def page_async(
auth=auth,
timeout=timeout,
allow_redirects=allow_redirects,
is_oauth=is_oauth
)

def stream(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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)
2 changes: 1 addition & 1 deletion twilio/http/bearer_token_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
):
Expand Down
5 changes: 2 additions & 3 deletions twilio/twilio_bearer_token_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from threading import Lock
from concurrent.futures import ThreadPoolExecutor


class BearerTokenTwilioRestClient:
Expand All @@ -13,7 +12,7 @@ class TwilioBearerTokenAuth:
user_agent_extensions = None
region = None
edge = None

@classmethod
def init(cls, access_token):
with cls._lock:
Expand All @@ -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)}
return {"Authorization": "Bearer {token}".format(token=cls.access_token)}
2 changes: 1 addition & 1 deletion twilio/twilio_no_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def build_oauth_rest_client(cls):
builder.region(cls.region)
builder.edge(cls.edge)

return builder.build()
return builder.build()

0 comments on commit 2f329c7

Please sign in to comment.