44from urllib .parse import urlparse , urlunparse
55
66from twilio import __version__
7- from twilio .base .exceptions import TwilioException
87from twilio .http import HttpClient
98from twilio .http .http_client import TwilioHttpClient
109from twilio .http .response import Response
10+ from twilio .authStrategy .authType import AuthType
11+ from twilio .credential .credentialProvider import CredentialProvider
1112
1213
1314class ClientBase (object ):
@@ -23,6 +24,7 @@ def __init__(
2324 environment : Optional [MutableMapping [str , str ]] = None ,
2425 edge : Optional [str ] = None ,
2526 user_agent_extensions : Optional [List [str ]] = None ,
27+ credential_provider : Optional [CredentialProvider ] = None ,
2628 ):
2729 """
2830 Initializes the Twilio Client
@@ -35,7 +37,9 @@ def __init__(
3537 :param environment: Environment to look for auth details, defaults to os.environ
3638 :param edge: Twilio Edge to make requests to, defaults to None
3739 :param user_agent_extensions: Additions to the user agent string
40+ :param credential_provider: credential provider for authentication method that needs to be used
3841 """
42+
3943 environment = environment or os .environ
4044
4145 self .username = username or environment .get ("TWILIO_ACCOUNT_SID" )
@@ -48,9 +52,8 @@ def __init__(
4852 """ :type : str """
4953 self .user_agent_extensions = user_agent_extensions or []
5054 """ :type : list[str] """
51-
52- if not self .username or not self .password :
53- raise TwilioException ("Credentials are required to create a TwilioClient" )
55+ self .credential_provider = credential_provider or None
56+ """ :type : CredentialProvider """
5457
5558 self .account_sid = account_sid or self .username
5659 """ :type : str """
@@ -69,8 +72,6 @@ def request(
6972 auth : Optional [Tuple [str , str ]] = None ,
7073 timeout : Optional [float ] = None ,
7174 allow_redirects : bool = False ,
72- is_oauth : bool = False ,
73- domain : Optional [str ] = None ,
7475 ) -> Response :
7576 """
7677 Makes a request to the Twilio API using the configured http client
@@ -88,20 +89,26 @@ def request(
8889 :returns: Response from the Twilio API
8990 """
9091
91- print ('*****' )
92- if not is_oauth :
93- auth = self .get_auth (auth )
9492 headers = self .get_headers (method , headers )
93+
94+ ##If credential provider is provided by user, get the associated auth strategy
95+ ##Using the auth strategy, fetch the auth string and set it to authorization header
96+ auth_strategy = None ##Initialization
97+ if self .credential_provider :
98+ print (f'Reached here 2 { self .credential_provider } ' )
99+ auth_strategy = self .credential_provider .to_auth_strategy ()
100+ if auth_strategy .auth_type == AuthType .TOKEN :
101+ auth_strategy .fetch_token ()
102+ headers ["Authorization" ] = auth_strategy .get_auth_string ()
103+ if auth_strategy .auth_type == AuthType .BASIC :
104+ headers ["Authorization" ] = auth_strategy .get_auth_string ()
105+ else :
106+ auth = self .get_auth (auth )
107+
108+ print (f'auth2 *** { auth } ' )
109+
110+
95111 uri = self .get_hostname (uri )
96- if is_oauth :
97- OauthTokenBase = dynamic_import (
98- "twilio.base.oauth_token_base" , "OauthTokenBase"
99- )
100- token = OauthTokenBase ().get_oauth_token (
101- domain , "v1" , self .username , self .password
102- )
103- headers ["Authorization" ] = f"Bearer { token } "
104- headers .get ("Authorization" )
105112
106113 return self .http_client .request (
107114 method ,
@@ -124,7 +131,6 @@ async def request_async(
124131 auth : Optional [Tuple [str , str ]] = None ,
125132 timeout : Optional [float ] = None ,
126133 allow_redirects : bool = False ,
127- is_oauth : bool = False ,
128134 ) -> Response :
129135 """
130136 Asynchronously makes a request to the Twilio API using the configured http client
@@ -146,19 +152,25 @@ async def request_async(
146152 raise RuntimeError (
147153 "http_client must be asynchronous to support async API requests"
148154 )
149- if not is_oauth :
150- auth = self . get_auth ( auth )
155+
156+
151157 headers = self .get_headers (method , headers )
152- uri = self .get_hostname (uri )
153- if is_oauth :
154- OauthTokenBase = dynamic_import (
155- "twilio.base.oauth_token_base" , "OauthTokenBase"
156- )
157- token = OauthTokenBase ().get_oauth_token (
158- domain , "v1" , self .username , self .password
159- )
160- headers ["Authorization" ] = f"Bearer { token } "
161- headers .get ("Authorization" )
158+
159+ ##If credential provider is provided by user, get the associated auth strategy
160+ ##Using the auth strategy, fetch the auth string and set it to authorization header
161+ auth_strategy = None ##Initialization
162+ if self .credential_provider :
163+ print (f'Reached here 1' )
164+ auth_strategy = self .credential_provider .to_auth_strategy ()
165+ if auth_strategy .auth_type == AuthType .TOKEN :
166+ auth_strategy .fetch_token ()
167+ headers ["Authorization" ] = auth_strategy .get_auth_string ()
168+ if auth_strategy .auth_type == AuthType .BASIC :
169+ headers ["Authorization" ] = auth_strategy .get_auth_string ()
170+ else :
171+ auth = self .get_auth (auth )
172+
173+ print (f'auth2 *** { auth } ' )
162174
163175 return await self .http_client .request (
164176 method ,
0 commit comments