Skip to content

Commit ba9cec3

Browse files
committed
[GEM] Decouples elasticsearch-transport from elasticsearch
1 parent f060f3a commit ba9cec3

File tree

12 files changed

+139
-174
lines changed

12 files changed

+139
-174
lines changed

elasticsearch-api/spec/rest_yaml_tests_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@
6161
end
6262
end
6363

64-
6564
# Skipped tests
66-
file = File.expand_path(__dir__ + '/skipped_tests.yml')
65+
file = File.expand_path("#{__dir__}/skipped_tests.yml")
6766
skipped_tests = YAML.load_file(file)
6867

6968
# The directory of rest api YAML files.

elasticsearch-transport/Gemfile

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@ source 'https://rubygems.org'
2020
# Specify your gem's dependencies in elasticsearch-transport.gemspec
2121
gemspec
2222

23-
if File.exist? File.expand_path('../../elasticsearch-api/elasticsearch-api.gemspec', __FILE__)
24-
gem 'elasticsearch-api', path: File.expand_path('../../elasticsearch-api', __FILE__), require: false
23+
if File.exist? File.expand_path('../elasticsearch-api/elasticsearch-api.gemspec', __dir__)
24+
gem 'elasticsearch-api', path: File.expand_path('../elasticsearch-api', __dir__), require: false
2525
end
2626

27-
if File.exist? File.expand_path('../../elasticsearch-extensions/elasticsearch-extensions.gemspec', __FILE__)
28-
gem 'elasticsearch-extensions', path: File.expand_path('../../elasticsearch-extensions', __FILE__), require: false
29-
end
30-
31-
if File.exist? File.expand_path('../../elasticsearch/elasticsearch.gemspec', __FILE__)
32-
gem 'elasticsearch', path: File.expand_path('../../elasticsearch', __FILE__), require: false
27+
if File.exist? File.expand_path('../elasticsearch/elasticsearch.gemspec', __dir__)
28+
gem 'elasticsearch', path: File.expand_path('../elasticsearch', __dir__), require: false
3329
end
3430

3531
group :development, :test do

elasticsearch-transport/elasticsearch-transport.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Gem::Specification.new do |s|
5050
s.add_development_dependency 'ansi'
5151
s.add_development_dependency 'bundler'
5252
s.add_development_dependency 'cane'
53-
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
53+
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
54+
s.add_development_dependency 'elasticsearch', ['>= 7', '< 8.0.0']
5455
s.add_development_dependency 'elasticsearch-extensions'
5556
s.add_development_dependency 'hashie'
5657
s.add_development_dependency 'httpclient'

elasticsearch-transport/lib/elasticsearch/transport.rb

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,23 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
require "uri"
19-
require "time"
20-
require "timeout"
21-
require "multi_json"
22-
require "faraday"
18+
require 'uri'
19+
require 'time'
20+
require 'timeout'
21+
require 'multi_json'
22+
require 'faraday'
2323

24-
require "elasticsearch/transport/transport/loggable"
25-
require "elasticsearch/transport/transport/serializer/multi_json"
26-
require "elasticsearch/transport/transport/sniffer"
27-
require "elasticsearch/transport/transport/response"
28-
require "elasticsearch/transport/transport/errors"
29-
require "elasticsearch/transport/transport/base"
30-
require "elasticsearch/transport/transport/connections/selector"
31-
require "elasticsearch/transport/transport/connections/connection"
32-
require "elasticsearch/transport/transport/connections/collection"
33-
require "elasticsearch/transport/transport/http/faraday"
34-
require "elasticsearch/transport/client"
35-
require "elasticsearch/transport/redacted"
24+
require 'elasticsearch/transport/transport/loggable'
25+
require 'elasticsearch/transport/transport/serializer/multi_json'
26+
require 'elasticsearch/transport/transport/sniffer'
27+
require 'elasticsearch/transport/transport/response'
28+
require 'elasticsearch/transport/transport/errors'
29+
require 'elasticsearch/transport/transport/base'
30+
require 'elasticsearch/transport/transport/connections/selector'
31+
require 'elasticsearch/transport/transport/connections/connection'
32+
require 'elasticsearch/transport/transport/connections/collection'
33+
require 'elasticsearch/transport/transport/http/faraday'
34+
require 'elasticsearch/transport/client'
35+
require 'elasticsearch/transport/redacted'
3636

37-
require "elasticsearch/transport/version"
38-
39-
module Elasticsearch
40-
module Client
41-
42-
# A convenience wrapper for {::Elasticsearch::Transport::Client#initialize}.
43-
#
44-
def new(arguments={}, &block)
45-
Elasticsearch::Transport::Client.new(arguments, &block)
46-
end
47-
extend self
48-
end
49-
end
37+
require 'elasticsearch/transport/version'

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
module Elasticsearch
2222
module Transport
23-
2423
# Handles communication with an Elasticsearch cluster.
2524
#
2625
# See {file:README.md README} for usage and code examples.

elasticsearch-transport/lib/elasticsearch/transport/transport/http/faraday.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
4545
headers
4646
end
4747

48-
response = connection.connection.run_request(method.downcase.to_sym,
49-
url,
50-
( body ? __convert_to_json(body) : nil ),
51-
headers)
48+
response = connection.connection.run_request(
49+
method.downcase.to_sym,
50+
url,
51+
(body ? __convert_to_json(body) : nil),
52+
headers
53+
)
5254

5355
Response.new response.status, decompress_response(response.body), response.headers
5456
end

elasticsearch-transport/spec/elasticsearch/transport/base_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
expect(logger).not_to receive(:error).with(/secret_password/)
3333

