lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.68 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.69
- old
+ new
@@ -48,16 +48,19 @@
retry_count = 0
begin
Soaspec::SpecLogger.add_to 'request_params: ' + payload.to_s
response = RestClient.post(token_url, payload, cache_control: 'no_cache', verify_ssl: false)
rescue RestClient::Exception => e
+ Soaspec::SpecLogger.add_to("oauth_error: #{e.message}")
+ Soaspec::SpecLogger.add_to("oauth_error: #{e.response}")
retry_count += 1
retry if retry_count < 3
raise e
end
Soaspec::SpecLogger.add_to("response_headers: #{response.headers}")
Soaspec::SpecLogger.add_to("response_body: #{response.body}")
+ Soaspec::SpecLogger.add_to("response: #{response}")
JSON.parse(response)
end
define_method('access_token') do
oauth_response['access_token']
@@ -136,15 +139,11 @@
options = name
name = self.class.to_s
end
super
set_remove_key(options, :default_hash)
- merged_options = rest_resource_options
- merged_options[:headers] ||= {}
- merged_options[:headers].merge! parse_headers
- merged_options.merge!(options)
- @resource = RestClient::Resource.new(ERB.new(base_url_value).result(binding), merged_options)
+ @init_options = options
end
# Convert snakecase to PascalCase
def convert_to_pascal_case(key)
return key if /[[:upper:]]/ =~ key[0] # If first character already capital, don't do conversion
@@ -166,20 +165,32 @@
else
request
end
end
+ # Initialize value of merged options
+ def init_merge_options
+ options = rest_resource_options
+ options[:headers] ||= {}
+ options[:headers].merge! parse_headers
+ options.merge(@init_options)
+ end
+
# Used in together with Exchange request that passes such override parameters
# @param [Hash] override_parameters Params to characterize REST request
# @param_value [params] Extra parameters (E.g. headers)
# @param_value [suburl] URL appended to base_url of class
- # @param_value [method] REST method (get, post, etc)
+ # @param_value [method] REST method (:get, :post, etc)
def make_request(override_parameters)
+ @merged_options ||= init_merge_options
test_values = override_parameters
test_values[:params] ||= {}
test_values[:method] ||= :post
test_values[:suburl] = test_values[:suburl].to_s if test_values[:suburl]
test_values[:params][:params] = test_values[:q] if test_values[:q] # Use q for query parameters. Nested :params is ugly and long
+
+ # In order for ERB to be calculated at correct time, the first time request is made, the resource should be created
+ @resource ||= RestClient::Resource.new(ERB.new(base_url_value).result(binding), @merged_options)
@resource_used = test_values[:suburl] ? @resource[test_values[:suburl]] : @resource
begin
response = case test_values[:method]
\ No newline at end of file