Skip to content

Commit fbec376

Browse files
manisha1997Copilot
andauthored
fix: Regional API domain processing (#895)
* fix: Regional API domain processing * fix: Regional API domain processing * fix: Regional API domain processing * fix: Regional API domain processing * fix: Regional API domain processing * fix: Regional API domain processing * fix: Regional API domain processing * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * fix: Regional API domain processing * fix: Regional API domain processing --------- Co-authored-by: Copilot <[email protected]>
1 parent 72fa232 commit fbec376

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

tests/unit/rest/test_client.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import unittest
2+
import warnings
3+
24
import aiounittest
35

46
from mock import AsyncMock, Mock
7+
58
from twilio.http.response import Response
69
from twilio.rest import Client
710

@@ -18,16 +21,16 @@ def test_set_client_edge_default_region(self):
1821
)
1922

2023
def test_set_client_region(self):
21-
self.client.region = "region"
24+
self.client.region = "us1"
2225
self.assertEqual(
2326
self.client.get_hostname("https://api.twilio.com"),
24-
"https://api.region.twilio.com",
27+
"https://api.us1.twilio.com",
2528
)
2629

2730
def test_set_uri_region(self):
2831
self.assertEqual(
29-
self.client.get_hostname("https://api.region.twilio.com"),
30-
"https://api.region.twilio.com",
32+
self.client.get_hostname("https://api.us1.twilio.com"),
33+
"https://api.us1.twilio.com",
3134
)
3235

3336
def test_set_client_edge_region(self):
@@ -75,6 +78,27 @@ def test_periods_in_query(self):
7578
"https://api.edge.region.twilio.com/path/to/something.json?foo=12.34",
7679
)
7780

81+
def test_edge_deprecation_warning_when_only_edge_is_set(self):
82+
with warnings.catch_warnings(record=True) as w:
83+
warnings.simplefilter("always") # Ensure all warnings are caught
84+
Client(username="username", password="password", edge="edge") # Trigger the warning
85+
86+
# Check if a warning was raised
87+
self.assertGreater(len(w), 0)
88+
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
89+
self.assertIn("For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.", str(w[-1].message))
90+
91+
def test_edge_deprecation_warning_when_only_region_is_set(self):
92+
with warnings.catch_warnings(record=True) as w:
93+
warnings.simplefilter("always") # Ensure all warnings are caught
94+
Client(username="username", password="password", region="us1") # Trigger the warning
95+
96+
# Check if a warning was raised
97+
self.assertGreater(len(w), 0)
98+
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
99+
self.assertIn("For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.", str(w[-1].message))
100+
101+
78102

79103
class TestUserAgentClients(unittest.TestCase):
80104
def setUp(self):

twilio/base/client_base.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import platform
3+
import warnings
34
from typing import Dict, List, MutableMapping, Optional, Tuple
45
from urllib.parse import urlparse, urlunparse
56

@@ -12,7 +13,17 @@
1213

1314
class ClientBase(object):
1415
"""A client for accessing the Twilio API."""
15-
16+
region_mappings = {
17+
"au1": "sydney",
18+
"br1": "sao-paulo",
19+
"de1": "frankfurt",
20+
"ie1": "dublin",
21+
"jp1": "tokyo",
22+
"jp2": "osaka",
23+
"sg1": "singapore",
24+
"us1": "ashburn",
25+
"us2": "umatilla"
26+
}
1627
def __init__(
1728
self,
1829
username: Optional[str] = None,
@@ -34,7 +45,7 @@ def __init__(
3445
:param region: Twilio Region to make requests to, defaults to 'us1' if an edge is provided
3546
:param http_client: HttpClient, defaults to TwilioHttpClient
3647
:param environment: Environment to look for auth details, defaults to os.environ
37-
:param edge: Twilio Edge to make requests to, defaults to None
48+
:param edge: Twilio Edge to make requests to, defaults to None.
3849
:param user_agent_extensions: Additions to the user agent string
3950
:param credential_provider: credential provider for authentication method that needs to be used
4051
"""
@@ -45,7 +56,13 @@ def __init__(
4556
""" :type : str """
4657
self.password = password or environment.get("TWILIO_AUTH_TOKEN")
4758
""" :type : str """
48-
self.edge = edge or environment.get("TWILIO_EDGE")
59+
if (edge is not None and region is None) or (region is not None and edge is None):
60+
warnings.warn(
61+
"For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.",
62+
DeprecationWarning,
63+
stacklevel=2
64+
)
65+
self.edge = edge or environment.get("TWILIO_EDGE") or (self.region_mappings[region] if region is not None else "")
4966
""" :type : str """
5067
self.region = region or environment.get("TWILIO_REGION")
5168
""" :type : str """

0 commit comments

Comments
 (0)