我使用Grape创建API,我遇到了问题:如何在V2中安装V1
/api/v1/users.rb
module API
module V1
class Users < Grape::API
resource :users do
desc "Timeline" do
end
get :time_line do
{status: true, version: 'v1'}
end
desc "Timeline" do
end
get :profile do
{status: true, profile: {email: "julian@vinova.sg"}, version: 'v1'}
end
end
end
end
end
/api/v1/base.rb
module API
module V1
class Base < Grape::API
prefix 'api'
format :json
version 'v1', using: :path
mount API::V1::Users
add_swagger_documentation \
api_version: 'v1'
end
end
end
/api/v2/users.rb
module API
module V2
class Users < Grape::API
resource :users do
desc "Timeline" do
end
params do
use :pagination
end
get :time_line do
{status: true, version: 'v2'}
end
end
end
end
end
/api/v2/base.rb
module API
module V2
class Base < Grape::API
prefix 'api'
format :json
version 'v2', using: :path
mount API::V2::Users
add_swagger_documentation \
api_version: 'v2'
end
end
end
/api/base.rb
module API
class Base < Grape::API
mount ::API::V1::Base
mount ::API::V2::Base
end
end
我想:配置文件(/ api / v2 / users / profile)也可以在V2中实现,而不必重复代码。现在,有什么建议吗? 感谢