elasticsearch-transport 7.5.0 → 7.6.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 271a43455912981268da637666dddf8d9201752ba7b88787284dfc79039ac0e7
4
- data.tar.gz: 92f3307366179b9eb868b2224e94b87570ffee878a9013d9419f10271731aa6e
3
+ metadata.gz: 2b01c3835be0c7e8baced5fd1490e36b9a80f42cfb53a85beb255944d2aa4614
4
+ data.tar.gz: '08931111bab05fdd725afe95de0b40f643c96f30d82848d0899fe017ee999cf6'
5
5
  SHA512:
6
- metadata.gz: 1ead63661251f6b672a28f3bd759b86049f159225bebf2c053fbaaf5a03d3d974082131595980a2e44ef928fdcc640ce85e271398d5e3ac13cd0daaf5daf84a9
7
- data.tar.gz: 9e99b4a07e919000b06864741380293eb073b0a3ad71e227edf3a9a443b12a4ca26cbe573d33429748561059bec51b4a315345e52d15538e6dab0d84230b8aa2
6
+ metadata.gz: 8d92acc2c7d1a085001db7c976c8cbdb6787df88ea6162be50b5205b7846f3bf0e182b53f6766dfd43a0b61a0911291717b0579f98b351f02b8e5eb5d8bd04b3
7
+ data.tar.gz: 9bba1344253d072808f1468e5dc6fe0a9fe6815cf6a1f2831491c716ad9dd657b714a52c14432a85543a1e3e96a123d173061b74a622e051a35c26826204e464
data/Gemfile CHANGED
@@ -7,19 +7,23 @@ source 'https://rubygems.org'
7
7
  # Specify your gem's dependencies in elasticsearch-transport.gemspec
8
8
  gemspec
9
9
 
10
- if File.exist? File.expand_path("../../elasticsearch-api/elasticsearch-api.gemspec", __FILE__)
11
- gem 'elasticsearch-api', :path => File.expand_path("../../elasticsearch-api", __FILE__), :require => false
10
+ if File.exist? File.expand_path('../../elasticsearch-api/elasticsearch-api.gemspec', __FILE__)
11
+ gem 'elasticsearch-api', path: File.expand_path('../../elasticsearch-api', __FILE__), require: false
12
12
  end
13
13
 
14
- if File.exist? File.expand_path("../../elasticsearch-extensions/elasticsearch-extensions.gemspec", __FILE__)
15
- gem 'elasticsearch-extensions', :path => File.expand_path("../../elasticsearch-extensions", __FILE__), :require => false
14
+ if File.exist? File.expand_path('../../elasticsearch-extensions/elasticsearch-extensions.gemspec', __FILE__)
15
+ gem 'elasticsearch-extensions', path: File.expand_path('../../elasticsearch-extensions', __FILE__), require: false
16
16
  end
17
17
 
18
- if File.exist? File.expand_path("../../elasticsearch/elasticsearch.gemspec", __FILE__)
19
- gem 'elasticsearch', :path => File.expand_path("../../elasticsearch", __FILE__), :require => false
18
+ if File.exist? File.expand_path('../../elasticsearch/elasticsearch.gemspec', __FILE__)
19
+ gem 'elasticsearch', path: File.expand_path('../../elasticsearch', __FILE__), require: false
20
20
  end
21
21
 
22
22
  group :development do
23
23
  gem 'rspec'
24
- gem 'pry-nav'
24
+ if defined?(JRUBY_VERSION)
25
+ gem 'pry-nav'
26
+ else
27
+ gem 'pry-byebug'
28
+ end
25
29
  end
data/README.md CHANGED
@@ -28,12 +28,16 @@ Features overview:
28
28
  * Node reloading (based on cluster state) on errors or on demand
29
29
 
30
30
  For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections,
