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 Jun 24, 2024
1 parent 8cb3d47 commit 691071d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
7 changes: 2 additions & 5 deletions twilio/http/bearer_token_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ def __init__(self, orgs_token_manager: OrgTokenManager):

def get_headers(self):
headers = {}
if TwilioBearerTokenAuth.get_access_token() is None:
access_token = OrgTokenManager.fetch_access_token()
TwilioBearerTokenAuth.init(access_token)
elif TwilioBearerTokenAuth.get_access_token() is not None and self.is_token_expired(TwilioBearerTokenAuth.get_access_token()):
access_token = OrgTokenManager.fetch_access_token()
if TwilioBearerTokenAuth.get_access_token() is None or self.is_token_expired(TwilioBearerTokenAuth.get_access_token()):
access_token = self.orgs_token_manager.fetch_access_token()
TwilioBearerTokenAuth.init(access_token)
else:
access_token = TwilioBearerTokenAuth.get_access_token()
Expand Down
2 changes: 1 addition & 1 deletion twilio/http/orgs_token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, grant_type: str, client_id: str, client_secret: str, code: st

def fetch_access_token(self):
token_list = TokenList()
token_list.create(
return token_list.create(
grant_type=self.grant_type,
client_id=self.client_id,
client_secret=self.client_secret,
Expand Down
13 changes: 6 additions & 7 deletions twilio/rest/preview_iam/organizations_openapi/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from decimal import Decimal
from typing import Any, Dict, List, Optional, Union, Iterator, AsyncIterator
from twilio.base import deserialize, serialize, values
import json

from twilio.base.instance_resource import InstanceResource
from twilio.base.list_resource import ListResource
Expand Down Expand Up @@ -62,14 +63,13 @@ def __repr__(self) -> str:

class TokenList(ListResource):

def __init__(self, version: Version):
def __init__(self):
"""
Initialize the TokenList
:param version: Version that contains the resource
"""
super().__init__(version)


self._uri = '/token'
Expand Down Expand Up @@ -109,14 +109,13 @@ def create(self, grant_type: str, client_id: str, client_secret: Union[str, obje
try:
response = twilioHttpClient.request(
'POST',
'https://preview-iam.twilio.com',
data=data,
headers= {'content-type': 'json'},
'https://preview-iam.twilio.com/v1/token',
data=data
)
if response.status_code >= 400 or response.status_code < 500:
if response.status_code == 401:
retries += 1
continue
return response.status_code['data']
return json.loads(response.content)['access_token']
except Exception as e:
if retries == 5:
raise e
Expand Down

0 comments on commit 691071d

Please sign in to comment.