Skip to content

Commit 20717dc

Browse files
committed
[Client] Refactors User-Agent
Instead of setting it up on each request, this update sets it up on client initialization.
1 parent 7c96715 commit 20717dc

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

lib/elastic/enterprise-search/client.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def transport
7777
request_timeout: overall_timeout,
7878
adapter: adapter,
7979
transport_options: {
80-
request: { open_timeout: open_timeout }
80+
request: { open_timeout: open_timeout },
81+
headers: { user_agent: user_agent }
8182
},
8283
enable_meta_header: @options[:enable_meta_header] || true,
8384
trace: trace,
@@ -128,6 +129,19 @@ def host
128129

129130
@options[:host]
130131
end
132+
133+
private
134+
135+
def user_agent
136+
ua = "#{CLIENT_NAME}/#{CLIENT_VERSION}"
137+
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
138+
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
139+
meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} " \
140+
"#{RbConfig::CONFIG['target_cpu']}"
141+
end
142+
meta << "elastic-transport: #{Elastic::Transport::VERSION}"
143+
"#{ua} (#{meta.join('; ')})"
144+
end
131145
end
132146
end
133147
end

lib/elastic/enterprise-search/request.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def delete(path, params = {}, headers = {})
4848

4949
# Construct and send a request to the API.
5050
def request(method, path, params = {}, body = {}, headers = {})
51-
meta_headers = { authorization: decide_authorization(params), user_agent: request_user_agent }
51+
meta_headers = { authorization: decide_authorization(params) }
5252
headers = if !headers.is_a?(Hash)
5353
meta_headers
5454
else
@@ -79,17 +79,6 @@ def basic_auth_header
7979

8080
private
8181

82-
def request_user_agent
83-
ua = "#{CLIENT_NAME}/#{CLIENT_VERSION}"
84-
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
85-
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
86-
meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} " \
87-
"#{RbConfig::CONFIG['target_cpu']}"
88-
end
89-
meta << "elastic-transport: #{Elastic::Transport::VERSION}"
90-
"#{ua} (#{meta.join('; ')})"
91-
end
92-
9382
def decide_authorization(params)
9483
if params[:grant_type] == 'authorization_code'
9584
"Bearer #{params[:code]}"

spec/enterprise-search/client_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,16 @@ def messages; @strio.string; end
104104
end
105105
end
106106

107+
context 'user-agent' do
108+
context 'default' do
109+
let(:transport) { Elastic::EnterpriseSearch::Client.new.instance_variable_get('@transport') }
110+
let(:subject) { transport.transport.connections.first.connection.headers }
111+
112+
it 'includes the user-agent in transport' do
113+
expect(subject['user-agent']).to match("elastic-enterprise-search-ruby/#{Elastic::EnterpriseSearch::VERSION}")
114+
end
115+
end
116+
end
117+
107118
include_examples 'adapters compatibility'
108119
end

0 commit comments

Comments
 (0)