Class: HttpClient

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

Defined Under Namespace

Classes: Error, IOError, Response, Timeout

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpClient

Returns a new instance of HttpClient.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/http_client.rb', line 80

def initialize(options = {})
  options = {
    :use_connection_pool => false,
    :max_connections => 20,
    :max_connections_per_route => nil,
    :max_retries => 0,
    :response_class => Response,
    :connection_request_timeout => 100,
    :connect_timeout => 1000,
    :socket_timeout => 2000,
    :default_request_options => {}
  }.merge(options)
  @request_config = create_request_config(options)
  @connection_manager = create_connection_manager(options)
  @client = HttpClients.create_minimal(@connection_manager)
  @max_retries = options[:max_retries]
  @response_class = options[:response_class]
  @default_request_options = options[:default_request_options]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



78
79
80
# File 'lib/http_client.rb', line 78

def client
  @client
end

#default_request_optionsObject (readonly)

Returns the value of attribute default_request_options.



78
79
80
# File 'lib/http_client.rb', line 78

def default_request_options
  @default_request_options
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



78
79
80
# File 'lib/http_client.rb', line 78

def max_retries
  @max_retries
end

#response_classObject (readonly)

Returns the value of attribute response_class.



78
79
80
# File 'lib/http_client.rb', line 78

def response_class
  @response_class
end

Instance Method Details

#cleanup_connections(max_idle = 5) ⇒ Object



153
154
155
# File 'lib/http_client.rb', line 153

def cleanup_connections(max_idle = 5)
  @connection_manager.close_idle_connections(max_idle, TimeUnit::SECONDS)
end

#delete(uri, options = {}) ⇒ Object



127
128
129
130
# File 'lib/http_client.rb', line 127

def delete(uri, options = {})
  request = create_request(HttpDelete, uri, options)
  execute(request)
end

#get(uri, options = {}) ⇒ Object



100
101
102
103
104
# File 'lib/http_client.rb', line 100

def get(uri, options = {})
  uri = uri.sub(/\?.+$|$/, "?#{URI.encode_www_form(options[:params])}") if options[:params]
  request = create_request(HttpGet, uri, options)
  execute(request)
end

#head(uri, options = {}) ⇒ Object



132
133
134
135
# File 'lib/http_client.rb', line 132

def head(uri, options = {})
  request = create_request(HttpHead, uri, options)
  execute(request)
end

#options(uri, options = {}) ⇒ Object



137
138
139
140
# File 'lib/http_client.rb', line 137

def options(uri, options = {})
  request = create_request(HttpOptions, uri, options)
  execute(request)
end

#patch(uri, options = {}) ⇒ Object



120
121
122
123
124
125
# File 'lib/http_client.rb', line 120

def patch(uri, options = {})
  request = create_request(HttpPatch, uri, options)
  entity = create_entity(options)
  request.set_entity(entity) if entity
  execute(request)
end

#pool_statsObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/http_client.rb', line 142

def pool_stats
  raise "#{self.class.name}#pool_stats is supported only when using a connection pool" unless @connection_manager.is_a?(PoolingHttpClientConnectionManager)
  total_stats = @connection_manager.total_stats
  Hash(
    :idle => total_stats.available,
    :in_use => total_stats.leased,
    :max => total_stats.max,
    :waiting => total_stats.pending
  )
end

#post(uri, options = {}) ⇒ Object



106
107
108
109
110
111
# File 'lib/http_client.rb', line 106

def post(uri, options = {})
  request = create_request(HttpPost, uri, options)
  entity = create_entity(options)
  request.set_entity(entity) if entity
  execute(request)
end

#put(uri, options = {}) ⇒ Object



113
114
115
116
117
118
# File 'lib/http_client.rb', line 113

def put(uri, options = {})
  request = create_request(HttpPut, uri, options)
  entity = create_entity(options)
  request.set_entity(entity) if entity
  execute(request)
end

#shutdownObject



157
158
159
# File 'lib/http_client.rb', line 157

def shutdown
  @connection_manager.shutdown
end