lib/3scale/client.rb in 3scale_client-2.9.0 vs lib/3scale/client.rb in 3scale_client-2.10.0

- old
+ new

@@ -5,10 +5,11 @@ require '3scale/client/http_client' require '3scale/client/version' require '3scale/response' require '3scale/authorize_response' +require '3scale/rack_query' module ThreeScale Error = Class.new(RuntimeError) class ServerError < Error @@ -45,10 +46,13 @@ 'deprecated. In next versions, the signature of the report method is '\ 'going to be: '\ 'def report(transactions: [], service_id: nil).'.freeze private_constant :DEPRECATION_MSG_OLD_REPORT + EXTENSIONS_HEADER = '3scale-options'.freeze + private_constant :EXTENSIONS_HEADER + def initialize(options) if options[:provider_key].nil? || options[:provider_key] =~ /^\s*$/ raise ArgumentError, 'missing :provider_key' end @@ -67,10 +71,11 @@ def authrep(options) path = "/transactions/authrep.xml?provider_key=#{CGI.escape(provider_key)}" options_usage = options.delete :usage options_log = options.delete :log + extensions = options.delete :extensions options.each_pair do |param, value| path += "&#{param}=#{CGI.escape(value.to_s)}" end @@ -84,11 +89,12 @@ log << "#{escaped_key}=#{CGI.escape(value)}" end path += "&#{log.join('&')}" end - http_response = @http.get(path) + headers = extensions_to_header extensions if extensions + http_response = @http.get(path, headers: headers) case http_response when Net::HTTPSuccess,Net::HTTPConflict build_authorize_response(http_response.body) when Net::HTTPClientError @@ -146,11 +152,11 @@ # == Note # # The signature of this method is a bit complicated because we decided to # keep backwards compatibility with a previous version of the method: # def report(*transactions) - def report(*reports, transactions: [], service_id: nil, **rest) + def report(*reports, transactions: [], service_id: nil, extensions: nil, **rest) if (!transactions || transactions.empty?) && rest.empty? raise ArgumentError, 'no transactions to report' end transactions = transactions.concat(reports) @@ -162,11 +168,12 @@ payload = encode_transactions(transactions) payload['provider_key'] = CGI.escape(provider_key) payload['service_id'] = CGI.escape(service_id.to_s) if service_id - http_response = @http.post('/transactions.xml', payload) + headers = extensions_to_header extensions if extensions + http_response = @http.post('/transactions.xml', payload, headers: headers) case http_response when Net::HTTPSuccess build_report_response when Net::HTTPClientError @@ -187,10 +194,11 @@ # a key defined. # service_id:: id of the service (required if you have more than one service) # usage:: predicted usage. It is optional. It is a hash where the keys are metrics # and the values their predicted usage. # Example: {'hits' => 1, 'my_metric' => 100} + # extensions:: Optional. Hash of extension keys and values. # # == Return # # An ThreeScale::AuthorizeResponse object. It's +success?+ method returns true if # the authorization is successful, false otherwise. It contains additional information @@ -208,13 +216,15 @@ # if response.success? # # All good. Proceed... # end # def authorize(options) + extensions = options.delete :extensions path = "/transactions/authorize.xml" + options_to_params(options, ALL_PARAMS) - http_response = @http.get(path) + headers = extensions_to_header extensions if extensions + http_response = @http.get(path, headers: headers) case http_response when Net::HTTPSuccess,Net::HTTPConflict build_authorize_response(http_response.body) when Net::HTTPClientError @@ -257,13 +267,15 @@ # if response.success? # # All good. Proceed... # end # def oauth_authorize(options) + extensions = options.delete :extensions path = "/transactions/oauth_authorize.xml" + options_to_params(options, OAUTH_PARAMS) - http_response = @http.get(path) + headers = extensions_to_header extensions if extensions + http_response = @http.get(path, headers: headers) case http_response when Net::HTTPSuccess,Net::HTTPConflict build_authorize_response(http_response.body) when Net::HTTPClientError @@ -273,14 +285,12 @@ end end private - # The support for the 'hierarchy' param is experimental. Its support is not - # guaranteed for future versions. - OAUTH_PARAMS = [:app_id, :app_key, :service_id, :redirect_url, :usage, :hierarchy] - ALL_PARAMS = [:user_key, :app_id, :app_key, :service_id, :redirect_url, :usage, :hierarchy] + OAUTH_PARAMS = [:app_id, :app_key, :service_id, :redirect_url, :usage] + ALL_PARAMS = [:user_key, :app_id, :app_key, :service_id, :redirect_url, :usage] REPORT_PARAMS = [:user_key, :app_id, :service_id, :timestamp] def options_to_params(options, allowed_keys) params = { :provider_key => provider_key } @@ -379,8 +389,13 @@ node = doc.at_css('error') response = klass.new response.error!(node.content.to_s.strip, node['code'].to_s.strip) response + end + + # Encode extensions header + def extensions_to_header(extensions) + { EXTENSIONS_HEADER => RackQuery.encode(extensions) } end end end