Class: Twilio::REST::ClientBase
- Inherits:
-
Object
- Object
- Twilio::REST::ClientBase
- Defined in:
- lib/twilio-ruby/base/client_base.rb
Direct Known Subclasses
Constant Summary collapse
- @@default_region =
rubocop:disable Style/ClassVars
'us1'- @@region_mappings =
Maps region codes to their corresponding edge location names Used to automatically set edge based on region for backward compatibility
{ 'au1' => 'sydney', 'br1' => 'sao-paulo', 'de1' => 'frankfurt', 'ie1' => 'dublin', 'jp1' => 'tokyo', 'jp2' => 'osaka', 'sg1' => 'singapore', 'us1' => 'ashburn', 'us2' => 'umatilla' }
Instance Attribute Summary collapse
-
#account_sid ⇒ Object
rubocop:enable Style/ClassVars.
-
#auth_token ⇒ Object
rubocop:enable Style/ClassVars.
-
#credentials ⇒ Object
rubocop:enable Style/ClassVars.
-
#edge ⇒ Object
rubocop:enable Style/ClassVars.
-
#http_client ⇒ Object
rubocop:enable Style/ClassVars.
-
#logger ⇒ Object
rubocop:enable Style/ClassVars.
-
#password ⇒ Object
rubocop:enable Style/ClassVars.
-
#region ⇒ Object
rubocop:enable Style/ClassVars.
-
#user_agent_extensions ⇒ Object
rubocop:enable Style/ClassVars.
-
#username ⇒ Object
rubocop:enable Style/ClassVars.
Instance Method Summary collapse
-
#build_uri(uri) ⇒ Object
Build the final request uri.
- #credential_provider(credential_provider = nil) ⇒ Object
- #generate_headers(method, headers) ⇒ Object
-
#initialize(username = nil, password = nil, account_sid = nil, region = nil, http_client = nil, logger = nil, user_agent_extensions = nil) ⇒ ClientBase
constructor
rubocop:disable Metrics/ParameterLists.
-
#request(host, port, method, uri, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object
Makes a request to the Twilio API using the configured http client Authentication information is automatically added if none is provided.
-
#validate_ssl_certificate ⇒ Object
Validate the SSL certificates for the Twilio API.
Constructor Details
#initialize(username = nil, password = nil, account_sid = nil, region = nil, http_client = nil, logger = nil, user_agent_extensions = nil) ⇒ ClientBase
rubocop:disable Metrics/ParameterLists
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/twilio-ruby/base/client_base.rb', line 25 def initialize(username = nil, password = nil, account_sid = nil, region = nil, http_client = nil, logger = nil, user_agent_extensions = nil) @username = username || Twilio.account_sid @password = password || Twilio.auth_token @region = region || Twilio.region if Twilio.edge @edge = Twilio.edge elsif @region && @@region_mappings[region] warn '[DEPRECATION] Setting default `Edge` for the provided `region`.' @edge = @@region_mappings[region] end @account_sid = account_sid || @username @auth_token = @password @auth = [@username, @password] @http_client = http_client || Twilio.http_client || Twilio::HTTP::Client.new @logger = logger || Twilio.logger @user_agent_extensions = user_agent_extensions || [] end |
Instance Attribute Details
#account_sid ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def account_sid @account_sid end |
#auth_token ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def auth_token @auth_token end |
#credentials ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def credentials @credentials end |
#edge ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def edge @edge end |
#http_client ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def http_client @http_client end |
#logger ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def logger @logger end |
#password ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def password @password end |
#region ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def region @region end |
#user_agent_extensions ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def user_agent_extensions @user_agent_extensions end |
#username ⇒ Object
rubocop:enable Style/ClassVars
21 22 23 |
# File 'lib/twilio-ruby/base/client_base.rb', line 21 def username @username end |
Instance Method Details
#build_uri(uri) ⇒ Object
Build the final request uri
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/twilio-ruby/base/client_base.rb', line 98 def build_uri(uri) if (@region.nil? && !@edge.nil?) || (!@region.nil? && @edge.nil?) # rubocop:disable Layout/LineLength warn '[DEPRECATION] For regional processing, DNS is of format product.<edge>.<region>.twilio.com;otherwise use product.twilio.com.' # rubocop:enable Layout/LineLength end 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) pieces = parsed_url.host.split('.') product = pieces[0] domain = pieces[-2, 2] new_edge = @edge new_region = @region case pieces.length when 4 new_region ||= pieces[1] when 5 new_edge ||= pieces[1] new_region ||= pieces[2] end new_region = @@default_region if !new_edge.nil? && new_region.nil? parsed_url.host = [product, new_edge, new_region, domain].reject(&:nil?).join('.') parsed_url.to_s end |
#credential_provider(credential_provider = nil) ⇒ Object
44 45 46 47 |
# File 'lib/twilio-ruby/base/client_base.rb', line 44 def credential_provider(credential_provider = nil) @credentials = credential_provider self end |
#generate_headers(method, headers) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/twilio-ruby/base/client_base.rb', line 140 def generate_headers(method, headers) ruby_config = RbConfig::CONFIG headers['User-Agent'] = "twilio-ruby/#{Twilio::VERSION} (#{ruby_config['host_os']} #{ruby_config['host_cpu']}) Ruby/#{RUBY_VERSION}" headers['Accept-Charset'] = 'utf-8' user_agent_extensions.each { |extension| headers['User-Agent'] += " #{extension}" } if ['POST', 'PUT'].include?(method) && !headers['Content-Type'] headers['Content-Type'] = 'application/x-www-form-urlencoded' end headers['Accept'] = 'application/json' unless headers['Accept'] headers end |
#request(host, port, method, uri, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object
Makes a request to the Twilio API using the configured http client Authentication information is automatically added if none is provided
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/twilio-ruby/base/client_base.rb', line 53 def request(host, port, method, uri, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) # rubocop:disable Metrics/MethodLength auth ||= @auth headers = generate_headers(method, headers) uri = build_uri(uri) if @logger @logger.debug('--BEGIN Twilio API Request--') @logger.debug("Request Method: <#{method}>") headers.each do |key, value| @logger.debug("#{key}:#{value}") unless key.downcase == 'authorization' end url = URI(uri) @logger.debug("Host:#{url.host}") @logger.debug("Path:#{url.path}") @logger.debug("Query:#{url.query}") @logger.debug("Request Params:#{params}") end auth = @credentials.to_auth_strategy.auth_string unless @credentials.nil? response = @http_client.request( host, port, method, uri, params, data, headers, auth, timeout ) if @logger @logger.debug("Response Status Code:#{response.status_code}") @logger.debug("Response Headers:#{response.headers}") @logger.debug('--END TWILIO API REQUEST--') end response end |
#validate_ssl_certificate ⇒ Object
Validate the SSL certificates for the Twilio API
133 134 135 136 137 138 |
# File 'lib/twilio-ruby/base/client_base.rb', line 133 def validate_ssl_certificate response = request('tls-test.twilio.com', '443', 'GET', 'https://tls-test.twilio.com') return unless response.status_code < 200 || response.status_code >= 300 raise RestError.new 'Unexpected response from certificate endpoint', response end |