Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to inject custom hook just before request and just after response #5

Merged
merged 1 commit into from
Feb 10, 2012

Conversation

leonbreedt
Copy link
Contributor

Not sure how useful this would be to a wider audience, but I wanted some visibility into what was going out onto the wire and what was coming back.

Instead of hacking ugly log statements into Http::Client, and checking if a verbose option was supplied, I thought something a tad more general would be better, so this commit adds the ability to register a block to be invoked just before and just after the request is sent and response is received.

The block is passed request and response objects (currently the Net::HTTP ones, as they give me the most info).

Example usage:

def dump_request(request)
  puts "> #{request.method} #{request.path}"
  request.each_header do |header, value|
    puts "> #{header}: #{value}"
  end
end

def dump_response(response)
  puts
  puts "< HTTP/#{response.http_version} #{response.code} #{response.msg}"
  response.each_header do |header, value|
    puts "< #{header}: #{value}"
  end
  puts
end

def login(uri, access_code, password)
  form = {ac: access_code, pw: password}
  Http.with('User-Agent' => USER_AGENT)
      .on(:request) {|req| dump_request(req)}
      .on(:response) {|resp| dump_response(resp)}
      .post(uri.to_s, :form => form)
end

Cheers for the awesome lib.

… just

after a response is received. Useful for hacks like tracing.
@leonbreedt
Copy link
Contributor Author

Perhaps #before and #after are better names, though.

@blambeau blambeau mentioned this pull request Feb 9, 2012
tarcieri added a commit that referenced this pull request Feb 10, 2012
Ability to inject custom hook just before request and just after response
@tarcieri tarcieri merged commit 8ccb457 into httprb:master Feb 10, 2012
@tarcieri
Copy link
Member

I like on(:request) and on(:response) actually :)

@tarcieri
Copy link
Member

So there is a problem which I'm starting to take care of merging another patch (#6). I don't want to expose Net::HTTP internals as part of the API. In fact, I would like to completely rewrite the client using the http_parser gem.

I'd like to rework this a little so the callbacks give you Http::Request and Http::Response objects. Right now there's no Http::Request object per se, but it certainly makes sense to add one.

@leonbreedt
Copy link
Contributor Author

I've been thinking about it a bit and one thing which feels a bit off to me is introducing this new class, the sole purpose of which is to stash the block reference and register itself with the event callback list owned by the "chain".

It feels like methods on Chainable should be classified into methods which make additions to shared configuration (e.g. custom headers, callbacks, and so on), and terminal methods which perform actions using the shared configuration.

And it doesn't feel like a seperate class needs to be introduced for that - Perhaps thats what you were aiming with with the "Parameters" class?

Will ruminate on it some more :)

@blambeau
Copy link
Contributor

See also #7, as the current way of doing is not POLS about chainability. I'm currently experimenting along this in experimental branches on my repo (middlewares and true-chainable). Any comment welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants