Sha256: 04ed12f5f22bf976f422100413f030c3581416f92f7f936a0c400ba240f56426

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module FinAppsCore
  module REST
    module Connection # :nodoc:
      # @return [Faraday::Connection]
      def faraday(config, logger)
        options = connection_options config

        Faraday.new(options) do |conn|
          conn.request :accept_json
          conn.request :user_agent
          if config.valid_user_credentials?
            conn.request :no_encoding_basic_authentication, config.user_token
          else
            conn.request :tenant_authentication, config.tenant_token
          end
          conn.request :x_consumer_id, config.consumer_id if config.consumer_id
          conn.request :json
          conn.request :retry
          conn.request :multipart
          conn.request :url_encoded
          conn.request :request_id, config.request_id if config.request_id

          conn.use FinAppsCore::Middleware::RaiseError
          conn.response :json,
                        content_type: /\bjson$/,
                        parser_options: { symbolize_names: true }
          conn.response :logger, logger, bodies: true

          # Adapter (ensure that the adapter is always last.)
          conn.adapter Faraday.default_adapter
        end
      end
      module_function :faraday

      def connection_options(config)
        {
          url: "#{config.host}/v#{Defaults::API_VERSION}/",
          request: { open_timeout: config.timeout,
                     timeout: config.timeout }
        }
      end
      module_function :connection_options
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
finapps_core-5.0.7 lib/finapps_core/rest/connection.rb