Class: Twilio::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio-ruby/http/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, proxy_user = nil, proxy_pass = nil, ssl_ca_file = nil, timeout: nil) ⇒ Client

Returns a new instance of Client.


11
12
13
14
15
16
17
18
19
20
# File 'lib/twilio-ruby/http/http_client.rb', line 11

def initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, proxy_user = nil, proxy_pass = nil,
               ssl_ca_file = 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
  @ssl_ca_file = ssl_ca_file
  @timeout = timeout
  @adapter = Faraday.default_adapter
  @configure_connection_blocks = []
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter


8
9
10
# File 'lib/twilio-ruby/http/http_client.rb', line 8

def adapter
  @adapter
end

#last_requestObject (readonly)

Returns the value of attribute last_request


9
10
11
# File 'lib/twilio-ruby/http/http_client.rb', line 9

def last_request
  @last_request
end

#last_responseObject (readonly)

Returns the value of attribute last_response


9
10
11
# File 'lib/twilio-ruby/http/http_client.rb', line 9

def last_response
  @last_response
end

#timeoutObject (readonly)

Returns the value of attribute timeout


9
10
11
# File 'lib/twilio-ruby/http/http_client.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#_request(request) ⇒ Object


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
# File 'lib/twilio-ruby/http/http_client.rb', line 29

def _request(request)
  @connection = Faraday.new(url: request.host + ':' + request.port.to_s, ssl: { verify: true }) do |f|
    f.options.params_encoder = Faraday::FlatParamsEncoder
    f.request :url_encoded
    f.headers = request.headers
    f.basic_auth(request.auth[0], request.auth[1])
    f.proxy = "#{@proxy_prot}://#{@proxy_auth}#{@proxy_path}" if @proxy_prot && @proxy_path
    f.options.open_timeout = request.timeout || @timeout
    f.options.timeout = request.timeout || @timeout

    @configure_connection_blocks.each { |block| block.call(f) }
    f.adapter @adapter
  end

  @last_request = request
  @last_response = nil

  response = send(request)
  if response.status == 504
    object = { message: 'Request timeout', code: 504 }.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

Raises:

  • (ArgumentError)

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

#request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object


69
70
71
72
# File 'lib/twilio-ruby/http/http_client.rb', line 69

def request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil)
  request = Twilio::Request.new(host, port, method, url, params, data, headers, auth, timeout)
  _request(request)
end

#send(request) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/twilio-ruby/http/http_client.rb', line 61

def send(request)
  @connection.send(request.method.downcase.to_sym,
                   request.url,
                   request.method == 'GET' ? request.params : request.data)
rescue Faraday::Error => e
  raise Twilio::REST::TwilioError, e
end