3434
expect {
35-
client.cluster.stats
35+
client.perform_request('GET', '_cluster/stats')
3636
}.to raise_exception(Faraday::ConnectionFailed)
3737
end
3838

3939
it 'replaces the password with the string \'REDACTED\'' do
4040
expect(logger).to receive(:error).with(/REDACTED/)
4141
expect {
42-
client.cluster.stats
42+
client.perform_request('GET', '_cluster/stats')
4343
}.to raise_exception(Faraday::ConnectionFailed)
4444
end
4545
end
@@ -133,7 +133,7 @@
133133
end
134134

135135
it 'raises an exception' do
136-
expect { client.info }.to raise_exception(Faraday::ConnectionFailed)
136+
expect { client.perform_request('GET', '/') }.to raise_exception(Faraday::ConnectionFailed)
137137
end
138138
end
139139

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
end
2525
end
2626

27-
it 'is aliased as Elasticsearch::Client' do
28-
expect(Elasticsearch::Client.new).to be_a(described_class)
29-
end
30-
3127
it 'has a default transport' do
3228
expect(client.transport).to be_a(Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS)
3329
end
@@ -1417,14 +1413,14 @@
14171413
let(:client) { described_class.new(host: hosts) }
14181414

14191415
it 'doesnae raise an ArgumentError' do
1420-
expect { client.search(opaque_id: 'no_error') }.not_to raise_error
1416+
expect { client.perform_request('GET', '_search', opaque_id: 'no_error') }.not_to raise_error
14211417
end
14221418

14231419
it 'uses X-Opaque-Id in the header' do
14241420
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1425-
expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
1421+
expect { client.perform_request('GET', '_search', {}, nil, opaque_id: 'opaque_id') }.not_to raise_error
14261422
expect(client).to have_received(:perform_request)
1427-
.with('GET', '_search', { opaque_id: 'opaque_id' }, nil, {})
1423+
.with('GET', '_search', {}, nil, { opaque_id: 'opaque_id' })
14281424
end
14291425
end
14301426
end
@@ -1501,7 +1497,7 @@
15011497

15021498
it 'performs the request with the header' do
15031499
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1504-
expect { client.search(headers: headers) }.not_to raise_error
1500+
expect { client.perform_request('GET', '_search', {}, nil, headers) }.not_to raise_error
15051501
expect(client).to have_received(:perform_request)
15061502
.with('GET', '_search', {}, nil, headers)
15071503
end
@@ -1515,7 +1511,7 @@
15151511
)
15161512
end
15171513
let(:instance_headers) { { set_in_instantiation: 'header value' } }
1518-
let(:param_headers) {{'user-agent' => 'My Ruby Tests', 'set-on-method-call' => 'header value'}}
1514+
let(:param_headers) { {'user-agent' => 'My Ruby Tests', 'set-on-method-call' => 'header value'} }
15191515

15201516
it 'performs the request with the header' do
15211517
expected_headers = client.transport.connections.connections.first.connection.headers.merge(param_headers)
@@ -1524,7 +1520,7 @@
15241520
.to receive(:run_request)
15251521
.with(:get, "http://#{hosts[0]}/_search", nil, expected_headers) { OpenStruct.new(body: '')}
15261522

1527-
client.search(headers: param_headers)
1523+
client.perform_request('GET', '_search', {}, nil, param_headers)
15281524
end
15291525
end
15301526
end
@@ -1561,7 +1557,6 @@
15611557
end
15621558

15631559
context 'when a request is made' do
1564-
15651560
let!(:response) do
15661561
client.perform_request('GET', '_cluster/health')
15671562
end
@@ -1572,9 +1567,7 @@
15721567
end
15731568

15741569
describe '#initialize' do
1575-
15761570
context 'when options are specified' do
1577-
15781571
let(:transport_options) do
15791572
{ headers: { accept: 'application/yaml', content_type: 'application/yaml' } }
15801573
end
@@ -1590,7 +1583,6 @@
15901583
end
15911584

15921585
context 'when a block is provided' do
1593-
15941586
let(:client) do
15951587
Elasticsearch::Client.new(host: ELASTICSEARCH_HOSTS.first, logger: logger) do |client|
15961588
client.headers['Accept'] = 'application/yaml'
@@ -1928,7 +1920,7 @@
19281920
end
19291921

19301922
let(:node_names) do
1931-
client.nodes.stats['nodes'].collect do |name, stats|
1923+
client.perform_request('GET', '_nodes/stats').body('nodes').collect do |name, stats|
19321924
stats['name']
19331925
end
19341926
end
@@ -1947,7 +1939,6 @@
19471939
end
19481940

19491941
context 'when patron is used as an adapter', unless: jruby? do
1950-
19511942
before do
19521943
require 'patron'
19531944
end

elasticsearch-transport/spec/elasticsearch/transport/meta_header_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
require 'spec_helper'
19+
require 'elasticsearch'
1920

2021
describe Elasticsearch::Transport::Client do
2122
context 'meta-header' do

elasticsearch-transport/spec/spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
SimpleCov.start { add_filter %r{^/test|spec/} }
2020
end
2121

22-
require 'elasticsearch'
2322
require 'elasticsearch-transport'
2423
require 'logger'
2524
require 'ansi/code'
@@ -61,7 +60,8 @@ def jruby?
6160
#
6261
# @since 7.0.0
6362
def node_names
64-
$node_names ||= default_client.nodes.stats['nodes'].collect do |name, stats|
63+
node_stats = default_client.perform_request('GET', '_nodes/stats').body
64+
$node_names ||= node_stats['nodes'].collect do |name, stats|
6565
stats['name']
6666
end
6767
end

0 commit comments

Comments
 (0)