httpi icon indicating copy to clipboard operation
httpi copied to clipboard

Allow common `block` for all requests

Open Wardrop opened this issue 5 years ago • 3 comments

As a suggestion, I often have the requirement to setup the adapter client for every request. For example, with the httpclient adapter I often need to do the following:

response = HTTPI.get(request) do |x|
  x.ssl_config.set_default_paths
  x.force_basic_auth = true
end

To save repeating myself, or otherwise creating my own "request" method, it would be nice if HTTPI allowed one to set a global block to be executed before each request to setup the client. Given that the adapter is set globally, it makes sense to allow an adapter-specific block to be set globally as well.

Something like this:

HTTPI.adapter = :httpclient
HTTPI.adapter_client_setup = proc do |x|
  x.ssl_config.set_default_paths
  x.force_basic_auth = true
end

When a request is made, this block would be executed before the request-specific block. In the following example, force_basic_auth would be turned off for this particular request, but ssl_config.set_default_paths would remain set.

HTTPI.adapter = :httpclient
HTTPI.adapter_client_setup = proc do |x|
  x.ssl_config.set_default_paths
  x.force_basic_auth = true
end
# ...
HTTPI.get(request) do |x|
  x.force_basic_auth = false
end

Thoughts?

Wardrop avatar Apr 23 '19 02:04 Wardrop

By the way, happy to send through a pull request.

Wardrop avatar Apr 23 '19 02:04 Wardrop

Hi Tom @Wardrop

To save repeating myself, or otherwise creating my own "request" method, it would be nice if HTTPI allowed one to set a global block to be executed before each request to setup the client. Given that the adapter is set globally, it makes sense to allow an adapter-specific block to be set globally as well.

I like adapter_client_setup block. 👍

Feel free to send a PR. I'm planning to merge #201 and #198 and release a 2.5.0 version. We can add this feature on 2.5.0 or 2.5.1. Ok?

rogerleite avatar Apr 27 '19 15:04 rogerleite

No worries, pull request made.

Wardrop avatar Apr 29 '19 00:04 Wardrop

Resolved in #237 / #203

pcai avatar Oct 01 '22 04:10 pcai