lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.58 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.59

- old
+ new

@@ -23,50 +23,30 @@ end # Will create access_token method based on passed parameters def oauth2(client_id: nil, client_secret: nil, token_url: nil, username: nil, password: nil, security_token: nil) define_method('oauth_response') do - response = if password && username && security_token - RestClient.post( - token_url, - { - grant_type: 'password', - client_id: client_id, - client_secret: client_secret, - username: username, - password: (password + security_token), - multipart: true - }, - cache_control: 'no_cache', - verify_ssl: false - ) - elsif password && username - RestClient.post( - token_url, - { - grant_type: 'password', - client_id: client_id, - client_secret: client_secret, - username: username, - password: password, - multipart: true - }, - cache_control: 'no_cache', - verify_ssl: false - ) - else - RestClient.post( - token_url, - { - grant_type: 'client_credentials', - client_id: client_id, - client_secret: client_secret - }, - cache_control: 'no_cache', - verify_ssl: false - ) - end + payload = if password && username + { + grant_type: 'password', + client_id: client_id, + client_secret: client_secret, + username: username, + password: security_token ? (password + security_token) : password, + multipart: true + } + else + { + grant_type: 'client_credentials', + client_id: client_id, + client_secret: client_secret + } + end + response = RestClient.post(token_url, payload, cache_control: 'no_cache', verify_ssl: false) + Soaspec::SpecLogger.add_to 'request_params: ' + payload.to_s + Soaspec::SpecLogger.add_to('response_headers: ' + response.headers.to_s) + Soaspec::SpecLogger.add_to('response_body: ' + response.to_s) JSON.parse(response) end define_method('access_token') do oauth_response['access_token'] @@ -121,10 +101,15 @@ def rest_resource_options { } end + def parse_headers + # rest_client_headers.map { |h| ERB.new(h).result(binding) } + Hash[rest_client_headers.map { |k, header| [k, ERB.new(header).result(binding)] }] + end + # Setup object to handle communicating with a particular SOAP WSDL # @param [Hash] options Options defining SOAP request. WSDL, authentication def initialize(name = self.class.to_s, options = {}) raise "Base URL not set! Please set in class with 'base_url' method" unless base_url_value @default_hash = {} @@ -134,10 +119,10 @@ end super set_remove_key(options, :default_hash) merged_options = rest_resource_options merged_options[:headers] ||= {} - merged_options[:headers].merge! rest_client_headers + merged_options[:headers].merge! parse_headers merged_options.merge!(options) @resource = RestClient::Resource.new(base_url_value, merged_options) # @resource[url_extension].get end # Used in together with Exchange request that passes such override parameters \ No newline at end of file