Class: Manticore::Client
- Inherits:
-
Object
- Object
- Manticore::Client
- Includes:
- ProxiesInterface
- Defined in:
- lib/manticore/client.rb,
lib/manticore/client/proxies.rb
Overview
Core Manticore client, with a backing PoolingHttpClientConnectionManager
Defined Under Namespace
Modules: ProxiesInterface Classes: AsyncProxy, BackgroundProxy, BaseProxy, StubProxy
Constant Summary collapse
- DEFAULT_MAX_POOL_SIZE =
The default maximum pool size for requests
50
- DEFAULT_REQUEST_TIMEOUT =
60
- DEFAULT_SOCKET_TIMEOUT =
10
- DEFAULT_CONNECT_TIMEOUT =
10
- DEFAULT_MAX_REDIRECTS =
5
- DEFAULT_EXPECT_CONTINUE =
false
- DEFAULT_STALE_CHECK =
false
Instance Method Summary collapse
-
#clear_pending ⇒ Object
Remove all pending asynchronous requests.
-
#clear_stubs! ⇒ Object
Wipe all currently-set stubs.
-
#delete(url, options = {}, &block) ⇒ Response
Perform a HTTP DELETE request.
-
#execute! ⇒ Array
Execute all queued async requests.
-
#executor ⇒ Object
Get at the underlying ExecutorService used to invoke asynchronous calls.
-
#get(url, options = {}, &block) ⇒ Response
Perform a HTTP GET request.
-
#head(url, options = {}, &block) ⇒ Response
Perform a HTTP HEAD request.
-
#initialize(options = {}) {|builder, request_config| ... } ⇒ Client
constructor
Create a new HTTP client with a backing request pool.
-
#options(url, options = {}, &block) ⇒ Response
Perform a HTTP OPTIONS request.
-
#patch(url, options = {}, &block) ⇒ Response
Perform a HTTP PATCH request.
-
#pool_stats ⇒ Object
Return a hash of statistics about this client’s HTTP connection pool.
-
#post(url, options = {}, &block) ⇒ Response
Perform a HTTP POST request.
-
#put(url, options = {}, &block) ⇒ Response
Perform a HTTP PUT request.
-
#stub(url, stubs) ⇒ Object
Cause this client to return a stubbed response for this URL.
-
#unstub(url) ⇒ Object
Cause this client to unstubbed previously-stubbed URL.
Methods included from ProxiesInterface
#async, #background, #respond_with
Constructor Details
#initialize(options = {}) {|builder, request_config| ... } ⇒ Client
Create a new HTTP client with a backing request pool. if you pass a block to the initializer, the underlying HttpClientBuilder and RequestConfig.Builder will be yielded so that you can operate on them directly.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/manticore/client.rb', line 125 def initialize( = {}) builder = client_builder builder.set_user_agent .fetch(:user_agent, "Manticore #{VERSION}") @use_cookies = .fetch(:cookies, false) builder. unless @use_cookies builder.disable_content_compression if .fetch(:compression, true) == false builder.set_proxy get_proxy_host([:proxy]) if .key?(:proxy) builder.set_retry_handler do |exception, executionCount, context| if (executionCount > .fetch(:automatic_retries, 3)) false else case exception when Java::OrgApacheHttp::NoHttpResponseException, Java::JavaNet::SocketException context.setAttribute "retryCount", executionCount true else false end end end # This should make it easier to reuse connections # TODO: Determine what this actually does! # builder.disable_connection_state keepalive = .fetch(:keepalive, true) if keepalive == false builder.set_connection_reuse_strategy {|response, context| false } else builder.set_connection_reuse_strategy DefaultConnectionReuseStrategy.new end socket_config_builder = SocketConfig.custom socket_config_builder.set_so_timeout( .fetch(:socket_timeout, DEFAULT_SOCKET_TIMEOUT) * 1000 ) socket_config_builder.set_tcp_no_delay( .fetch(:tcp_no_delay, true) ) builder.set_default_socket_config socket_config_builder.build builder.set_connection_manager pool() request_config = RequestConfig.custom request_config.set_connection_request_timeout .fetch(:request_timeout, DEFAULT_REQUEST_TIMEOUT) * 1000 request_config.set_connect_timeout .fetch(:connect_timeout, DEFAULT_CONNECT_TIMEOUT) * 1000 request_config.set_socket_timeout .fetch(:socket_timeout, DEFAULT_SOCKET_TIMEOUT) * 1000 request_config.set_max_redirects .fetch(:max_redirects, DEFAULT_MAX_REDIRECTS) request_config.set_expect_continue_enabled .fetch(:expect_continue, DEFAULT_EXPECT_CONTINUE) request_config.set_stale_connection_check_enabled .fetch(:stale_check, DEFAULT_STALE_CHECK) request_config.set_circular_redirects_allowed false yield builder, request_config if block_given? builder.set_default_request_config request_config.build @client = builder.build @options = @async_requests = [] @stubs = {} end |
Instance Method Details
#clear_pending ⇒ Object
Remove all pending asynchronous requests.
264 265 266 267 |
# File 'lib/manticore/client.rb', line 264 def clear_pending @async_requests.clear nil end |
#clear_stubs! ⇒ Object
Wipe all currently-set stubs.
257 258 259 |
# File 'lib/manticore/client.rb', line 257 def clear_stubs! @stubs.clear end |
#delete(url, options = {}, &block) ⇒ Response
Perform a HTTP DELETE request
222 223 224 |
# File 'lib/manticore/client.rb', line 222 def delete(url, = {}, &block) request HttpDelete, url, , &block end |
#execute! ⇒ Array
Execute all queued async requests
272 273 274 275 276 277 |
# File 'lib/manticore/client.rb', line 272 def execute! method = executor.java_method(:submit, [java.util.concurrent.Callable.java_class]) result = @async_requests.map {|r| method.call r } @async_requests.clear result.map(&:get) end |
#executor ⇒ Object
Get at the underlying ExecutorService used to invoke asynchronous calls.
280 281 282 283 |
# File 'lib/manticore/client.rb', line 280 def executor create_executor_if_needed @executor end |
#get(url, options = {}, &block) ⇒ Response
Perform a HTTP GET request
198 199 200 |
# File 'lib/manticore/client.rb', line 198 def get(url, = {}, &block) request HttpGet, url, , &block end |
#head(url, options = {}, &block) ⇒ Response
Perform a HTTP HEAD request
210 211 212 |
# File 'lib/manticore/client.rb', line 210 def head(url, = {}, &block) request HttpHead, url, , &block end |
#options(url, options = {}, &block) ⇒ Response
Perform a HTTP OPTIONS request
228 229 230 |
# File 'lib/manticore/client.rb', line 228 def (url, = {}, &block) request HttpOptions, url, , &block end |
#patch(url, options = {}, &block) ⇒ Response
Perform a HTTP PATCH request
234 235 236 |
# File 'lib/manticore/client.rb', line 234 def patch(url, = {}, &block) request HttpPatch, url, , &block end |
#pool_stats ⇒ Object
Return a hash of statistics about this client’s HTTP connection pool
184 185 186 187 188 189 190 191 192 |
# File 'lib/manticore/client.rb', line 184 def pool_stats stats = @pool.get_total_stats { max: stats.get_max, leased: stats.get_leased, pending: stats.get_pending, available: stats.get_available } end |
#post(url, options = {}, &block) ⇒ Response
Perform a HTTP POST request
216 217 218 |
# File 'lib/manticore/client.rb', line 216 def post(url, = {}, &block) request HttpPost, url, , &block end |
#put(url, options = {}, &block) ⇒ Response
Perform a HTTP PUT request
204 205 206 |
# File 'lib/manticore/client.rb', line 204 def put(url, = {}, &block) request HttpPut, url, , &block end |
#stub(url, stubs) ⇒ Object
Cause this client to return a stubbed response for this URL
247 248 249 |
# File 'lib/manticore/client.rb', line 247 def stub(url, stubs) @stubs[url] = stubs end |
#unstub(url) ⇒ Object
Cause this client to unstubbed previously-stubbed URL
252 253 254 |
# File 'lib/manticore/client.rb', line 252 def unstub(url) @stubs.delete(url) end |