2

Its been an interesting an busy week. I am working on a Rails project and included Grape to implement the API.

The API has 2 sections

  • No auth required (no headers)
  • Auth required

I setup the app with and all is working...

  • Grape
  • Grape Swagger
  • Grape Swagger Rails

For stating that a header is required I use some thing like this...

class ProfilesApi < Grape::API

  resource :profiles do

    desc 'List all profiles' do
      headers Authorization: {
                description: 'Validates identity through JWT provided in auth/login',
                required: true
              }
    end
    get do
      present User.all, with: Presenters::ProfilePresenter
    end
  end
end

Now the problem is that I this description in a lot of similar mountable API classes.

Is there a way that can kind of make this common (kind of inherited) so that I don't need to define it wit every Grape method.

    desc 'List all profiles' do
      headers Authorization: {
                description: 'Validates identity through JWT provided in auth/login',
                required: true
              }
    end

Thanks in advance and hope you guys enjoy the weekend.

Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71

2 Answers2

6

Yes, there's a way. I achieve that by defining a method in class API so that it's accessible in everything that inherits from API. Something like:

module Myapp
  class API < Grape::API
    def self.auth_headers
      { Authorization: { description: 'Validates identity through JWT provided in auth/login',required: true}}
    end
  end
end

And you access it like that:

desc "List all profiles", {
  headers: Myapp::API.auth_headers
}

Of course, there're much more ways but they depend on your implementation.

Brozorec
  • 1,163
  • 9
  • 15
0

i think this may be an updated version of grape-api thing, I've had to get this working with :

    desc 'My description text' do
       headers Authorization: {description: "pass the access token as Bearer", required: true }
   end
Ben
  • 1,292
  • 1
  • 13
  • 21