Skip to content

Commit bd17551

Browse files
authored
fix: Regional API domain processing (#765)
* 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 * 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 * 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 * 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 * fix: Regional API domain processing * fix: Regional API domain processing * fix: Regional API domain processing
1 parent 8cabe11 commit bd17551

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

lib/twilio-ruby/base/client_base.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ module REST
33
class ClientBase
44
# rubocop:disable Style/ClassVars
55
@@default_region = 'us1'
6+
# Maps region codes to their corresponding edge location names
7+
# Used to automatically set edge based on region for backward compatibility
8+
@@region_mappings = {
9+
'au1' => 'sydney',
10+
'br1' => 'sao-paulo',
11+
'de1' => 'frankfurt',
12+
'ie1' => 'dublin',
13+
'jp1' => 'tokyo',
14+
'jp2' => 'osaka',
15+
'sg1' => 'singapore',
16+
'us1' => 'ashburn',
17+
'us2' => 'umatilla'
18+
}
619
# rubocop:enable Style/ClassVars
720

821
attr_accessor :http_client, :username, :password, :account_sid, :auth_token, :region, :edge, :logger,
@@ -14,7 +27,17 @@ def initialize(username = nil, password = nil, account_sid = nil, region = nil,
1427
@username = username || Twilio.account_sid
1528
@password = password || Twilio.auth_token
1629
@region = region || Twilio.region
17-
@edge = Twilio.edge
30+
if (region.nil? && !Twilio.edge.nil?) || (!region.nil? && Twilio.edge.nil?)
31+
# rubocop:disable Layout/LineLength
32+
warn '[DEPRECATION] For regional processing, DNS is of format product.<edge>.<region>.twilio.com;otherwise use product.twilio.com.'
33+
end
34+
if Twilio.edge
35+
@edge = Twilio.edge
36+
elsif @region && @@region_mappings[region]
37+
warn '[DEPRECATION] Setting default `Edge` for the provided `region`.'
38+
# rubocop:enable Layout/LineLength
39+
@edge = @@region_mappings[region]
40+
end
1841
@account_sid = account_sid || @username
1942
@auth_token = @password
2043
@auth = [@username, @password]
@@ -78,6 +101,10 @@ def request(host, port, method, uri, params = {}, data = {}, headers = {}, auth
78101
##
79102
# Build the final request uri
80103
def build_uri(uri)
104+
if @edge.nil? && @region && @@region_mappings[@region]
105+
warn '[DEPRECATION] Setting default `Edge` for the provided `region`.'
106+
@edge = @@region_mappings[@region]
107+
end
81108
return uri if @region.nil? && @edge.nil?
82109

83110
parsed_url = URI(uri)

spec/rest/client_spec.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,65 @@
88
end
99

1010
describe Twilio::REST::Client do
11+
context 'configuration of edge' do
12+
it 'uses the edge value from region map' do
13+
@client = Twilio::REST::Client.new('myUser', 'myPassword', nil, 'us1', 'myClient', 'myLogger')
14+
expect(@client.account_sid).to eq('myUser')
15+
expect(@client.auth_token).to eq('myPassword')
16+
expect(@client.http_client).to eq('myClient')
17+
expect(@client.region).to eq('us1')
18+
expect(@client.edge).to eq('ashburn')
19+
expect(@client.logger).to eq('myLogger')
20+
end
21+
22+
it 'uses the edge value from region map using setter' do
23+
@client = Twilio::REST::Client.new('myUser', 'myPassword', nil)
24+
@client.region = 'us1'
25+
@client.http_client = Twilio::HTTP::Client.new
26+
@connection = Faraday::Connection.new
27+
expect(Faraday).to receive(:new).and_yield(@connection).and_return(@connection)
28+
allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {}))
29+
@client.request('host', 'port', 'GET', 'https://api.twilio.com')
30+
expect(@client.region).to eq('us1')
31+
expect(@client.edge).to eq('ashburn')
32+
end
33+
34+
it 'catches warning when setting region' do
35+
original_stderr = $stderr
36+
$stderr = StringIO.new
37+
begin
38+
@client = Twilio::REST::Client.new('myUser', 'myPassword', 'someSid', 'ie1', 'myClient', 'myLogger')
39+
warn '[DEPRECATION] For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.'
40+
warn '[DEPRECATION] Setting default `Edge` for the provided `region`.'
41+
warnings = $stderr.string
42+
expect(warnings).to include('[DEPRECATION] For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.')
43+
expect(warnings).to include('[DEPRECATION] Setting default `Edge` for the provided `region`.')
44+
ensure
45+
$stderr = original_stderr
46+
end
47+
end
48+
49+
it 'catches warning when setting edge' do
50+
Twilio.configure do |config|
51+
config.account_sid = 'someSid'
52+
config.auth_token = 'someToken'
53+
config.http_client = 'someClient'
54+
config.edge = 'someEdge'
55+
config.logger = 'someLogger'
56+
end
57+
original_stderr = $stderr
58+
$stderr = StringIO.new
59+
begin
60+
@client = Twilio::REST::Client.new('myUser', 'myPassword', 'someSid')
61+
warn '[DEPRECATION] For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.+[DEPRECATION] Setting default `Edge` for the provided `region`.'
62+
warnings = $stderr.string
63+
expect(warnings).to include('[DEPRECATION] For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.+[DEPRECATION] Setting default `Edge` for the provided `region`.')
64+
ensure
65+
$stderr = original_stderr
66+
end
67+
end
68+
end
69+
1170
context 'configuration' do
1271
before do
1372
Twilio.configure do |config|

0 commit comments

Comments
 (0)