1111from twilio import __version__
1212from twilio .base .exceptions import TwilioException
1313from twilio .base .obsolete import obsolete_client
14+ from twilio .compat import (
15+ urlparse ,
16+ urlunparse ,
17+ )
1418from twilio .http .http_client import TwilioHttpClient
1519
1620
1721class Client (object ):
1822 """ A client for accessing the Twilio API. """
1923
2024 def __init__ (self , username = None , password = None , account_sid = None , region = None ,
21- http_client = None , environment = None ):
25+ http_client = None , environment = None , edge = None ):
2226 """
2327 Initializes the Twilio Client
2428
2529 :param str username: Username to authenticate with
2630 :param str password: Password to authenticate with
2731 :param str account_sid: Account Sid, defaults to Username
28- :param str region: Twilio Region to make requests to
32+ :param str region: Twilio Region to make requests to, defaults to 'us1' if an edge is provided
2933 :param HttpClient http_client: HttpClient, defaults to TwilioHttpClient
3034 :param dict environment: Environment to look for auth details, defaults to os.environ
35+ :param str edge: Twilio Edge to make requests to, defaults to None
3136
3237 :returns: Twilio Client
3338 :rtype: twilio.rest.Client
@@ -40,7 +45,9 @@ def __init__(self, username=None, password=None, account_sid=None, region=None,
4045 """ :type : str """
4146 self .account_sid = account_sid or self .username
4247 """ :type : str """
43- self .region = region
48+ self .edge = edge or environment .get ('TWILIO_EDGE' )
49+ """ :type : str """
50+ self .region = region or environment .get ('TWILIO_REGION' )
4451 """ :type : str """
4552
4653 if not self .username or not self .password :
@@ -116,11 +123,7 @@ def request(self, method, uri, params=None, data=None, headers=None, auth=None,
116123 if 'Accept' not in headers :
117124 headers ['Accept' ] = 'application/json'
118125
119- if self .region :
120- head , tail = uri .split ('.' , 1 )
121-
122- if not tail .startswith (self .region ):
123- uri = '.' .join ([head , self .region , tail ])
126+ uri = self .get_hostname (uri )
124127
125128 return self .http_client .request (
126129 method ,
@@ -133,6 +136,41 @@ def request(self, method, uri, params=None, data=None, headers=None, auth=None,
133136 allow_redirects = allow_redirects
134137 )
135138
139+ def get_hostname (self , uri ):
140+ """
141+ Determines the proper hostname given edge and region preferences
142+ via client configuration or uri.
143+
144+ :param str uri: Fully qualified url
145+
146+ :returns: The final uri used to make the request
147+ :rtype: str
148+ """
149+ if not self .edge and not self .region :
150+ return uri
151+
152+ parsed_url = urlparse (uri )
153+ pieces = parsed_url .netloc .split ('.' )
154+ prefix = pieces [0 ]
155+ suffix = '.' .join (pieces [- 2 :])
156+ region = None
157+ edge = None
158+ if len (pieces ) == 4 :
159+ # product.region.twilio.com
160+ region = pieces [1 ]
161+ elif len (pieces ) == 5 :
162+ # product.edge.region.twilio.com
163+ edge = pieces [1 ]
164+ region = pieces [2 ]
165+
166+ edge = self .edge or edge
167+ region = self .region or region or (edge and 'us1' )
168+
169+ parsed_url = parsed_url ._replace (
170+ netloc = '.' .join ([part for part in [prefix , edge , region , suffix ] if part ])
171+ )
172+ return urlunparse (parsed_url )
173+
136174 @property
137175 def accounts (self ):
138176 """
0 commit comments