Skip to content

Commit 6860f67

Browse files
Merge #354
354: Support custom agents r=brunoocasali a=brunoocasali Related to meilisearch/integration-guides#150 take #2 We will use this door to send information from the Rails gem. Co-authored-by: Bruno Casali <[email protected]>
2 parents 606f684 + e19ccc5 commit 6860f67

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/meilisearch/http_request.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ def build_default_options_headers
9090
{
9191
'Content-Type' => 'application/json',
9292
'Authorization' => ("Bearer #{@api_key}" unless @api_key.nil?),
93-
'User-Agent' => MeiliSearch.qualified_version
93+
'User-Agent' => [
94+
@options.fetch(:client_agents, []),
95+
MeiliSearch.qualified_version
96+
].flatten.join(';')
9497
}.compact
9598
end
9699

spec/meilisearch/index/base_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@
126126
index.fetch_info
127127
end
128128

129+
it 'supports client_agents' do
130+
custom_agent = 'Meilisearch Rails (v0.0.1)'
131+
options = { timeout: 2, max_retries: 1, client_agents: [custom_agent] }
132+
expected_headers = {
133+
'Authorization' => "Bearer #{MASTER_KEY}",
134+
'User-Agent' => "#{custom_agent};#{MeiliSearch.qualified_version}"
135+
}
136+
137+
new_client = MeiliSearch::Client.new(URL, MASTER_KEY, options)
138+
new_client.create_index!('options')
139+
index = new_client.fetch_index('options')
140+
expect(index.options).to eq(options.merge({ convert_body?: true }))
141+
142+
expect(MeiliSearch::Index).to receive(:get).with(
143+
"#{URL}/indexes/options",
144+
{
145+
headers: expected_headers,
146+
body: 'null',
147+
query: {},
148+
max_retries: 1,
149+
timeout: 2
150+
}
151+
).and_return(double(success?: true,
152+
parsed_response: { 'createdAt' => '2021-10-16T14:57:35Z',
153+
'updatedAt' => '2021-10-16T14:57:35Z' }))
154+
index.fetch_info
155+
end
156+
129157
it 'deletes index' do
130158
client.create_index!('uid')
131159

0 commit comments

Comments
 (0)