11
11
from twilio import __version__
12
12
from twilio .base .exceptions import TwilioException
13
13
from twilio .base .obsolete import obsolete_client
14
+ from twilio .compat import (
15
+ urlparse ,
16
+ urlunparse ,
17
+ )
14
18
from twilio .http .http_client import TwilioHttpClient
15
19
16
20
17
21
class Client (object ):
18
22
""" A client for accessing the Twilio API. """
19
23
20
24
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 ):
22
26
"""
23
27
Initializes the Twilio Client
24
28
25
29
:param str username: Username to authenticate with
26
30
:param str password: Password to authenticate with
27
31
: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
29
33
:param HttpClient http_client: HttpClient, defaults to TwilioHttpClient
30
34
: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
31
36
32
37
:returns: Twilio Client
33
38
:rtype: twilio.rest.Client
@@ -40,7 +45,9 @@ def __init__(self, username=None, password=None, account_sid=None, region=None,
40
45
""" :type : str """
41
46
self .account_sid = account_sid or self .username
42
47
""" :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' )
44
51
""" :type : str """
45
52
46
53
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,
116
123
if 'Accept' not in headers :
117
124
headers ['Accept' ] = 'application/json'
118
125
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 )
124
127
125
128
return self .http_client .request (
126
129
method ,
@@ -133,6 +136,41 @@ def request(self, method, uri, params=None, data=None, headers=None, auth=None,
133
136
allow_redirects = allow_redirects
134
137
)
135
138
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
+
136
174
@property
137
175
def accounts (self ):
138
176
"""
0 commit comments