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

Get the request object from the response? #463

Closed
thegreyfellow opened this issue Feb 26, 2018 · 10 comments · Fixed by #546
Closed

Get the request object from the response? #463

thegreyfellow opened this issue Feb 26, 2018 · 10 comments · Fixed by #546

Comments

@thegreyfellow
Copy link

thegreyfellow commented Feb 26, 2018

Is it possible to get the request object from the response?
Sometimes if not always when debugging if a response failed I need to know if I missed something on the request body or headers, but now I have no way to do that. if there is any work around that would be great.

Thanks.


HTTPrb v3.0.0
ruby v2.3.5

@ixti
Copy link
Member

ixti commented Feb 26, 2018

It's not doable atm - response has no "knowledege" of request (except request URI) - but in face of thinking about next major version update I really think we should change that. I also find it pretty useful to be able to get request object that was used to get response.

@ixti
Copy link
Member

ixti commented Feb 26, 2018

You can "monkey-patch" client for your needs atm though:

module HTTP
  class Request
    attr_accessor :req
  end

  class Client
    def perform(req, options)
      super(req, options).tap { |res| res.req = req }
    end
  end
end

@thegreyfellow
Copy link
Author

Thanks for the quick response, not sure how to use this piece of code though, I tried to include it inside the classe where I require 'http' but I get this error:

NoMethodError (super: no superclass method `perform' for #<HTTP::Client:0x00007fad94d1c358>):

@ixti
Copy link
Member

ixti commented Feb 26, 2018

Oh. Sorry. I was thinking about different approach and forgot to "adapt" it to a more generic solution. Was thinking about re-defining perform on client instance... Anyway here's working example:

module HTTP
  class Response
    attr_accessor :req
  end

  class Client
    alias_method :__perform__, :perform

    def perform(req, options)
      __perform__(req, options).tap { |res| res.req = req }
    end
  end
end

@thegreyfellow
Copy link
Author

Thanks that worked! I agree this feature would be very interesting to have, it will aid in debugging a lot. Any ideas on where to start, to implement it in the gem?

@ixti
Copy link
Member

ixti commented Feb 26, 2018

It's in fact trivial to implement in the gem, so instead of passing only uri keyword to response, we can pass whole request:

https://github.com/httprb/http/blob/master/lib/http/client.rb#L74-L83

Obviosuly, Response needs to be updated to match this change as well. This will be a massive backward incompatible change - but that's fine for next (4.x) releases.

/cc @httprb/core

@thegreyfellow
Copy link
Author

Awesome! that would be great to see it happen in v4.x. Thanks @ixti :)

joshuaflanagan added a commit to ShippingEasy/http that referenced this issue Apr 24, 2019
This fixes httprb#463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
joshuaflanagan added a commit to ShippingEasy/http that referenced this issue Apr 24, 2019
This fixes httprb#463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
joshuaflanagan added a commit to ShippingEasy/http that referenced this issue Apr 24, 2019
This fixes httprb#463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
joshuaflanagan added a commit to ShippingEasy/http that referenced this issue Apr 24, 2019
This fixes httprb#463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
@uberllama
Copy link

Any movement on this? Part of my requirement in choosing an HTTP lib is that I can easily dump out both the assembled request and the response to database audit columns. As it stands, I have to build up a request manually from an outer wrapper that takes the args I will pass to httprb.

@kris
Copy link

kris commented Oct 19, 2019

I really enjoy working with this library, except for the lacking debug of the request object. Any movement on this (outside of a monkey patch) would be great.

@tarcieri
Copy link
Member

If @joshuaflanagan can update #546 I'd consider merging it

joshuaflanagan added a commit to ShippingEasy/http that referenced this issue Oct 20, 2019
This fixes httprb#463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
joshuaflanagan added a commit to ShippingEasy/http that referenced this issue Oct 20, 2019
This fixes httprb#463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
tarcieri pushed a commit that referenced this issue Oct 21, 2019
This fixes #463

This will greatly enhance the abilities of pluggable `Features` by
being able to reference the Request that initiated a Response.
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 a pull request may close this issue.

5 participants