diff --git a/CHANGELOG.md b/CHANGELOG.md index 698383ad..8b6466cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 5.2.0 (2023-11-07) +- Allow passing a Faraday connection configuration block to the client. + ## 5.1.0 (2023-09-29) - Remove logic extracting parameters from document in bulk requests. Parameters now must be sent separately from the document to be parsed correctly. diff --git a/lib/elastomer_client/client.rb b/lib/elastomer_client/client.rb index db772ad6..df1225f8 100644 --- a/lib/elastomer_client/client.rb +++ b/lib/elastomer_client/client.rb @@ -49,7 +49,7 @@ def initialize(host: "localhost", port: 9200, url: nil, read_timeout: 5, open_timeout: 2, max_retries: 0, retry_delay: 0.075, opaque_id: false, adapter: Faraday.default_adapter, max_request_size: MAX_REQUEST_SIZE, strict_params: false, es_version: nil, compress_body: false, compression: Zlib::DEFAULT_COMPRESSION, - basic_auth: nil, token_auth: nil) + basic_auth: nil, token_auth: nil, &block) @url = url || "http://#{host}:#{port}" @@ -70,6 +70,7 @@ def initialize(host: "localhost", port: 9200, url: nil, @compression = compression @basic_auth = basic_auth @token_auth = token_auth + @connection_block = block end attr_reader :host, :port, :url @@ -155,6 +156,8 @@ def connection conn.basic_auth(@basic_auth[:username], @basic_auth[:password]) end + @connection_block&.call(conn) + if @adapter.is_a?(Array) conn.adapter(*@adapter) else diff --git a/lib/elastomer_client/version.rb b/lib/elastomer_client/version.rb index a47140b7..776761f9 100644 --- a/lib/elastomer_client/version.rb +++ b/lib/elastomer_client/version.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module ElastomerClient - VERSION = "5.1.0" + VERSION = "5.2.0" def self.version VERSION diff --git a/test/client_test.rb b/test/client_test.rb index 8d6cf5fa..f5fee84f 100644 --- a/test/client_test.rb +++ b/test/client_test.rb @@ -11,6 +11,18 @@ assert_includes c.connection.builder.handlers, Faraday::Adapter::Test end + it "allows configuring the Faraday when a block is given" do + assert ElastomerClient::Client.new.connection.builder.handlers.none? { |handler| handler.klass == FaradayMiddleware::Instrumentation } + + c = ElastomerClient::Client.new do |connection| + assert_kind_of(Faraday::Connection, connection) + + connection.use :instrumentation + end + + assert c.connection.builder.handlers.any? { |handler| handler.klass == FaradayMiddleware::Instrumentation } + end + it "use Faraday's default adapter if none is specified" do c = ElastomerClient::Client.new adapter = Faraday::Adapter.lookup_middleware(Faraday.default_adapter)