Class: Twilio::HTTP::Client
- Inherits:
-
Object
- Object
- Twilio::HTTP::Client
- Defined in:
- lib/twilio-ruby/http/http_client.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#last_request ⇒ Object
readonly
Returns the value of attribute last_request.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#_request(request) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #configure_connection(&block) ⇒ Object
-
#initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, proxy_user = nil, proxy_pass = nil, timeout: nil) ⇒ Client
constructor
A new instance of Client.
- #jwt_token?(auth) ⇒ Boolean
- #request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object
- #send(request) ⇒ Object
Constructor Details
#initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, proxy_user = nil, proxy_pass = nil, timeout: nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 |
# File 'lib/twilio-ruby/http/http_client.rb', line 12 def initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, proxy_user = nil, proxy_pass = nil, timeout: nil) @proxy_prot = proxy_prot @proxy_path = "#{proxy_addr}:#{proxy_port}" if proxy_addr && proxy_port @proxy_auth = "#{proxy_user}:#{proxy_pass}@" if proxy_pass && proxy_user @timeout = timeout @adapter = Faraday.default_adapter @configure_connection_blocks = [] end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
9 10 11 |
# File 'lib/twilio-ruby/http/http_client.rb', line 9 def adapter @adapter end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
10 11 12 |
# File 'lib/twilio-ruby/http/http_client.rb', line 10 def connection @connection end |
#last_request ⇒ Object (readonly)
Returns the value of attribute last_request.
10 11 12 |
# File 'lib/twilio-ruby/http/http_client.rb', line 10 def last_request @last_request end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
10 11 12 |
# File 'lib/twilio-ruby/http/http_client.rb', line 10 def last_response @last_response end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/twilio-ruby/http/http_client.rb', line 10 def timeout @timeout end |
Instance Method Details
#_request(request) ⇒ Object
rubocop:disable Metrics/MethodLength
29 30 31 32 33 34 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 |
# File 'lib/twilio-ruby/http/http_client.rb', line 29 def _request(request) # rubocop:disable Metrics/MethodLength middle_ware = case request.headers['Content-Type'] when 'application/json' :json when 'application/x-www-form-urlencoded' :url_encoded else :url_encoded end @connection = Faraday.new(url: request.host + ':' + request.port.to_s, ssl: { verify: true }) do |f| f..params_encoder = Faraday::FlatParamsEncoder f.request(middle_ware) f.headers = request.headers if Faraday::VERSION.start_with?('2.') f.request(:authorization, :basic, request.auth[0], request.auth[1]) else f.request(:basic_auth, request.auth[0], request.auth[1]) end f.proxy = "#{@proxy_prot}://#{@proxy_auth}#{@proxy_path}" if @proxy_prot && @proxy_path f..open_timeout = request.timeout || @timeout f..timeout = request.timeout || @timeout f.params = request.params.nil? ? {} : request.params @configure_connection_blocks.each { |block| block.call(f) } f.adapter @adapter end @last_request = request @last_response = nil response = send(request) if (500..599).include?(response.status) object = { message: "Server error (#{response.status})", code: response.status }.to_json elsif response.body && !response.body.empty? object = response.body elsif response.status == 400 object = { message: 'Bad request', code: 400 }.to_json end twilio_response = Twilio::Response.new(response.status, object, headers: response.headers) @last_response = twilio_response twilio_response end |
#configure_connection(&block) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/twilio-ruby/http/http_client.rb', line 22 def configure_connection(&block) raise ArgumentError, "#{__method__} must be given a block!" unless block_given? @configure_connection_blocks << block nil end |
#jwt_token?(auth) ⇒ Boolean
88 89 90 91 92 93 |
# File 'lib/twilio-ruby/http/http_client.rb', line 88 def jwt_token?(auth) return false unless auth.is_a?(String) parts = auth.split('.') parts.length == 3 end |
#request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object
82 83 84 85 86 |
# File 'lib/twilio-ruby/http/http_client.rb', line 82 def request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) headers['Authorization'] = auth if jwt_token?(auth) request = Twilio::Request.new(host, port, method, url, params, data, headers, auth, timeout) _request(request) end |
#send(request) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/twilio-ruby/http/http_client.rb', line 74 def send(request) @connection.send(request.method.downcase.to_sym, request.url, request.data) rescue Faraday::Error => e raise Twilio::REST::TwilioError, e end |