lib/finapps/rest/connection.rb in finapps-0.22.4.pre vs lib/finapps/rest/connection.rb in finapps-1.0.0

- old
+ new

@@ -7,69 +7,44 @@ # @param [Hash] company_credentials # @param [Hash] config # @return [Faraday::Connection] def set_up_connection(company_credentials, config) - company_credentials.validate_required_strings! - logger.debug "##{__method__.to_s} => company_credentials were provided." + host_url = config[:host].blank? ? DEFAULTS[:host] : config[:host] + raise InvalidArgumentsError.new "Invalid argument: host_url: #{host_url}" unless host_url.start_with?('http://', 'https://') - host = config[:host] - validate_host_url! host - - base_url = "#{host}/v#{API_VERSION}" - logger.debug " base_url: #{base_url}" - + base_url = "#{host_url}/v#{API_VERSION}" timeout = config[:timeout].blank? ? DEFAULTS[:timeout] : config[:timeout] - logger.debug " timeout: #{timeout}" Faraday.new(:url => base_url, :request => { :open_timeout => timeout, :timeout => timeout}, :headers => { :accept => HEADERS[:accept], :user_agent => HEADERS[:user_agent]}) do |conn| - set_request_middleware(conn, company_credentials) - set_basic_authentication(conn, config) - set_response_middleware(conn) + # add basic authentication header if user credentials were provided + user_identifier = config[:user_identifier] + user_token = config[:user_token] + conn.request :basic_auth, user_identifier, user_token unless user_identifier.blank? || user_token.blank? + # company level authentication + conn.use FinApps::Middleware::ApiToken, company_credentials + + conn.request :json + conn.request :retry + conn.request :multipart + conn.request :url_encoded + conn.use FinApps::Middleware::RaiseHttpExceptions + conn.response :rashify + conn.response :json, :content_type => /\bjson$/ + conn.use FinApps::Middleware::ResponseLogger + # Adapter (ensure that the adapter is always last.) conn.adapter :typhoeus end end - private - def set_response_middleware(conn) - conn.use FinApps::Middleware::RaiseHttpExceptions - conn.response :rashify - conn.response :json, :content_type => /\bjson$/ - conn.use FinApps::Middleware::ResponseLogger - end - - def set_request_middleware(conn, company_credentials) - conn.use FinApps::Middleware::ApiToken, company_credentials - conn.request :json - conn.request :retry - conn.request :multipart - conn.request :url_encoded - end - - def set_basic_authentication(conn, config) - if config[:user_identifier].blank? || config[:user_token].blank? - logger.debug "##{__method__.to_s} => User credentials were not provided. Authentication header not set." - else - conn.request :basic_auth, config[:user_identifier], config[:user_token] - logger.debug "##{__method__.to_s} => Basic Authentication header set for provided user credentials." - end - end - - def validate_host_url!(host_url) - raise MissingArgumentsError.new 'Missing argument: host_url.' if host_url.blank? - raise InvalidArgumentsError.new 'Invalid argument: host_url does not specify a valid protocol (http/https).' unless host_url.start_with?('http://', 'https://') - - logger.debug "##{__method__.to_s} => host [#{host_url}] passed validation." - end - end end -end \ No newline at end of file +end