Grape API(swagger doc) - 'desc'的全局配置

时间:2015-11-08 10:54:49

标签: ruby-on-rails api documentation swagger grape

这是一个有趣的忙碌的一周。我正在开发一个Rails项目,并包含Grape来实现 API

API有2个部分

  • 无需身份验证(无标题)
  • 需要验证

我设置了应用程序,一切正常......

  • 葡萄
  • Grape Swagger
  • Grape Swagger Rails

为了说明标题是必需的,我使用这样的东西......

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

现在的问题是我在许多类似的可安装API类中的这种描述。

有没有一种方法可以使这种常见(继承类型),以便我不需要用每个Grape方法定义它。

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

提前致谢,希望你们周末享受。

2 个答案:

答案 0 :(得分:5)

是的,有办法。我通过在class API中定义一个方法来实现这一点,以便在从API继承的所有内容中都可以访问它。类似的东西:

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

你可以这样访问它:

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

当然,还有更多方法,但它们取决于您的实施。

答案 1 :(得分:0)

我认为这可能是grape-api的更新版本,我必须使用它:

    desc 'My description text' do
       headers Authorization: {description: "pass the access token as Bearer", required: true }
   end