ruby-grape / grape

An opinionated framework for creating REST-like APIs in Ruby.
http://www.ruby-grape.org
MIT License
9.88k stars 1.22k forks source link

Access response_object/@body return in after filter #1695

Open stevelacey opened 6 years ago

stevelacey commented 6 years ago

I am attempting to implement API versioning ala Stripe (https://stripe.com/blog/api-versioning).

I currently have a working prototype that hooks into Grape before/after hooks – but to modify the response I have to reparse and regenerate the JSON from response.body – when obviously I would prefer to hook in earlier and muck with the hash before it's jsonified – it also means I have to correct the headers e.g. Content-Length, if I want those to be correct.

I see in https://github.com/ruby-grape/grape/blob/master/lib/grape/endpoint.rb#L262-L265 you now assign response_object to @body but not until after the after hooks have fired.

Is there a way for me to get at this? Is there a reason I shouldn't? Is there a reason @body shouldn't be assigned sooner?

dblock commented 6 years ago

I don't see an immediate reason why you shouldn't have access to the response body in after, feel free to PR.

stevelacey commented 6 years ago

@dblock I am not entirely sure how it'd work if I simply move that line up – I can probably edit the @body hash by reference but that seems weird

Am I right in assuming that others are mostly using these hooks for injecting headers and stuff? Is request/response manipulation more significant than that a case you want to support? I intend to adjust params going in and json coming out

dblock commented 6 years ago

I think you should definitely be able to modify the response body on the way out other than in a middleware, so please PR something with tests, etc.

stevelacey commented 6 years ago

Also, any suggestions how to get at params? A use-case I intend to support is renaming params coming in to the system. Maybe I want Rack middleware rather than Grape..

dblock commented 6 years ago

Params can be modified in before AFAIK.

stevelacey commented 6 years ago

@dblock only env is available... and I'm not sure how to pull params out of that and/or put them back in a meaningful way – do I need a pass through params builder?

dblock commented 6 years ago

Is this already run? https://github.com/ruby-grape/grape/blob/91c6c78ae3d3f3ffabaf57ffc4dc35ab7cfc7b5f/lib/grape/middleware/globals.rb

stevelacey commented 6 years ago

Right but does modifying though achieve anything? On Fri, 27 Oct 2017 at 20:49, Daniel Doubrovkine (dB.) @dblockdotorg < notifications@github.com> wrote:

Is this already run? https://github.com/ruby-grape/grape/blob/91c6c78ae3d3f3ffabaf57ffc4dc35ab7cfc7b5f/lib/grape/middleware/globals.rb

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ruby-grape/grape/issues/1695#issuecomment-339976707, or mute the thread https://github.com/notifications/unsubscribe-auth/AARq-1nxScsBOLAFDDoISMeeuFMAycYnks5swd92gaJpZM4QD5Xn .

dblock commented 6 years ago

I am not sure. Try to build specs for what you're trying to achieve and I can see if I have time to help.

stevelacey commented 6 years ago

@dblock global doesn't appear to have run, should it have?

dblock commented 6 years ago

Don't know, most of this was refactored in the last 3-4 versions of Grape and i haven't touched much code. Sorry i can't be more helpful.