Add api key to every request in ActiveResource

前端 未结 7 1720
北荒
北荒 2020-12-31 01:20

I have 2 RESTful Rails apps I\'m trying to make talk to each other. Both are written in Rails 3 (beta3 at the moment). The requests to the service will require the use an ap

相关标签:
7条回答
  • 2020-12-31 02:22

    I'd recommend that you have a base class inheriting from ActiveResource::Base and override the self.collection_path and self.element_path class methods to always inject the API KEY something like:

    class Base < ActiveResource::Base
      def self.collection_path(prefix_options = {}, query_options = {})
       super(prefix_options, query_options.merge(api_key: THE_API_KEY))
      end
    
      def self.element_path(id, prefix_options = {}, query_options = {})
        super(id, prefix_options, query_options.merge(api_key: THE_API_KEY))
      end
    end
    
    class User < Base; end
    
    User.all # GET /users/?api_key=THE_API_KEY
    

    This will always inject your API_KEY in your ActiveResource method calls.

    0 讨论(0)
提交回复
热议问题