Skip to content

Commit eae213d

Browse files
committed
Implements X-Opaque-Id
1 parent 0b7f5a8 commit eae213d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,16 @@ def initialize(arguments={}, &block)
146146
#
147147
def perform_request(method, path, params = {}, body = nil, headers = nil)
148148
method = @send_get_body_as if 'GET' == method && body
149+
if @opaque_id
150+
headers = {} if headers.nil?
151+
headers.merge!('X-Opaque-Id' => @opaque_id)
152+
@opaque_id = nil # Remove Opaque id after each request
153+
end
149154
transport.perform_request(method, path, params, body, headers)
150155
end
151156

157+
attr_accessor :opaque_id
158+
152159
private
153160

154161
def set_api_key

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,21 @@
652652
expect(request).to be(true)
653653
end
654654
end
655+
656+
context 'when x-opaque-id is set before calling a method' do
657+
let(:client) { described_class.new(host: hosts) }
658+
659+
it 'uses x-opaque-id on a request' do
660+
client.opaque_id = '12345'
661+
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('12345')
662+
end
663+
664+
it 'deletes x-opaque-id on a second request' do
665+
client.opaque_id = 'asdfg'
666+
expect(client.perform_request('GET', '/').headers['x-opaque-id']).to eq('asdfg')
667+
expect(client.perform_request('GET', '/').headers).not_to include('x-opaque-id')
668+
end
669+
end
655670
end
656671

657672
context 'when the client connects to Elasticsearch' do

0 commit comments

Comments
 (0)