lib/hubspot/discovery/base_api_client.rb in hubspot-api-client-14.5.2 vs lib/hubspot/discovery/base_api_client.rb in hubspot-api-client-15.0.0

- old
+ new

@@ -1,6 +1,7 @@ -require_rel '../helpers/camel_case' +require_relative '../helpers/camel_case' +require_relative '../helpers/path' module Hubspot module Discovery module BaseApiClient attr_reader :base_params @@ -20,44 +21,79 @@ def config @config ||= new_config end def api_client - @api_client ||= Kernel.const_get("#{codegen_module_name}::ApiClient").new(config) + require_with_mapping "#{codegen_module_path}/api_client" + require_api_error + @api_client ||= Kernel.const_get( "#{codegen_module_name}::ApiClient").new(config) end def api + class_name = codegen_api_path.gsub(/(.*)\/(.*)/, '\2') + require_with_mapping codegen_api_path.gsub(class_name, "api/#{class_name}") + require_api_models @api ||= Kernel.const_get(codegen_api_class).new(api_client) end def get_all(params = {}) api.get_all(params) end private def new_config - config = Kernel.const_get("#{self.class.name.gsub('Discovery::', '').gsub(/(.*)::.*/, '\1')}::Configuration").new + require_with_mapping "#{codegen_module_path}/configuration" + config = Kernel.const_get("#{codegen_module_name}::Configuration").new config.access_token = base_params[:access_token] if base_params[:access_token] config.api_key['hapikey'] = base_params[:api_key] if base_params[:api_key] config.api_key['hapikey'] = base_params[:developer_api_key] if base_params[:developer_api_key] config end + def require_with_mapping(path) + Hubspot::Helpers::Path.new.require_with_mapping(path) + end + + def require_api_models + def (Kernel.const_get(codegen_module_name)).const_get(const) + require 'hubspot/helpers/path' + codegen_module_path = Hubspot::Helpers::Path.new.format(self.name).gsub('hubspot/', 'hubspot/codegen/') + codegen_model = Hubspot::Helpers::Path.new.format(const) + Hubspot::Helpers::Path.new.require_with_mapping("#{codegen_module_path}/models/#{codegen_model}") + super + rescue LoadError + super + end + end + + def require_api_error + require_with_mapping "#{codegen_module_path}/api_error" + end + def codegen_api_class self.class.name.gsub('Discovery::', '') end def codegen_module_name - codegen_api_class.gsub(/(.*)::.*/, '\1') + @codegen_module_name ||= codegen_api_class.gsub(/(.*)::.*/, '\1') end + def codegen_api_path + @codegen_api_path ||= Hubspot::Helpers::Path.new.format(self.class.name).gsub('discovery/', 'codegen/') + end + + def codegen_module_path + @codegen_module_path ||= codegen_api_path.gsub(/(.*)\/.*/, '\1') + end + def call_api(api_method, params_to_pass) api.public_send(api_method, *params_to_pass) end def call_api_with_rescue(api_method, params_to_pass) + require_api_error error = Kernel.const_get("#{codegen_module_name}::ApiError") call_api(api_method, params_to_pass) rescue error => e yield(e) end @@ -115,9 +151,10 @@ end params_to_pass = signature_params.map do |req, param| if params_with_defaults[param].nil? model_name = Hubspot::Helpers::CamelCase.new.format(param.to_s) + require_with_mapping "#{codegen_module_path}/models/#{param.to_s}" Kernel.const_get("#{codegen_module_name}::#{model_name}").build_from_hash(params_with_defaults[:body]) else params_with_defaults[param] end rescue NameError