before_filter
does Grape provide a mechanism like before_filter in rails controller?
say:
resources :missions do
before_filter :authenticate!, :only => [:post]
post do
create mission here...
end
get do
return all missions here...
end
end
You can't specify :only or :except, but Grape does have a universal before mechanism:
class Something < Grape::API
before do
# before code
end
get '/awesome' do
# the 'before' code will run before this code
end
end
As far as I can tell, the before method doesn't exist. I have started to implement the functionality on my own and would love to contribute it. Am I doing something wrong or does the above code not work (as in: https://github.com/intridea/grape/issues/58)?
If you really need to have a before filter only for certain requests, how is that done ?
@letronje If I am not mistaken these are scoped to the API class, and you could mount multiple of these (I could be wrong, would be nice to extend the documentation either way).
I have same question as @letronje ...How on earth do you only require authentication for certain requests? It's super easy with before_filter, but I can't figure it out for the life of me with this.
Did you try my response above? I mean write two classes, AuthenticatedRequestsAPI and UnauthenticatedRequestsAPI, mount them into the same API. Only one would have a before filter.
@dblock Can you provide an example? @rubencodes @letronje @leondu Did you ever figure this out?
I'd like to set locale for all actions, how can I do that with all API? I tried with BaseAPI, and define like this:
class BaseAPI < Grape::API
before do
I18n.locale = env['rack.session'][:lang]
end
end
then others will be extended from this class, but above snippet is never run
@tam-vo It should. Try this in the actual API class, I imagine that will work. Then try again in the base class, if it doesn't work open a new bug please (with a test would be nice).
I wrote two classes for auth and none-auth. But it does not work, please have a look what's wrong with it. The root Class:
class API < Grape::API
prefix 'api'
format :json
rescue_from :all
mount AuthLessAPI
mount AuthRequiredAPI
end
The auth required class
class AuthRequiredAPI < Grape::API
before do
error!("401 Unauthorized, 401") unless authenticated
end
mount UserAPI
mount ...
end
The auth less class
class AuthLessAPI < Grape::API
... some api ...
end
The API in the UserAPI does not required authontication.
@birbird You'll have to provide a lot more detail on "it doesn't work". I suggest opening a new issue and making code that clearly demonstrates the problem.
is there any way to scope the filter like "before" till some extent, not for all the post/get requests?
Yes, see https://github.com/ruby-grape/grape#before-and-after.
If anyone else comes here, I was having trouble getting callbacks to work in a base grape class that mounted others. The solution for me was to make sure the before/after callbacks are called before the mounting of the other classes are called.
Here's an example from our api:
module Api
module V2
class Base < Grape::API
extend Api::V2::Documentation
extend Api::V2::Authorization
include ApiHelper
after do # must be called before the mount calls below!
track_request
end
mount Api::V2::Endpoints::Auth
mount Api::V2::Endpoints::Users
end
end
end