Skip to content

Commit 949300d

Browse files
committed
[CLIENT] Adds API compatibility header
1 parent a8f14c5 commit 949300d

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,15 @@ def initialize(arguments={}, &block)
145145
@options[:http] ||= {}
146146

147147
set_api_key if (@api_key = @arguments[:api_key])
148+
set_compatibility_header if ENV['ELASTIC_CLIENT_APIVERSIONING']
148149

149150
@seeds = extract_cloud_creds(@arguments)
150151
@seeds ||= __extract_hosts(@arguments[:hosts] ||
151-
@arguments[:host] ||
152-
@arguments[:url] ||
153-
@arguments[:urls] ||
154-
ENV['ELASTICSEARCH_URL'] ||
155-
DEFAULT_HOST)
152+
@arguments[:host] ||
153+
@arguments[:url] ||
154+
@arguments[:urls] ||
155+
ENV['ELASTICSEARCH_URL'] ||
156+
DEFAULT_HOST)
156157

157158
@send_get_body_as = @arguments[:send_get_body_as] || 'GET'
158159
@opaque_id_prefix = @arguments[:opaque_id_prefix] || nil
@@ -200,6 +201,17 @@ def set_api_key
200201
@arguments.delete(:password)
201202
end
202203

204+
def set_compatibility_header
205+
return unless ['1', 'true'].include?(ENV['ELASTIC_CLIENT_APIVERSIONING'])
206+
207+
add_header(
208+
{
209+
'Accept' => 'application/vnd.elasticsearch+json;compatible-with=7',
210+
'Content-Type' => 'application/vnd.elasticsearch+json; compatible-with=7'
211+
}
212+
)
213+
end
214+
203215
def add_header(header)
204216
headers = @arguments[:transport_options]&.[](:headers) || {}
205217
headers.merge!(header)

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,46 @@
14281428
end
14291429
end
14301430

1431+
context 'when using the API Compatibility Header' do
1432+
it 'sets the API compatibility headers' do
1433+
ENV['ELASTIC_CLIENT_APIVERSIONING'] = 'true'
1434+
client = described_class.new(host: hosts)
1435+
headers = client.transport.connections.first.connection.headers
1436+
1437+
expect(headers['Content-Type']).to eq('application/vnd.elasticsearch+json; compatible-with=7')
1438+
expect(headers['Accept']).to eq('application/vnd.elasticsearch+json;compatible-with=7')
1439+
1440+
response = client.perform_request('GET', '/')
1441+
expect(response.headers['content-type']).to eq('application/json; charset=UTF-8')
1442+
1443+
ENV.delete('ELASTIC_CLIENT_APIVERSIONING')
1444+
end
1445+
1446+
it 'does not use API compatibility headers' do
1447+
val = ENV.delete('ELASTIC_CLIENT_APIVERSIONING')
1448+
client = described_class.new(host: hosts)
1449+
expect(client.transport.connections.first.connection.headers['Content-Type']).to eq('application/json')
1450+
ENV['ELASTIC_CLIENT_APIVERSIONING'] = val
1451+
end
1452+
1453+
it 'does not use API compatibility headers when it is set to unsupported values' do
1454+
val = ENV.delete('ELASTIC_CLIENT_APIVERSIONING')
1455+
1456+
ENV['ELASTIC_CLIENT_APIVERSIONING'] = 'test'
1457+
client = described_class.new(host: hosts)
1458+
expect(client.transport.connections.first.connection.headers['Content-Type']).to eq('application/json')
1459+
1460+
ENV['ELASTIC_CLIENT_APIVERSIONING'] = 'false'
1461+
client = described_class.new(host: hosts)
1462+
expect(client.transport.connections.first.connection.headers['Content-Type']).to eq('application/json')
1463+
1464+
ENV['ELASTIC_CLIENT_APIVERSIONING'] = '3'
1465+
client = described_class.new(host: hosts)
1466+
expect(client.transport.connections.first.connection.headers['Content-Type']).to eq('application/json')
1467+
ENV['ELASTIC_CLIENT_APIVERSIONING'] = val
1468+
end
1469+
end
1470+
14311471
context 'when Elasticsearch response includes a warning header' do
14321472
let(:client) do
14331473
Elasticsearch::Transport::Client.new(hosts: hosts)

0 commit comments

Comments
 (0)