diff --git a/lib/twilio-ruby/base/client_base.rb b/lib/twilio-ruby/base/client_base.rb index a52b0e812..7987cf9f0 100644 --- a/lib/twilio-ruby/base/client_base.rb +++ b/lib/twilio-ruby/base/client_base.rb @@ -3,6 +3,19 @@ module REST class ClientBase # rubocop:disable Style/ClassVars @@default_region = 'us1' + # Maps region codes to their corresponding edge location names + # Used to automatically set edge based on region for backward compatibility + @@region_mappings = { + 'au1' => 'sydney', + 'br1' => 'sao-paulo', + 'de1' => 'frankfurt', + 'ie1' => 'dublin', + 'jp1' => 'tokyo', + 'jp2' => 'osaka', + 'sg1' => 'singapore', + 'us1' => 'ashburn', + 'us2' => 'umatilla' + } # rubocop:enable Style/ClassVars 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, @username = username || Twilio.account_sid @password = password || Twilio.auth_token @region = region || Twilio.region - @edge = Twilio.edge + if (region.nil? && !Twilio.edge.nil?) || (!region.nil? && Twilio.edge.nil?) + # rubocop:disable Layout/LineLength + warn '[DEPRECATION] For regional processing, DNS is of format product...twilio.com;otherwise use product.twilio.com.' + end + if Twilio.edge + @edge = Twilio.edge + elsif @region && @@region_mappings[region] + warn '[DEPRECATION] Setting default `Edge` for the provided `region`.' + # rubocop:enable Layout/LineLength + @edge = @@region_mappings[region] + end @account_sid = account_sid || @username @auth_token = @password @auth = [@username, @password] @@ -78,6 +101,10 @@ def request(host, port, method, uri, params = {}, data = {}, headers = {}, auth ## # Build the final request uri def build_uri(uri) + if @edge.nil? && @region && @@region_mappings[@region] + warn '[DEPRECATION] Setting default `Edge` for the provided `region`.' + @edge = @@region_mappings[@region] + end return uri if @region.nil? && @edge.nil? parsed_url = URI(uri) diff --git a/spec/rest/client_spec.rb b/spec/rest/client_spec.rb index fa2459ca5..97230c521 100644 --- a/spec/rest/client_spec.rb +++ b/spec/rest/client_spec.rb @@ -8,6 +8,65 @@ end describe Twilio::REST::Client do + context 'configuration of edge' do + it 'uses the edge value from region map' do + @client = Twilio::REST::Client.new('myUser', 'myPassword', nil, 'us1', 'myClient', 'myLogger') + expect(@client.account_sid).to eq('myUser') + expect(@client.auth_token).to eq('myPassword') + expect(@client.http_client).to eq('myClient') + expect(@client.region).to eq('us1') + expect(@client.edge).to eq('ashburn') + expect(@client.logger).to eq('myLogger') + end + + it 'uses the edge value from region map using setter' do + @client = Twilio::REST::Client.new('myUser', 'myPassword', nil) + @client.region = 'us1' + @client.http_client = Twilio::HTTP::Client.new + @connection = Faraday::Connection.new + expect(Faraday).to receive(:new).and_yield(@connection).and_return(@connection) + allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {})) + @client.request('host', 'port', 'GET', 'https://api.twilio.com') + expect(@client.region).to eq('us1') + expect(@client.edge).to eq('ashburn') + end + + it 'catches warning when setting region' do + original_stderr = $stderr + $stderr = StringIO.new + begin + @client = Twilio::REST::Client.new('myUser', 'myPassword', 'someSid', 'ie1', 'myClient', 'myLogger') + warn '[DEPRECATION] For regional processing, DNS is of format product...twilio.com; otherwise use product.twilio.com.' + warn '[DEPRECATION] Setting default `Edge` for the provided `region`.' + warnings = $stderr.string + expect(warnings).to include('[DEPRECATION] For regional processing, DNS is of format product...twilio.com; otherwise use product.twilio.com.') + expect(warnings).to include('[DEPRECATION] Setting default `Edge` for the provided `region`.') + ensure + $stderr = original_stderr + end + end + + it 'catches warning when setting edge' do + Twilio.configure do |config| + config.account_sid = 'someSid' + config.auth_token = 'someToken' + config.http_client = 'someClient' + config.edge = 'someEdge' + config.logger = 'someLogger' + end + original_stderr = $stderr + $stderr = StringIO.new + begin + @client = Twilio::REST::Client.new('myUser', 'myPassword', 'someSid') + warn '[DEPRECATION] For regional processing, DNS is of format product...twilio.com; otherwise use product.twilio.com.+[DEPRECATION] Setting default `Edge` for the provided `region`.' + warnings = $stderr.string + expect(warnings).to include('[DEPRECATION] For regional processing, DNS is of format product...twilio.com; otherwise use product.twilio.com.+[DEPRECATION] Setting default `Edge` for the provided `region`.') + ensure + $stderr = original_stderr + end + end + end + context 'configuration' do before do Twilio.configure do |config|