Class: HTTParty::ConnectionAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/httparty/connection_adapter.rb

Overview

Default connection adapter that returns a new Net::HTTP each time

Custom Connection Factories

If you like to implement your own connection adapter, subclassing HTTPParty::ConnectionAdapter will make it easier. Just override the #connection method. The uri and options attributes will have all the info you need to construct your http connection. Whatever you return from your connection method needs to adhere to the Net::HTTP interface as this is what HTTParty expects.

Configuration

There is lots of configuration data available for your connection adapter in the #options attribute. It is up to you to interpret them within your connection adapter. Take a look at the implementation of HTTParty::ConnectionAdapter#connection for examples of how they are used. Some things that are probably interesting are as follows:

  • :timeout: timeout in seconds

  • :open_timeout: http connection open_timeout in seconds, overrides timeout if set

  • :read_timeout: http connection read_timeout in seconds, overrides timeout if set

  • :debug_output: see HTTParty::ClassMethods.debug_output.

  • :pem: contains pem data. see HTTParty::ClassMethods.pem.

  • :verify: verify the server’s certificate against the ca certificate.

  • :verify_peer: set to false to turn off server verification but still send client certificate

  • :ssl_ca_file: see HTTParty::ClassMethods.ssl_ca_file.

  • :ssl_ca_path: see HTTParty::ClassMethods.ssl_ca_path.

  • :connection_adapter_options: contains the hash you passed to HTTParty.connection_adapter when you configured your connection adapter

Examples:

log the uri and options

class LoggingConnectionAdapter < HTTParty::ConnectionAdapter
  def connection
    puts uri
    puts options
    Net::HTTP.new(uri)
  end
end

count number of http calls

class CountingConnectionAdapter < HTTParty::ConnectionAdapter
  @@count = 0

  self.count
    @@count
  end

  def connection
    self.count += 1
    super
  end
end

Constant Summary collapse

StripIpv6BracketsRegex =

Private: Regex used to strip brackets from IPv6 URIs.

/\A\[(.*)\]\z/
OPTION_DEFAULTS =
{
  verify: true,
  verify_peer: true
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ ConnectionAdapter

Returns a new instance of ConnectionAdapter.

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
# File 'lib/httparty/connection_adapter.rb', line 68

def initialize(uri, options = {})
  uri_adapter = options[:uri_adapter] || URI
  raise ArgumentError, "uri must be a #{uri_adapter}, not a #{uri.class}" unless uri.is_a? uri_adapter

  @uri = uri
  @options = OPTION_DEFAULTS.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



66
67
68
# File 'lib/httparty/connection_adapter.rb', line 66

def options
  @options
end

#uriObject (readonly)

Returns the value of attribute uri.



66
67
68
# File 'lib/httparty/connection_adapter.rb', line 66

def uri
  @uri
end

Class Method Details

.call(uri, options) ⇒ Object

Public



62
63
64
# File 'lib/httparty/connection_adapter.rb', line 62

def self.call(uri, options)
  new(uri, options).connection
end

Instance Method Details

#connectionObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
130
# File 'lib/httparty/connection_adapter.rb', line 76

def connection
  host = clean_host(uri.host)
  port = uri.port || (uri.scheme == 'https' ? 443 : 80)
  if options.key?(:http_proxyaddr)
    http = Net::HTTP.new(host, port, options[:http_proxyaddr], options[:http_proxyport], options[:http_proxyuser], options[:http_proxypass])
  else
    http = Net::HTTP.new(host, port)
  end

  http.use_ssl = ssl_implied?(uri)

  attach_ssl_certificates(http, options)

  if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float))
    http.open_timeout = options[:timeout]
    http.read_timeout = options[:timeout]
  end

  if options[:read_timeout] && (options[:read_timeout].is_a?(Integer) || options[:read_timeout].is_a?(Float))
    http.read_timeout = options[:read_timeout]
  end

  if options[:open_timeout] && (options[:open_timeout].is_a?(Integer) || options[:open_timeout].is_a?(Float))
    http.open_timeout = options[:open_timeout]
  end

  if options[:debug_output]
    http.set_debug_output(options[:debug_output])
  end

  if options[:ciphers]
    http.ciphers = options[:ciphers]
  end

  # Bind to a specific local address or port
  #
  # @see https://bugs.ruby-lang.org/issues/6617
  if options[:local_host]
    if RUBY_VERSION >= "2.0.0"
      http.local_host = options[:local_host]
    else
      Kernel.warn("Warning: option :local_host requires Ruby version 2.0 or later")
    end
  end

  if options[:local_port]
    if RUBY_VERSION >= "2.0.0"
      http.local_port = options[:local_port]
    else
      Kernel.warn("Warning: option :local_port requires Ruby version 2.0 or later")
    end
  end

  http
end