Skip to content

Commit

Permalink
Merge pull request #283 from github/misalcedo-patch-1
Browse files Browse the repository at this point in the history
Allow a custom Faraday connection configuration block.
  • Loading branch information
misalcedo authored Nov 7, 2023
2 parents 78707f1 + 1764cd1 commit ec2474b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
5 changes: 4 additions & 1 deletion lib/elastomer_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/elastomer_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module ElastomerClient
VERSION = "5.1.0"
VERSION = "5.2.0"

def self.version
VERSION
Expand Down
12 changes: 12 additions & 0 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ec2474b

Please sign in to comment.