31
- such as [Typhoeus](https://github.com/typhoeus/typhoeus).
32
- Just require the library (`require 'typhoeus'; require 'typhoeus/adapters/faraday'`) in your code,
33
- and it will be automatically used; currently these libraries will be automatically detected and used:
34
- [Patron](https://github.com/toland/patron),
35
- [HTTPClient](https://rubygems.org/gems/httpclient) and
36
- [Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent).
31
+ such as [patron](https://github.com/toland/patron).
32
+ Just require the library (`require 'patron'`) in your code,
33
+ and it will be automatically used.
34
+
35
+ Currently these libraries will be automatically detected and used:
36
+ - [Patron](https://github.com/toland/patron)
37
+ - [HTTPClient](https://rubygems.org/gems/httpclient)
38
+ - [Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent)
39
+
40
+ **Note on [Typhoeus](https://github.com/typhoeus/typhoeus)**: Typhoeus is compatible and will be automatically detected too. However, the latest release (v1.3.1 at the moment of writing this) is not compatible with Faraday 1.0. [It still uses the deprecated `Faraday::Error` namespace](https://github.com/typhoeus/typhoeus/blob/v1.3.1/lib/typhoeus/adapters/faraday.rb#L100). If you want to use it with this gem, we suggest getting `master` from GitHub, since this has been fixed for v1.4.0. We'll update this if/when v1.4.0 is released.a
37
41
 
38
42
  For detailed information, see example configurations [below](#transport-implementations).
39
43
 
@@ -69,6 +73,22 @@ Full documentation is available at <http://rubydoc.info/gems/elasticsearch-trans
69
73
 
70
74
  ## Configuration
71
75
 
76
+ * [Setting Hosts](#setting-hosts)
77
+ * [Default port](#default-port)
78
+ * [Connect using an Elastic Cloud ID](#connect-using-an-elastic-cloud-id)
79
+ * [Authentication](#authentication)
80
+ * [Logging](#logging)
81
+ * [Identifying running tasks with X-Opaque-Id](#identifying-running-tasks-with-x-opaque-id)
82
+ * [Setting Timeouts](#setting-timeouts)
83
+ * [Randomizing Hosts](#randomizing-hosts)
84
+ * [Retrying on Failures](#retrying-on-failures)
85
+ * [Reloading Hosts](#reloading-hosts)
86
+ * [Connection Selector](#connection-selector)
87
+ * [Transport Implementations](#transport-implementations)
88
+ * [Serializer implementations](#serializer-implementations)
89
+ * [Exception Handling](#exception-handling)
90
+ * [Development and Community](#development-and-community)
91
+
72
92
  The client supports many configurations options for setting up and managing connections,
73
93
  configuring logging, customizing the transport library, etc.
74
94
 
@@ -144,6 +164,26 @@ use the `transport_options` option:
144
164
  Elasticsearch::Client.new url: 'https://username:password@example.com:9200',
145
165
  transport_options: { ssl: { ca_file: '/path/to/cacert.pem' } }
146
166
 
167
+ You can also use [**API Key authentication**](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html):
168
+
169
+ ``` ruby
170
+ Elasticsearch::Client.new(
171
+ host: host,
172
+ transport_options: transport_options,
173
+ api_key: credentials
174
+ )
175
+ ```
176
+
177
+ Where credentials is either the base64 encoding of `id` and `api_key` joined by a colon or a hash with the `id` and `api_key`:
178
+
179
+ ``` ruby
180
+ Elasticsearch::Client.new(
181
+ host: host,
182
+ transport_options: transport_options,
183
+ api_key: {id: 'my_id', api_key: 'my_api_key'}
184
+ )
185
+ ```
186
+
147
187
  ### Logging
148
188
 
149
189
  To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class),
@@ -173,6 +213,28 @@ You can pass the client any conforming logger implementation:
173
213
  log.level = :info
174
214
 
175
215
  client = Elasticsearch::Client.new logger: log
216
+ ### Identifying running tasks with X-Opaque-Id
217
+
218
+ The X-Opaque-Id header allows to track certain calls, or associate certain tasks with the client that started them ([more on the Elasticsearch docs](https://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html#_identifying_running_tasks)). To use this feature, you need to set an id for `opaque_id` on the client on each request. Example:
219
+
220
+ ```ruby
221
+ client = Elasticsearch::Client.new
222
+ client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
223
+ ```
224
+ The search request will include the following HTTP Header:
225
+ ```
226
+ X-Opaque-Id: 123456
227
+ ```
228
+
229
+ You can also set a prefix for X-Opaque-Id when initializing the client. This will be prepended to the id you set before each request if you're using X-Opaque-Id. Example:
230
+ ```ruby
231
+ client = Elasticsearch::Client.new(opaque_id_prefix: 'eu-west1')
232
+ client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
233
+ ```
234
+ The request will include the following HTTP Header:
235
+ ```
236
+ X-Opaque-Id: eu-west1_123456
237
+ ```
176
238
 
177
239
  ### Setting Timeouts
178
240
 
@@ -308,44 +370,35 @@ constructor, use the `transport_options` key:
308
370
 
309
371
  To configure the _Faraday_ instance directly, use a block:
310
372
 
311
- require 'typhoeus'
312
- require 'typhoeus/adapters/faraday'
373
+ require 'patron'
313
374
 
314
375
  client = Elasticsearch::Client.new(host: 'localhost', port: '9200') do |f|
315
376
  f.response :logger
316
- f.adapter :typhoeus
377
+ f.adapter :patron
317
378
  end
318
379
 
319
- You can use any standard Faraday middleware and plugins in the configuration block,
320
- for example sign the requests for the [AWS Elasticsearch service](https://aws.amazon.com/elasticsearch-service/):
321
-
322
- require 'faraday_middleware/aws_signers_v4'
323
-
324
- client = Elasticsearch::Client.new url: 'https://search-my-cluster-abc123....es.amazonaws.com' do |f|
325
- f.request :aws_signers_v4,
326
- credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET_ACCESS_KEY']),
327
- service_name: 'es',
328
- region: 'us-east-1'
329
- end
380
+ You can use any standard Faraday middleware and plugins in the configuration block, for example sign the requests for the [AWS Elasticsearch service](https://aws.amazon.com/elasticsearch-service/). See [the AWS documentation](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-request-signing.html#es-request-signing-ruby) for an example.
330
381
 
331
382
  You can also initialize the transport class yourself, and pass it to the client constructor
332
383
  as the `transport` argument:
333
384
 
334
- require 'typhoeus'
335
- require 'typhoeus/adapters/faraday'
385
+ ```ruby
386
+ require 'patron'
336
387
 
337
- transport_configuration = lambda do |f|
338
- f.response :logger
339
- f.adapter :typhoeus
340
- end
388
+ transport_configuration = lambda do |f|
389
+ f.response :logger
390
+ f.adapter :patron
391
+ end
341
392
 
342
- transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
343
- hosts: [ { host: 'localhost', port: '9200' } ],
344
- &transport_configuration
393
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
394
+ hosts: [ { host: 'localhost', port: '9200' } ],
395
+ &transport_configuration
396
+
397
+ # Pass the transport to the client
398
+ #
399
+ client = Elasticsearch::Client.new transport: transport
400
+ ```
345
401
 
346
- # Pass the transport to the client
347
- #
348
- client = Elasticsearch::Client.new transport: transport
349
402
 
350
403
  Instead of passing the transport to the constructor, you can inject it at run time:
351
404
 
@@ -8,74 +8,50 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
8
8
  require 'elasticsearch/transport/version'
9
9
 
10
10
  Gem::Specification.new do |s|
11
- s.name = "elasticsearch-transport"
11
+ s.name = 'elasticsearch-transport'
12
12
  s.version = Elasticsearch::Transport::VERSION
13
- s.authors = ["Karel Minarik"]
14
- s.email = ["karel.minarik@elasticsearch.org"]
15
- s.summary = "Ruby client for Elasticsearch."
16
- s.homepage = "https://github.com/elasticsearch/elasticsearch-ruby/tree/master/elasticsearch-transport"
17
- s.license = "Apache-2.0"
13
+ s.authors = ['Karel Minarik']
14
+ s.email = ['karel.minarik@elasticsearch.org']
15
+ s.summary = 'Ruby client for Elasticsearch.'
16
+ s.homepage = 'https://github.com/elasticsearch/elasticsearch-ruby/tree/master/elasticsearch-transport'
17
+ s.license = 'Apache-2.0'
18
18
 
19
19
  s.files = `git ls-files`.split($/)
20
20
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
21
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
22
- s.require_paths = ["lib"]
23
-
24
- s.extra_rdoc_files = [ "README.md", "LICENSE" ]
25
- s.rdoc_options = [ "--charset=UTF-8" ]
26
-
27
- s.required_ruby_version = '>= 1.9'
28
-
29
- s.add_dependency "multi_json"
30
- s.add_dependency "faraday", '>= 0.14', "< 1"
31
-
32
- if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
33
- s.add_dependency "system_timer"
34
- end
35
-
36
- s.add_development_dependency "bundler"
37
-
38
- if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
39
- s.add_development_dependency "rake", "~> 11.1"
40
- else
41
- s.add_development_dependency "rake", "< 11.0"
42
- end
43
-
44
- s.add_development_dependency "ansi"
45
- s.add_development_dependency "shoulda-context"
46
- s.add_development_dependency "mocha"
47
- s.add_development_dependency "yard"
48
- s.add_development_dependency "pry"
49
-
50
- # Gems for testing integrations
51
- s.add_development_dependency "curb" unless defined? JRUBY_VERSION
52
- s.add_development_dependency "patron" unless defined? JRUBY_VERSION
53
- s.add_development_dependency "typhoeus", '~> 0.6'
54
- s.add_development_dependency "net-http-persistent"
55
- s.add_development_dependency "httpclient"
56
- s.add_development_dependency "manticore", '~> 0.6' if defined? JRUBY_VERSION
57
- s.add_development_dependency "hashie"
58
-
59
- # Prevent unit test failures on Ruby 1.8
60
- if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
61
- s.add_development_dependency "test-unit", '~> 2'
62
- s.add_development_dependency "json", '~> 1.8'
63
- end
64
-
65
- if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
66
- s.add_development_dependency "minitest"
67
- s.add_development_dependency "minitest-reporters"
68
- s.add_development_dependency "elasticsearch-extensions"
69
- s.add_development_dependency "ruby-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius)
70
- s.add_development_dependency "require-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius)
71
- s.add_development_dependency "simplecov", '~> 0.17', '< 0.18'
72
- s.add_development_dependency "simplecov-rcov"
73
- s.add_development_dependency "cane"
74
- end
75
-
76
- if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
77
- s.add_development_dependency "test-unit", '~> 2'
78
- end
22
+ s.require_paths = ['lib']
23
+
24
+ s.extra_rdoc_files = [ 'README.md', 'LICENSE' ]
25
+ s.rdoc_options = [ '--charset=UTF-8' ]
26
+
27
+ s.required_ruby_version = '>= 2.4'
28
+
29
+ s.add_dependency 'multi_json'
30
+ s.add_dependency 'faraday', '~> 1'
31
+
32
+ s.add_development_dependency 'cane'
33
+ s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
34
+ s.add_development_dependency 'elasticsearch-extensions'
35
+ s.add_development_dependency 'minitest'
36
+ s.add_development_dependency 'minitest-reporters'
37
+ s.add_development_dependency 'rake', '~> 11.1'
38
+ s.add_development_dependency 'require-prof' unless defined?(JRUBY_VERSION) || defined?(Rubinius)
39
+ s.add_development_dependency 'ruby-prof' unless defined?(JRUBY_VERSION) || defined?(Rubinius)
40
+ s.add_development_dependency 'simplecov', '~> 0.17', '< 0.18'
41
+ s.add_development_dependency 'simplecov-rcov'
42
+ s.add_development_dependency 'ansi'
43
+ s.add_development_dependency 'hashie'
44
+ s.add_development_dependency 'httpclient'
45
+ s.add_development_dependency 'manticore', '~> 0.6' if defined? JRUBY_VERSION
46
+ s.add_development_dependency 'mocha'
47
+ s.add_development_dependency 'net-http-persistent'
48
+ s.add_development_dependency 'patron' unless defined? JRUBY_VERSION
49
+ s.add_development_dependency 'pry'
50
+ s.add_development_dependency 'shoulda-context'
51
+ s.add_development_dependency 'test-unit', '~> 2'
52
+ s.add_development_dependency 'typhoeus', '~> 0.6'
53
+ s.add_development_dependency 'yard'
54
+ s.add_development_dependency 'bundler'
79
55
 
80
56
  s.description = <<-DESC.gsub(/^ /, '')
81
57
  Ruby client for Elasticsearch. See the `elasticsearch` gem for full integration.
@@ -99,6 +99,11 @@ module Elasticsearch
99
99
  # The default is false. Responses will automatically be inflated if they are compressed.
100
100
  # If a custom transport object is used, it must handle the request compression and response inflation.
101
101
  #
102
+ # @option api_key [String, Hash] :api_key Use API Key Authentication, either the base64 encoding of `id` and `api_key`
103
+ # joined by a colon as a String, or a hash with the `id` and `api_key` values.
104
+ # @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client. This
105
+ # will be prepended to the id you set before each request if you're using X-Opaque-Id
106
+ #
102
107
  # @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
103
108
  #
104
109
  def initialize(arguments={}, &block)
@@ -114,6 +119,8 @@ module Elasticsearch
114
119
  @arguments[:http] ||= {}
115
120
  @options[:http] ||= {}
116
121
 
122
+ set_api_key if (@api_key = @arguments[:api_key])
123
+
117
124
  @seeds = extract_cloud_creds(@arguments)
118
125
  @seeds ||= __extract_hosts(@arguments[:hosts] ||
119
126
  @arguments[:host] ||
@@ -123,9 +130,10 @@ module Elasticsearch
123
130
  DEFAULT_HOST)
124
131
 
125
132
  @send_get_body_as = @arguments[:send_get_body_as] || 'GET'
133
+ @opaque_id_prefix = @arguments[:opaque_id_prefix] || nil
126
134
 
127
135
  if @arguments[:request_timeout]
128
- @arguments[:transport_options][:request] = { :timeout => @arguments[:request_timeout] }
136
+ @arguments[:transport_options][:request] = { timeout: @arguments[:request_timeout] }
129
137
  end
130
138
 
131
139
  if @arguments[:transport]
@@ -133,27 +141,41 @@ module Elasticsearch
133
141
  else
134
142
  transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
135
143
  if transport_class == Transport::HTTP::Faraday
136
- @transport = transport_class.new(:hosts => @seeds, :options => @arguments) do |faraday|
137
- block.call faraday if block
138
- unless (h = faraday.builder.handlers.last) && h.name.start_with?("Faraday::Adapter")
139
- faraday.adapter(@arguments[:adapter] || __auto_detect_adapter)
140
- end
144
+ @transport = transport_class.new(hosts: @seeds, options: @arguments) do |faraday|
145
+ faraday.adapter(@arguments[:adapter] || __auto_detect_adapter)
146
+ block&.call faraday
141
147
  end
142
148
  else
143
- @transport = transport_class.new(:hosts => @seeds, :options => @arguments)
149
+ @transport = transport_class.new(hosts: @seeds, options: @arguments)
144
150
  end
145
151
  end
146
152
  end
147
153
 
148
154
  # Performs a request through delegation to {#transport}.
149
155
  #
150
- def perform_request(method, path, params={}, body=nil, headers=nil)
156
+ def perform_request(method, path, params = {}, body = nil, headers = nil)
151
157
  method = @send_get_body_as if 'GET' == method && body
158
+ if (opaque_id = params.delete(:opaque_id))
159
+ headers = {} if headers.nil?
160
+ opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
161
+ headers.merge!('X-Opaque-Id' => opaque_id)
162
+ end
152
163
  transport.perform_request(method, path, params, body, headers)
153
164
  end
154
165
 
155
166
  private
156
167
 
168
+ def set_api_key
169
+ @api_key = __encode(@api_key) if @api_key.is_a? Hash
170
+ headers = @arguments[:transport_options]&.[](:headers) || {}
171
+ headers.merge!('Authorization' => "ApiKey #{@api_key}")
172
+ @arguments[:transport_options].merge!(
173
+ headers: headers
174
+ )
175
+ @arguments.delete(:user)
176
+ @arguments.delete(:password)
177
+ end
178
+
157
179
  def extract_cloud_creds(arguments)
158
180
  return unless arguments[:cloud_id]
159
181
  name = arguments[:cloud_id].split(':')[0]
@@ -225,8 +247,14 @@ module Elasticsearch
225
247
  raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
226
248
  end
227
249
 
228
- @options[:http][:user] ||= host_parts[:user]
229
- @options[:http][:password] ||= host_parts[:password]
250
+ if @api_key
251
+ # Remove Basic Auth if using API KEY
252
+ host_parts.delete(:user)
253
+ host_parts.delete(:password)
254
+ else
255
+ @options[:http][:user] ||= host_parts[:user]
256
+ @options[:http][:password] ||= host_parts[:password]
257
+ end
230
258
 
231
259
  host_parts[:port] = host_parts[:port].to_i if host_parts[:port]
232
260
  host_parts[:path].chomp!('/') if host_parts[:path]
@@ -255,6 +283,13 @@ module Elasticsearch
255
283
  ::Faraday.default_adapter
256
284
  end
257
285
  end
286
+
287
+ # Encode credentials for the Authorization Header
288
+ # Credentials is the base64 encoding of id and api_key joined by a colon
289
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
290
+ def __encode(api_key)
291
+ Base64.strict_encode64([api_key[:id], api_key[:api_key]].join(':'))
292
+ end
258
293
  end
259
294
  end
260
295
  end
@@ -48,7 +48,7 @@ module Elasticsearch
48
48
  # @return [Array]
49
49
  #
50
50
  def host_unreachable_exceptions
51
- [::Faraday::Error::ConnectionFailed, ::Faraday::Error::TimeoutError]
51
+ [::Faraday::ConnectionFailed, ::Faraday::TimeoutError]
52
52
  end
53
53
 
54
54
  private
@@ -4,6 +4,6 @@
4
4
 
5
5
  module Elasticsearch
6
6
  module Transport
7
- VERSION = "7.5.0"
7
+ VERSION = "7.6.0.pre"
8
8
  end
9
9
  end
@@ -49,7 +49,6 @@ describe Elasticsearch::Transport::Client do
49
49
  end
50
50
 
51
51
  context 'when a User-Agent header is specified as client option' do
52
-
53
52
  let(:client) do
54
53
  described_class.new(transport_options: { headers: { 'User-Agent' => 'testing' } })
55
54
  end
@@ -59,6 +58,49 @@ describe Elasticsearch::Transport::Client do
59
58
  end
60
59
  end
61
60
 
61
+ context 'when an encoded api_key is provided' do
62
+ let(:client) do
63
+ described_class.new(api_key: 'an_api_key')
64
+ end
65
+ let(:authorization_header) do
66
+ client.transport.connections.first.connection.headers['Authorization']
67
+ end
68
+
69
+ it 'Adds the ApiKey header to the connection' do
70
+ expect(authorization_header).to eq('ApiKey an_api_key')
71
+ end
72
+ end
73
+
74
+ context 'when an un-encoded api_key is provided' do
75
+ let(:client) do
76
+ described_class.new(api_key: { id: 'my_id', api_key: 'my_api_key' })
77
+ end
78
+ let(:authorization_header) do
79
+ client.transport.connections.first.connection.headers['Authorization']
80
+ end
81
+
82
+ it 'Adds the ApiKey header to the connection' do
83
+ expect(authorization_header).to eq("ApiKey #{Base64.strict_encode64('my_id:my_api_key')}")
84
+ end
85
+ end
86
+
87
+ context 'when basic auth and api_key are provided' do
88
+ let(:client) do
89
+ described_class.new(
90
+ api_key: { id: 'my_id', api_key: 'my_api_key' },
91
+ host: 'http://elastic:password@localhost:9200'
92
+ )
93
+ end
94
+ let(:authorization_header) do
95
+ client.transport.connections.first.connection.headers['Authorization']
96
+ end
97
+
98
+ it 'removes basic auth credentials' do
99
+ expect(authorization_header).not_to match(/^Basic/)
100
+ expect(authorization_header).to match(/^ApiKey/)
101
+ end
102
+ end
103
+
62
104
  context 'when a user-agent header is specified as client option in lower-case' do
63
105
 
64
106
  let(:client) do
@@ -176,45 +218,43 @@ describe Elasticsearch::Transport::Client do
176
218
  end
177
219
 
178
220
  describe 'adapter' do
179
-
180
221
  context 'when no adapter is specified' do
181
-
182
222
  let(:adapter) do
183
- client.transport.connections.all.first.connection.builder.handlers
223
+ client.transport.connections.all.first.connection.builder.adapter
184
224
  end
185
225
 
186
226
  it 'uses Faraday NetHttp' do
187
- expect(adapter).to include(Faraday::Adapter::NetHttp)
227
+ expect(adapter).to eq Faraday::Adapter::NetHttp
188
228
  end
189
229
  end
190
230
 
191
231
  context 'when the adapter is specified' do
192
232
 
193
233
  let(:adapter) do
194
- client.transport.connections.all.first.connection.builder.handlers
234
+ client.transport.connections.all.first.connection.builder.adapter
195
235
  end
196
236
 
197
237
  let(:client) do
198
- described_class.new(adapter: :typhoeus)
238
+ described_class.new(adapter: :patron)
199
239
  end
200
240
 
201
241
  it 'uses Faraday with the adapter' do
202
- expect(adapter).to include(Faraday::Adapter::Typhoeus)
242
+ expect(adapter).to eq Faraday::Adapter::Patron
203
243
  end
204
244
  end
205
245
 
206
246
  context 'when the adapter is specified as a string key' do
207
247
 
208
248
  let(:adapter) do
209
- client.transport.connections.all.first.connection.builder.handlers
249
+ client.transport.connections.all.first.connection.builder.adapter
210
250
  end
211
251
 
212
252
  let(:client) do
213
- described_class.new('adapter' => :typhoeus)
253
+ described_class.new('adapter' => :patron)
214
254
  end
215
255
 
216
256
  it 'uses Faraday with the adapter' do
217
- expect(adapter).to include(Faraday::Adapter::Typhoeus)
257
+ expect(adapter).to eq Faraday::Adapter::Patron
218
258
  end
219
259
  end
220
260
 
@@ -226,11 +266,11 @@ describe Elasticsearch::Transport::Client do
226
266
  end
227
267
 
228
268
  let(:adapter) do
229
- client.transport.connections.all.first.connection.builder.handlers
269
+ client.transport.connections.all.first.connection.builder.adapter
230
270
  end
231
271
 
232
272
  it 'uses the detected adapter' do
233
- expect(adapter).to include(Faraday::Adapter::Patron)
273
+ expect(adapter).to eq Faraday::Adapter::Patron
234
274
  end
235
275
  end
236
276
 
@@ -238,17 +278,21 @@ describe Elasticsearch::Transport::Client do
238
278
 
239
279
  let(:client) do
240
280
  described_class.new do |faraday|
241
- faraday.adapter :typhoeus
281
+ faraday.adapter :patron
242
282
  faraday.response :logger
243
283
  end
244
284
  end
245
285
 
286
+ let(:adapter) do
287
+ client.transport.connections.all.first.connection.builder.adapter
288
+ end
289
+
246
290
  let(:handlers) do
247
291
  client.transport.connections.all.first.connection.builder.handlers
248
292
  end
249
293
 
250
294
  it 'sets the adapter' do
251
- expect(handlers).to include(Faraday::Adapter::Typhoeus)
295
+ expect(adapter).to eq Faraday::Adapter::Patron
252
296
  end
253
297
 
254
298
  it 'sets the logger' do
@@ -1055,10 +1099,43 @@ describe Elasticsearch::Transport::Client do
1055
1099
  expect(request).to be(true)
1056
1100
  end
1057
1101
  end
1102
+
1103
+ context 'when x-opaque-id is set' do
1104
+ let(:client) { described_class.new(host: hosts) }
1105
+
1106
+ it 'uses x-opaque-id on a request' do
1107
+ expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq('12345')
1108
+ end
1109
+ end
1110
+
1111
+ context 'when an x-opaque-id prefix is set on initialization' do
1112
+ let(:prefix) { 'elastic_cloud' }
1113
+ let(:client) do
1114
+ described_class.new(host: hosts, opaque_id_prefix: prefix)
1115
+ end
1116
+
1117
+ it 'uses x-opaque-id on a request' do
1118
+ expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq("#{prefix}12345")
1119
+ end
1120
+
1121
+ context 'when using an API call' do
1122
+ let(:client) { described_class.new(host: hosts) }
1123
+
1124
+ it 'doesnae raise an ArgumentError' do
1125
+ expect { client.search(opaque_id: 'no_error') }.not_to raise_error
1126
+ end
1127
+
1128
+ it 'uses X-Opaque-Id in the header' do
1129
+ allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1130
+ expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
1131
+ expect(client).to have_received(:perform_request)
1132
+ .with('GET', '_search', { opaque_id: 'opaque_id' }, nil)
1133
+ end
1134
+ end
1135
+ end
1058
1136
  end
1059
1137
 
1060
1138
  context 'when the client connects to Elasticsearch' do
1061
-
1062
1139
  let(:logger) do
1063
1140
  Logger.new(STDERR).tap do |logger|
1064
1141
  logger.formatter = proc do |severity, datetime, progname, msg|
@@ -1136,15 +1213,14 @@ describe Elasticsearch::Transport::Client do
1136
1213
  end
1137
1214
 
1138
1215
  context 'when the Faraday adapter is set in the block' do
1139
-
1140
1216
  let(:client) do
1141
1217
  Elasticsearch::Client.new(host: ELASTICSEARCH_HOSTS.first, logger: logger) do |client|
1142
1218
  client.adapter(:net_http_persistent)
1143
1219
  end
1144
1220
  end
1145
1221
 
1146
- let(:connection_handler) do
1147
- client.transport.connections.first.connection.builder.handlers.first
1222
+ let(:handler_name) do
1223
+ client.transport.connections.first.connection.builder.adapter.name
1148
1224
  end
1149
1225
 
1150
1226
  let(:response) do
@@ -1152,7 +1228,7 @@ describe Elasticsearch::Transport::Client do
1152
1228
  end
1153
1229
 
1154
1230
  it 'sets the adapter' do
1155
- expect(connection_handler.name).to eq('Faraday::Adapter::NetHttpPersistent')
1231
+ expect(handler_name).to eq('Faraday::Adapter::NetHttpPersistent')
1156
1232
  end
1157
1233
 
1158
1234
  it 'uses the adapter to connect' do
@@ -1202,7 +1278,7 @@ describe Elasticsearch::Transport::Client do
1202
1278
  expect(client.perform_request('GET', '_nodes/_local'))
1203
1279
  expect {
1204
1280
  client.perform_request('GET', '_nodes/_local')
1205
- }.to raise_exception(Faraday::Error::ConnectionFailed)
1281
+ }.to raise_exception(Faraday::ConnectionFailed)
1206
1282
  end
1207
1283
  end
1208
1284
 
@@ -1486,12 +1562,12 @@ describe Elasticsearch::Transport::Client do
1486
1562
  { adapter: :patron }
1487
1563
  end
1488
1564
 
1489
- let(:connection_handler) do
1490
- client.transport.connections.first.connection.builder.handlers.first
1565
+ let(:adapter) do
1566
+ client.transport.connections.first.connection.builder.adapter
1491
1567
  end
1492
1568
 
1493
1569
  it 'uses the patron connection handler' do
1494
- expect(connection_handler).to eq('Faraday::Adapter::Patron')
1570
+ expect(adapter).to eq('Faraday::Adapter::Patron')
1495
1571
  end
1496
1572
 
1497
1573
  it 'keeps connections open' do
@@ -3,10 +3,11 @@ require 'elasticsearch-transport'
3
3
  require 'logger'
4
4
  require 'ansi/code'
5
5
  require 'hashie/mash'
6
- require 'pry-nav'
7
6
  if defined?(JRUBY_VERSION)
8
7
  require 'elasticsearch/transport/transport/http/manticore'
8
+ require 'pry-nav'
9
9
  else
10
+ require 'pry-byebug'
10
11
  require 'elasticsearch/transport/transport/http/curb'
11
12
  require 'curb'
12
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.5.0
4
+ version: 7.6.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-26 00:00:00.000000000 Z
11
+ date: 2020-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -28,24 +28,18 @@ dependencies:
28
28
  name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0.14'
34
- - - "<"
31
+ - - "~>"
35
32
  - !ruby/object:Gem::Version
36
33
  version: '1'
37
34
  type: :runtime
38
35
  prerelease: false
39
36
  version_requirements: !ruby/object:Gem::Requirement
40
37
  requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: '0.14'
44
- - - "<"
38
+ - - "~>"
45
39
  - !ruby/object:Gem::Version
46
40
  version: '1'
47
41
  - !ruby/object:Gem::Dependency
48
- name: bundler
42
+ name: cane
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
45
  - - ">="
@@ -59,21 +53,21 @@ dependencies:
59
53
  - !ruby/object:Gem::Version
60
54
  version: '0'
61
55
  - !ruby/object:Gem::Dependency
62
- name: rake
56
+ name: curb
63
57
  requirement: !ruby/object:Gem::Requirement
64
58
  requirements:
65
- - - "~>"
59
+ - - ">="
66
60
  - !ruby/object:Gem::Version
67
- version: '11.1'
61
+ version: '0'
68
62
  type: :development
69
63
  prerelease: false
70
64
  version_requirements: !ruby/object:Gem::Requirement
71
65
  requirements:
72
- - - "~>"
66
+ - - ">="
73
67
  - !ruby/object:Gem::Version
74
- version: '11.1'
68
+ version: '0'
75
69
  - !ruby/object:Gem::Dependency
76
- name: ansi
70
+ name: elasticsearch-extensions
77
71
  requirement: !ruby/object:Gem::Requirement
78
72
  requirements:
79
73
  - - ">="
@@ -87,7 +81,7 @@ dependencies:
87
81
  - !ruby/object:Gem::Version
88
82
  version: '0'
89
83
  - !ruby/object:Gem::Dependency
90
- name: shoulda-context
84
+ name: minitest
91
85
  requirement: !ruby/object:Gem::Requirement
92
86
  requirements:
93
87
  - - ">="
@@ -101,7 +95,7 @@ dependencies:
101
95
  - !ruby/object:Gem::Version
102
96
  version: '0'
103
97
  - !ruby/object:Gem::Dependency
104
- name: mocha
98
+ name: minitest-reporters
105
99
  requirement: !ruby/object:Gem::Requirement
106
100
  requirements:
107
101
  - - ">="
@@ -115,21 +109,21 @@ dependencies:
115
109
  - !ruby/object:Gem::Version
116
110
  version: '0'
117
111
  - !ruby/object:Gem::Dependency
118
- name: yard
112
+ name: rake
119
113
  requirement: !ruby/object:Gem::Requirement
120
114
  requirements:
121
- - - ">="
115
+ - - "~>"
122
116
  - !ruby/object:Gem::Version
123
- version: '0'
117
+ version: '11.1'
124
118
  type: :development
125
119
  prerelease: false
126
120
  version_requirements: !ruby/object:Gem::Requirement
127
121
  requirements:
128
- - - ">="
122
+ - - "~>"
129
123
  - !ruby/object:Gem::Version
130
- version: '0'
124
+ version: '11.1'
131
125
  - !ruby/object:Gem::Dependency
132
- name: pry
126
+ name: require-prof
133
127
  requirement: !ruby/object:Gem::Requirement
134
128
  requirements:
135
129
  - - ">="
@@ -143,7 +137,7 @@ dependencies:
143
137
  - !ruby/object:Gem::Version
144
138
  version: '0'
145
139
  - !ruby/object:Gem::Dependency
146
- name: curb
140
+ name: ruby-prof
147
141
  requirement: !ruby/object:Gem::Requirement
148
142
  requirements:
149
143
  - - ">="
@@ -157,35 +151,41 @@ dependencies:
157
151
  - !ruby/object:Gem::Version
158
152
  version: '0'
159
153
  - !ruby/object:Gem::Dependency
160
- name: patron
154
+ name: simplecov
161
155
  requirement: !ruby/object:Gem::Requirement
162
156
  requirements:
163
- - - ">="
157
+ - - "~>"
164
158
  - !ruby/object:Gem::Version
165
- version: '0'
159
+ version: '0.17'
160
+ - - "<"
161
+ - !ruby/object:Gem::Version
162
+ version: '0.18'
166
163
  type: :development
167
164
  prerelease: false
168
165
  version_requirements: !ruby/object:Gem::Requirement
169
166
  requirements:
170
- - - ">="
167
+ - - "~>"
171
168
  - !ruby/object:Gem::Version
172
- version: '0'
169
+ version: '0.17'
170
+ - - "<"
171
+ - !ruby/object:Gem::Version
172
+ version: '0.18'
173
173
  - !ruby/object:Gem::Dependency
174
- name: typhoeus
174
+ name: simplecov-rcov
175
175
  requirement: !ruby/object:Gem::Requirement
176
176
  requirements:
177
- - - "~>"
177
+ - - ">="
178
178
  - !ruby/object:Gem::Version
179
- version: '0.6'
179
+ version: '0'
180
180
  type: :development
181
181
  prerelease: false
182
182
  version_requirements: !ruby/object:Gem::Requirement
183
183
  requirements:
184
- - - "~>"
184
+ - - ">="
185
185
  - !ruby/object:Gem::Version
186
- version: '0.6'
186
+ version: '0'
187
187
  - !ruby/object:Gem::Dependency
188
- name: net-http-persistent
188
+ name: ansi
189
189
  requirement: !ruby/object:Gem::Requirement
190
190
  requirements:
191
191
  - - ">="
@@ -199,7 +199,7 @@ dependencies:
199
199
  - !ruby/object:Gem::Version
200
200
  version: '0'
201
201
  - !ruby/object:Gem::Dependency
202
- name: httpclient
202
+ name: hashie
203
203
  requirement: !ruby/object:Gem::Requirement
204
204
  requirements:
205
205
  - - ">="
@@ -213,7 +213,7 @@ dependencies:
213
213
  - !ruby/object:Gem::Version
214
214
  version: '0'
215
215
  - !ruby/object:Gem::Dependency
216
- name: hashie
216
+ name: httpclient
217
217
  requirement: !ruby/object:Gem::Requirement
218
218
  requirements:
219
219
  - - ">="
@@ -227,7 +227,7 @@ dependencies:
227
227
  - !ruby/object:Gem::Version
228
228
  version: '0'
229
229
  - !ruby/object:Gem::Dependency
230
- name: minitest
230
+ name: mocha
231
231
  requirement: !ruby/object:Gem::Requirement
232
232
  requirements:
233
233
  - - ">="
@@ -241,7 +241,7 @@ dependencies:
241
241
  - !ruby/object:Gem::Version
242
242
  version: '0'
243
243
  - !ruby/object:Gem::Dependency
244
- name: minitest-reporters
244
+ name: net-http-persistent
245
245
  requirement: !ruby/object:Gem::Requirement
246
246
  requirements:
247
247
  - - ">="
@@ -255,7 +255,7 @@ dependencies:
255
255
  - !ruby/object:Gem::Version
256
256
  version: '0'
257
257
  - !ruby/object:Gem::Dependency
258
- name: elasticsearch-extensions
258
+ name: patron
259
259
  requirement: !ruby/object:Gem::Requirement
260
260
  requirements:
261
261
  - - ">="
@@ -269,7 +269,7 @@ dependencies:
269
269
  - !ruby/object:Gem::Version
270
270
  version: '0'
271
271
  - !ruby/object:Gem::Dependency
272
- name: ruby-prof
272
+ name: pry
273
273
  requirement: !ruby/object:Gem::Requirement
274
274
  requirements:
275
275
  - - ">="
@@ -283,7 +283,7 @@ dependencies:
283
283
  - !ruby/object:Gem::Version
284
284
  version: '0'
285
285
  - !ruby/object:Gem::Dependency
286
- name: require-prof
286
+ name: shoulda-context
287
287
  requirement: !ruby/object:Gem::Requirement
288
288
  requirements:
289
289
  - - ">="
@@ -297,41 +297,35 @@ dependencies:
297
297
  - !ruby/object:Gem::Version
298
298
  version: '0'
299
299
  - !ruby/object:Gem::Dependency
300
- name: simplecov
300
+ name: test-unit
301
301
  requirement: !ruby/object:Gem::Requirement
302
302
  requirements:
303
303
  - - "~>"
304
304
  - !ruby/object:Gem::Version
305
- version: '0.17'
306
- - - "<"
307
- - !ruby/object:Gem::Version
308
- version: '0.18'
305
+ version: '2'
309
306
  type: :development
310
307
  prerelease: false
311
308
  version_requirements: !ruby/object:Gem::Requirement
312
309
  requirements:
313
310
  - - "~>"
314
311
  - !ruby/object:Gem::Version
315
- version: '0.17'
316
- - - "<"
317
- - !ruby/object:Gem::Version
318
- version: '0.18'
312
+ version: '2'
319
313
  - !ruby/object:Gem::Dependency
320
- name: simplecov-rcov
314
+ name: typhoeus
321
315
  requirement: !ruby/object:Gem::Requirement
322
316
  requirements:
323
- - - ">="
317
+ - - "~>"
324
318
  - !ruby/object:Gem::Version
325
- version: '0'
319
+ version: '0.6'
326
320
  type: :development
327
321
  prerelease: false
328
322
  version_requirements: !ruby/object:Gem::Requirement
329
323
  requirements:
330
- - - ">="
324
+ - - "~>"
331
325
  - !ruby/object:Gem::Version
332
- version: '0'
326
+ version: '0.6'
333
327
  - !ruby/object:Gem::Dependency
334
- name: cane
328
+ name: yard
335
329
  requirement: !ruby/object:Gem::Requirement
336
330
  requirements:
337
331
  - - ">="
@@ -345,19 +339,19 @@ dependencies:
345
339
  - !ruby/object:Gem::Version
346
340
  version: '0'
347
341
  - !ruby/object:Gem::Dependency
348
- name: test-unit
342
+ name: bundler
349
343
  requirement: !ruby/object:Gem::Requirement
350
344
  requirements:
351
- - - "~>"
345
+ - - ">="
352
346
  - !ruby/object:Gem::Version
353
- version: '2'
347
+ version: '0'
354
348
  type: :development
355
349
  prerelease: false
356
350
  version_requirements: !ruby/object:Gem::Requirement
357
351
  requirements:
358
- - - "~>"
352
+ - - ">="
359
353
  - !ruby/object:Gem::Version
360
- version: '2'
354
+ version: '0'
361
355
  description: 'Ruby client for Elasticsearch. See the `elasticsearch` gem for full
362
356
  integration.
363
357
 
@@ -422,14 +416,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
422
416
  requirements:
423
417
  - - ">="
424
418
  - !ruby/object:Gem::Version
425
- version: '1.9'
419
+ version: '2.4'
426
420
  required_rubygems_version: !ruby/object:Gem::Requirement
427
421
  requirements:
428
- - - ">="
422
+ - - ">"
429
423
  - !ruby/object:Gem::Version
430
- version: '0'
424
+ version: 1.3.1
431
425
  requirements: []
432
- rubygems_version: 3.0.6
426
+ rubygems_version: 3.1.2
433
427
  signing_key:
434
428
  specification_version: 4
435
429
  summary: Ruby client for Elasticsearch.