Class: HttpClient

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

Defined Under Namespace

Classes: ConnectionCleaner, Error, IOError, Response, Timeout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ HttpClient

Returns a new instance of HttpClient.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/http_client.rb', line 48

def initialize(config = {})
  @config = {
    :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,
    :max_idle => 5,
    :use_connection_cleaner => true,
    :default_request_options => {}
  }.merge(config)
  @request_config = create_request_config
  @connection_manager = create_connection_manager
  @client = HttpClients.create_minimal(@connection_manager)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



46
47
48
# File 'lib/http_client.rb', line 46

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



46
47
48
# File 'lib/http_client.rb', line 46

def config
  @config
end

Class Method Details

.connection_cleanerObject



42
43
44
# File 'lib/http_client.rb', line 42

def self.connection_cleaner
  @lock.synchronize { @connection_cleaner ||= ConnectionCleaner.new }
end

Instance Method Details

#cleanup_connections(max_idle = config[:max_idle]) ⇒ Object



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

def cleanup_connections(max_idle = config[:max_idle])
  @connection_manager.close_idle_connections(max_idle, TimeUnit::SECONDS)
end

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



94
95
96
97
# File 'lib/http_client.rb', line 94

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

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



67
68
69
70
71
# File 'lib/http_client.rb', line 67

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



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

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

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



104
105
106
107
# File 'lib/http_client.rb', line 104

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

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



87
88
89
90
91
92
# File 'lib/http_client.rb', line 87

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



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

def pool_stats
  return 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



73
74
75
76
77
78
# File 'lib/http_client.rb', line 73

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



80
81
82
83
84
85
# File 'lib/http_client.rb', line 80

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

#shutdownObject



124
125
126
# File 'lib/http_client.rb', line 124

def shutdown
  @connection_manager.shutdown
end