lib/hubspot/discovery/base_api_client.rb in hubspot-api-client-14.4.0 vs lib/hubspot/discovery/base_api_client.rb in hubspot-api-client-14.5.0
- old
+ new
@@ -57,10 +57,36 @@
call_api(api_method, params_to_pass)
rescue error => e
yield(e)
end
+ def call_api_with_retry(api_method, params_to_pass, retry_config, &block)
+ call_api_with_rescue(api_method, params_to_pass) do |error|
+ opts = retry_config.detect{ |k,v| k === error.code }&.last
+ retries = opts&.dig(:max_retries) || 5
+
+ block.call(error) unless block.nil?
+
+ response = error
+
+ while retries > 0 && opts
+ sleep opts[:seconds_delay] if opts[:seconds_delay]
+ response = call_api_with_rescue(api_method, params_to_pass) do |e|
+ block.call(e) unless block.nil?
+ e
+ end
+
+ return response unless response.respond_to?(:code)
+
+ opts = retry_config.detect{ |k,v| k === response.code }&.last
+ retries -= 1
+ end
+
+ response
+ end
+ end
+
def define_methods
define_api_methods
end
def define_api_methods
@@ -80,11 +106,11 @@
signature_params = api.method(api_method).parameters
signature_param_names = signature_params.map { |_, param| param }
params_with_defaults.each do |param_name, param_value|
- params_with_defaults[:opts][param_name] = param_value if !signature_param_names.include?(param_name) && param_name != :body
+ params_with_defaults[:opts][param_name] = param_value if !signature_param_names.include?(param_name) && param_name != :body && param_name != :retry
end
params_to_pass = signature_params.map do |req, param|
model_name = Hubspot::Helpers::CamelCase.new.format(param.to_s)
Kernel.const_get("#{codegen_module_name}::#{model_name}").build_from_hash(params_with_defaults[:body])
@@ -92,9 +118,10 @@
raise "Param #{param} is required for #{api.class}\##{api_method} method" if req == :req && params_with_defaults[param].nil?
params_with_defaults[param]
end
+ return call_api_with_retry(api_method, params_to_pass, params[:retry], &block) unless params[:retry].nil?
return call_api_with_rescue(api_method, params_to_pass, &block) unless block.nil?
call_api(api_method, params_to_pass)
end
end
end