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

Ziy*_*een 2 api documentation ruby-on-rails ruby-grape swagger

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

API有2个部分

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

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

  • 葡萄
  • 葡萄摇摆
  • 葡萄摇摆街

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

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
Run Code Online (Sandbox Code Playgroud)

现在问题是我在很多类似的可安装API类中进行了这种描述.

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

    desc 'List all profiles' do
      headers Authorization: {
                description: 'Validates identity through JWT provided in auth/login',
                required: true
              }
    end
Run Code Online (Sandbox Code Playgroud)

提前谢谢,希望你们周末愉快.

Bro*_*rec 6

是的,有办法.我通过定义一个方法来实现它,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
Run Code Online (Sandbox Code Playgroud)

你这样访问它:

desc "List all profiles", {
  headers: Myapp::API.auth_headers
}
Run Code Online (Sandbox Code Playgroud)

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