Skip to content
23 changes: 19 additions & 4 deletions tests/unit/rest/test_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import warnings

import aiounittest

from mock import AsyncMock, Mock

from test import client
from twilio.http.response import Response
from twilio.rest import Client

Expand All @@ -18,16 +22,16 @@
)

def test_set_client_region(self):
self.client.region = "region"
self.client.region = "us1"
self.assertEqual(
self.client.get_hostname("https://api.twilio.com"),
"https://api.region.twilio.com",
"https://api.us1.twilio.com",
)

def test_set_uri_region(self):
self.assertEqual(
self.client.get_hostname("https://api.region.twilio.com"),
"https://api.region.twilio.com",
self.client.get_hostname("https://api.us1.twilio.com"),
"https://api.us1.twilio.com",
)

def test_set_client_edge_region(self):
Expand Down Expand Up @@ -75,6 +79,17 @@
"https://api.edge.region.twilio.com/path/to/something.json?foo=12.34",
)

def test_edge_deprecation_warning(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always") # Ensure all warnings are caught
Client(username="username", password="password", edge="edge") # Trigger the warning

# Check if a warning was raised
self.assertTrue(len(w) > 0)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))

Check warning on line 89 in tests/unit/rest/test_client.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Consider using "assertGreater" instead.

See more on https://sonarcloud.io/project/issues?id=twilio_twilio-python&issues=AZq72sAA6fFi_fcCOHw5&open=AZq72sAA6fFi_fcCOHw5&pullRequest=895
self.assertIn("`edge` is deprecated and will be removed in a future version. Use `region` instead.", str(w[-1].message))



class TestUserAgentClients(unittest.TestCase):
def setUp(self):
Expand Down
23 changes: 20 additions & 3 deletions twilio/base/client_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import platform
import warnings
from typing import Dict, List, MutableMapping, Optional, Tuple
from urllib.parse import urlparse, urlunparse

Expand All @@ -9,10 +10,21 @@
from twilio.http.response import Response
from twilio.credential.credential_provider import CredentialProvider

warnings.simplefilter("always", DeprecationWarning)

class ClientBase(object):
"""A client for accessing the Twilio API."""

region_mappings = {
"au1": "sydney",
"br1": "sao-paulo",
"de1": "frankfurt",
"ie1": "dublin",
"jp1": "tokyo",
"jp2": "osaka",
"sg1": "singapore",
"us1": "ashburn",
"us2": "umatilla"
}
def __init__(
self,
username: Optional[str] = None,
Expand All @@ -34,7 +46,7 @@ def __init__(
:param region: Twilio Region to make requests to, defaults to 'us1' if an edge is provided
:param http_client: HttpClient, defaults to TwilioHttpClient
:param environment: Environment to look for auth details, defaults to os.environ
:param edge: Twilio Edge to make requests to, defaults to None
:param edge: (Deprecated) Twilio Edge to make requests to, defaults to None. Will be deprecated from 9.9.0. Twilio is moving towards regional processing. This will be removed from 10.x.x.
:param user_agent_extensions: Additions to the user agent string
:param credential_provider: credential provider for authentication method that needs to be used
"""
Expand All @@ -45,7 +57,12 @@ def __init__(
""" :type : str """
self.password = password or environment.get("TWILIO_AUTH_TOKEN")
""" :type : str """
self.edge = edge or environment.get("TWILIO_EDGE")
if edge is not None:
warnings.warn(
"`edge` is deprecated and will be removed in a future version. Use `region` instead.",
DeprecationWarning
)
self.edge = (self.region_mappings[region] if region is not None else "") or edge or environment.get("TWILIO_EDGE")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If region and edge both are provided, it will default to the edge specified in our mapping. Instead, it should give priority to the edge provided by the customer

""" :type : str """
self.region = region or environment.get("TWILIO_REGION")
""" :type : str """
Expand Down
Loading