gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:15:12 25 4
gpt4 key购买 nike

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

API 有 2 个部分

  • 无需授权(无 header )
  • 需要授权

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

  • 葡萄
  • 葡萄 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

提前致谢,祝大家周末愉快。

最佳答案

是的,有办法。我通过在 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
}

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

关于ruby-on-rails - Grape API (swagger doc) - 'desc' 的全局配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com