Skip to content

Commit f9bafc4

Browse files
committed
[GEM] Extracts opaque-id from elasticsearch-transport into elasticsearch
1 parent afb53ae commit f9bafc4

File tree

6 files changed

+66
-46
lines changed

6 files changed

+66
-46
lines changed

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

-9
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ class Client
113113
# The default is false. Responses will automatically be inflated if they are compressed.
114114
# If a custom transport object is used, it must handle the request compression and response inflation.
115115
#
116-
# @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client.
117-
# This will be prepended to the id you set before each request
118-
# if you're using X-Opaque-Id
119116
# @option enable_meta_header [Boolean] :enable_meta_header Enable sending the meta data header to Cloud.
120117
# (Default: true)
121118
#
@@ -143,7 +140,6 @@ def initialize(arguments = {}, &block)
143140
DEFAULT_HOST)
144141

145142
@send_get_body_as = @arguments[:send_get_body_as] || 'GET'
146-
@opaque_id_prefix = @arguments[:opaque_id_prefix] || nil
147143

148144
if @arguments[:request_timeout]
149145
@arguments[:transport_options][:request] = { timeout: @arguments[:request_timeout] }
@@ -170,11 +166,6 @@ def initialize(arguments = {}, &block)
170166
#
171167
def perform_request(method, path, params = {}, body = nil, headers = nil)
172168
method = @send_get_body_as if 'GET' == method && body
173-
if (opaque_id = params.delete(:opaque_id))
174-
headers = {} if headers.nil?
175-
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
176-
headers.merge!('X-Opaque-Id' => opaque_id)
177-
end
178169
transport.perform_request(method, path, params, body, headers)
179170
end
180171

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

-34
Original file line numberDiff line numberDiff line change
@@ -1196,40 +1196,6 @@
11961196
end
11971197
end
11981198

1199-
context 'when x-opaque-id is set' do
1200-
let(:client) { described_class.new(host: hosts) }
1201-
1202-
it 'uses x-opaque-id on a request' do
1203-
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq('12345')
1204-
end
1205-
end
1206-
1207-
context 'when an x-opaque-id prefix is set on initialization' do
1208-
let(:prefix) { 'elastic_cloud' }
1209-
let(:client) do
1210-
described_class.new(host: hosts, opaque_id_prefix: prefix)
1211-
end
1212-
1213-
it 'uses x-opaque-id on a request' do
1214-
expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq("#{prefix}12345")
1215-
end
1216-
1217-
context 'when using an API call' do
1218-
let(:client) { described_class.new(host: hosts) }
1219-
1220-
it 'doesnae raise an ArgumentError' do
1221-
expect { client.search(opaque_id: 'no_error') }.not_to raise_error
1222-
end
1223-
1224-
it 'uses X-Opaque-Id in the header' do
1225-
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1226-
expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
1227-
expect(client).to have_received(:perform_request)
1228-
.with('GET', '_search', { opaque_id: 'opaque_id' }, nil, {})
1229-
end
1230-
end
1231-
end
1232-
12331199
context 'when Elasticsearch response includes a warning header' do
12341200
let(:client) do
12351201
Elasticsearch::Transport::Client.new(hosts: hosts)

elasticsearch/lib/elasticsearch.rb

+13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ class Client
3434
# @option arguments [String] :cloud_id - The Cloud ID to connect to Elastic Cloud
3535
# @option api_key [String, Hash] :api_key Use API Key Authentication, either the base64 encoding of `id` and `api_key`
3636
# joined by a colon as a String, or a hash with the `id` and `api_key` values.
37+
# @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client.
38+
# This will be prepended to the id you set before each request
39+
# if you're using X-Opaque-Id
3740
def initialize(arguments = {}, &block)
41+
@opaque_id_prefix = arguments[:opaque_id_prefix] || nil
3842
api_key(arguments) if arguments[:api_key]
3943
if arguments[:cloud_id]
4044
arguments[:hosts] = setup_cloud_host(
@@ -50,6 +54,15 @@ def initialize(arguments = {}, &block)
5054
def method_missing(name, *args, &block)
5155
if methods.include?(name)
5256
super
57+
elsif name == :perform_request
58+
# The signature for perform_request is:
59+
# method, path, params, body, headers
60+
if (opaque_id = args[2]&.delete(:opaque_id))
61+
headers = args[4] || {}
62+
opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
63+
args[4] = headers.merge('X-Opaque-Id' => opaque_id)
64+
end
65+
@transport.send(name, *args, &block)
5366
else
5467
@transport.send(name, *args, &block)
5568
end

elasticsearch/spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@
2121
RSpec.configure do |config|
2222
config.formatter = :documentation
2323
end
24+
25+
def meta_version
26+
client.send(:client_meta_version, Elasticsearch::VERSION)
27+
end

elasticsearch/spec/unit/api_key_spec.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
require 'spec_helper'
1819

1920
describe Elasticsearch::Client do
@@ -81,9 +82,6 @@
8182
end
8283
end
8384

84-
def meta_version
85-
client.send(:client_meta_version, Elasticsearch::VERSION)
86-
end
8785
context 'when using API Key' do
8886
let(:client) do
8987
described_class.new(api_key: 'an_api_key')
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
require 'spec_helper'
18+
require 'ostruct'
19+
20+
describe Elasticsearch::Client do
21+
let(:transport) { client.instance_variable_get('@transport') }
22+
let(:client) { described_class.new }
23+
24+
before do
25+
allow(transport).to receive(:perform_request) { OpenStruct.new(body: '') }
26+
end
27+
28+
context 'when x-opaque-id is set' do
29+
it 'uses x-opaque-id on a request' do
30+
client.search(opaque_id: '12345')
31+
expect(transport).to have_received(:perform_request)
32+
.with('GET', '_search', {}, nil, { 'X-Opaque-Id' => '12345' })
33+
end
34+
end
35+
36+
context 'when an x-opaque-id prefix is set on initialization' do
37+
let(:prefix) { 'elastic_cloud' }
38+
let(:client) do
39+
described_class.new(opaque_id_prefix: prefix)
40+
end
41+
42+
it 'uses x-opaque-id on a request' do
43+
expect { client.search(opaque_id: '12345') }.not_to raise_error
44+
expect(transport).to have_received(:perform_request)
45+
.with('GET', '_search', {}, nil, { 'X-Opaque-Id' => 'elastic_cloud12345' })
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)