Sha256: 5b41dcd410e0e8ca89da340873966806360891e548acaf9bdc84b67265d6a239

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

require 'faraday'

module Twilio
  module HTTP
    class Client
      attr_accessor :adapter

      def initialize(proxy_addr = nil, proxy_port = nil, proxy_user = nil, proxy_pass = nil, ssl_ca_file = nil)
        @proxy_addr = proxy_addr
        @proxy_port = proxy_port
        @proxy_user = proxy_user
        @proxy_pass = proxy_pass
        @ssl_ca_file = ssl_ca_file
        @adapter = Faraday.default_adapter

        # Set default params encoder
        Faraday::Utils.default_params_encoder = Faraday::FlatParamsEncoder
      end

      def request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil)
        @connection = Faraday.new(url: host + ':' + port.to_s, ssl: { verify: true }) do |f|
          f.request :url_encoded
          f.adapter @adapter
          f.headers = headers
          f.basic_auth(auth[0], auth[1])
          if @proxy_addr
            f.proxy '#{@proxy_user}:#{@proxy_pass}@#{@proxy_addr}:#{@proxy_port}'
          end
          f.options.open_timeout = timeout
          f.options.timeout = timeout
        end

        response = @connection.send(method.downcase.to_sym, url, method == 'GET' ? params : data)

        @last_response = response
        if response.body && !response.body.empty?
          object = response.body
        elsif response.status == 400
          object = { message: 'Bad request', code: 400 }.to_json
        end

        TwilioResponse.new(response.status, object)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
twilio-ruby-5.1.0 lib/twilio-ruby/http/http_client.rb
twilio-ruby-5.0.0.alpha1 lib/twilio-ruby/http/http_client.rb
twilio-ruby-5.0.0 lib/twilio-ruby/http/http_client.rb