加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用active_model_serializers实现API版本控制

发布时间:2020-12-17 03:18:44 所属栏目:百科 来源:网络整理
导读:我知道已经存在一些问题以及 this is a open issue regarding AMS not handling namespaces too efficiently(这种版本化方法使用)但我想确保我在当前约束条件下处于正确的轨道. 现在我正在使用Rails 5和AMS 0.10.1,所以我做了以下事情: # config/initialize
我知道已经存在一些问题以及 this is a open issue regarding AMS not handling namespaces too efficiently(这种版本化方法使用)但我想确保我在当前约束条件下处于正确的轨道.

现在我正在使用Rails 5和AMS 0.10.1,所以我做了以下事情:

# config/initializers/active_model_serializer.rb
ActiveModelSerializers.config.serializer_lookup_enabled = false

禁用默认的序列化程序查找(无论如何都不起作用);和

# app/controllers/application_controller.rb
class ApplicationController < ActionController::API
  def get_serializer(resource,options = {})
    unless options[:each_serializer] || options[:serializer] then
      serializer = (self.class.name.gsub("Controller","").singularize + "Serializer").constantize
      resource.respond_to?(:to_ary) ? options[:each_serializer] = serializer : options[:serializer] = serializer
    end
    super(resource,options)
  end
end

覆盖默认情况下如何找到序列化程序;我的控制器和序列化器是这样的:

# app/controllers/api/v2/api_controller.rb
module Api::V2
  class ApiController < ApplicationController
    ...

# app/controllers/api/v2/users_controller.rb
module Api::V2
  class UsersController < ApiController
    ...

# app/serializers/api/v2/user_serializer.rb
module Api::V2
  class UserSerializer < ActiveModel::Serializer
    ...

现在,像ActiveModel :: Serializer.serializer_for(object)这样的东西将无法工作,所以我不得不使用example.metadata [:api_version]修补我的请求规范,以便在每次测试之前设置API版本,如果是示例没有设置它.

所以:

>有没有更好的记录方法?
>这是否接近正确?
>我会用这种方法进一步面对问题吗?
>如何改进?

解决方法

我想你在这里有什么好的.我使用相同的方法,它适用于我的应用程序.我从Ryan Bates那里挑选了最初的想法,他在那里解释了非常类似的方法

http://railscasts.com/episodes/350-rest-api-versioning

这是我用来为每个资源指定不同的序列化器:

module API
  module V3
    class AssetController < API::V3::ApiController
      def index
        render json: assets,status: :ok,each_serializer: API::V3::Serializers::AssetSerializer
      end
  end
end

在我的实现中,我在api / controllers / api / v3 / serializers中使用序列化器.所以你要对序列化程序类和控制器类进行版本控制

不确定你真的需要get_serializer,因为这更明确但不是什么大不了的事

如果你有很多api端点尝试在资源中组织它们.在我的config / routes.rb中,我有大约700个资源,所以我将它们分成单独的文件config / api / v1 / routes.rb …

namespace :api,defaults: {format: 'json'} do
  namespace :v1
    resources :assets
  end
end

在inflections.rb初始化器内部也很方便

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

对我来说,我认为最重要的问题是要有良好的测试覆盖率.我更喜欢spec并检查正确的状态代码200,201,…等以及使用json_schema的正确子输出

如果您需要进行身份验证,那么我建议您使用基于令牌的身份验证和JWT – JSON Web令牌.在我的实现中,我使用两个令牌.在执行POST和PATCH时读取一个令牌和不同的令牌(不确定是否需要).所以在API控制器里面就是这样的

class ApiController < ActionController::Base
  skip_before_action :verify_authenticity_token,if: :json_request?
  before_action :authenticate

  protected
  def json_request?
    request.format.json?
  end
  if request.headers['X-Authorization']
    token = request.headers['X-Authorization']
    payload = JWT.decode(token,'my_custom_key_to_check_if_key_has_been_tempered d_on_client_side')[0]
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读