lib/finapps/rest/connection.rb in finapps-0.22.2.pre vs lib/finapps/rest/connection.rb in finapps-0.22.3.pre

- old
+ new

@@ -7,65 +7,63 @@ # @param [Hash] company_credentials # @param [Hash] config # @return [Faraday::Connection] def set_up_connection(company_credentials, config) - logger.debug "##{__method__.to_s} => Started" - company_credentials.validate_required_strings! - logger.debug "##{__method__.to_s} => company_credentials passed validation." + logger.debug "##{__method__.to_s} => company_credentials were provided." host = config[:host] validate_host_url! host base_url = "#{host}/v#{API_VERSION}" logger.debug " base_url: #{base_url}" timeout = config[:timeout].blank? ? DEFAULTS[:timeout] : config[:timeout] logger.debug " timeout: #{timeout}" - user_identifier = config[:user_identifier] - logger.debug " user_identifier: #{user_identifier}" if user_identifier.present? + Faraday.new(:url => base_url, + :request => { + :open_timeout => timeout, + :timeout => timeout}, + :headers => { + :accept => HEADERS[:accept], + :user_agent => HEADERS[:user_agent]}) do |conn| - user_token = config[:user_token] - logger.debug ' user_token: [REDACTED]' if user_token.present? + set_request_middleware(conn, company_credentials) + set_basic_authentication(conn, config) + set_response_middleware(conn) - connection = Faraday.new(:url => base_url, - :request => { - :open_timeout => timeout, - :timeout => timeout}, - :headers => { - :accept => HEADERS[:accept], - :user_agent => HEADERS[:user_agent]}) do |conn| - - # Request Middleware - conn.use FinApps::Middleware::ApiToken, company_credentials - conn.request :json - conn.request :retry - conn.request :multipart - conn.request :url_encoded - if user_identifier.blank? || user_token.blank? - logger.debug "##{__method__.to_s} => User credentials were not provided. Authentication header not set." - else - conn.request :basic_auth, user_identifier, user_token - logger.debug "##{__method__.to_s} => Basic Authentication header set for provided user credentials." - end - - # Response Middleware - 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 - - logger.debug "##{__method__.to_s} => Completed" - connection 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." \ No newline at end of file