From ded7427e79709c42c96beee3271ca551318bd5a8 Mon Sep 17 00:00:00 2001 From: "Miguel D. Salcedo" Date: Tue, 7 Nov 2023 10:12:56 -0500 Subject: [PATCH 1/4] Allow a custom Faraday connection configuration block. --- lib/elastomer_client/client.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/elastomer_client/client.rb b/lib/elastomer_client/client.rb index db772ad6..a8ae53ae 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 From bba79d98b1251ab05a8e4909d48e75c963b3c1cd Mon Sep 17 00:00:00 2001 From: "Miguel D. Salcedo" Date: Tue, 7 Nov 2023 10:50:11 -0500 Subject: [PATCH 2/4] Update client.rb to remove trailing whitespace --- lib/elastomer_client/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elastomer_client/client.rb b/lib/elastomer_client/client.rb index a8ae53ae..df1225f8 100644 --- a/lib/elastomer_client/client.rb +++ b/lib/elastomer_client/client.rb @@ -157,7 +157,7 @@ def connection end @connection_block&.call(conn) - + if @adapter.is_a?(Array) conn.adapter(*@adapter) else From 898adb19de0956a3dad31c86b762f76b03c5b5f4 Mon Sep 17 00:00:00 2001 From: "Miguel D. Salcedo" Date: Tue, 7 Nov 2023 15:52:45 +0000 Subject: [PATCH 3/4] Bump the minor version --- CHANGELOG.md | 3 +++ lib/elastomer_client/version.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) 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/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 From 1764cd1a67b409a13c79c978d4cea2f49326f60a Mon Sep 17 00:00:00 2001 From: "Miguel D. Salcedo" Date: Tue, 7 Nov 2023 16:07:27 +0000 Subject: [PATCH 4/4] Add test for configuring the Faraday connection --- test/client_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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)