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'
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
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/twilio-ruby/base/client_base.rb', line 12 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 @edge = Twilio.edge @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
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def account_sid @account_sid end |
#auth_token ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def auth_token @auth_token end |
#credentials ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def credentials @credentials end |
#edge ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def edge @edge end |
#http_client ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def http_client @http_client end |
#logger ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def logger @logger end |
#password ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def password @password end |
#region ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def region @region end |
#user_agent_extensions ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def user_agent_extensions @user_agent_extensions end |
#username ⇒ Object
rubocop:enable Style/ClassVars
8 9 10 |
# File 'lib/twilio-ruby/base/client_base.rb', line 8 def username @username end |
Instance Method Details
#build_uri(uri) ⇒ Object
Build the final request uri
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/twilio-ruby/base/client_base.rb', line 80 def build_uri(uri) 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
26 27 28 29 |
# File 'lib/twilio-ruby/base/client_base.rb', line 26 def credential_provider(credential_provider = nil) @credentials = credential_provider self end |
#generate_headers(method, headers) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/twilio-ruby/base/client_base.rb', line 113 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/twilio-ruby/base/client_base.rb', line 35 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
106 107 108 109 110 111 |
# File 'lib/twilio-ruby/base/client_base.rb', line 106 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 |