Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify headers handling #295

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 48 additions & 45 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,109 +7,112 @@ module MeiliSearch
class HTTPRequest
include HTTParty

attr_reader :options
attr_reader :options, :headers

DEFAULT_OPTIONS = {
timeout: 1,
max_retries: 0,
convert_body?: true
}.freeze

def initialize(url, api_key = nil, options = {})
@base_url = url
@api_key = api_key
@options = merge_options({
timeout: 1,
max_retries: 0,
headers: build_default_options_headers(api_key),
convert_body?: true
}, options)
@options = DEFAULT_OPTIONS.merge(options)
@headers = build_default_options_headers
end

def http_get(relative_path = '', query_params = {})
send_request(
proc { |path, config| self.class.get(path, config) },
relative_path,
query_params: query_params,
options: remove_options_header(@options, 'Content-Type')
config: {
query_params: query_params,
headers: remove_headers(@headers.dup, 'Content-Type'),
options: @options
}
)
end

def http_post(relative_path = '', body = nil, query_params = nil, options = {})
send_request(
proc { |path, config| self.class.post(path, config) },
relative_path,
query_params: query_params,
body: body,
options: merge_options(@options, options)
config: {
query_params: query_params,
body: body,
headers: @headers.dup.merge(options[:headers] || {}),
options: @options.merge(options)
}
)
end

def http_put(relative_path = '', body = nil, query_params = nil)
send_request(
proc { |path, config| self.class.put(path, config) },
relative_path,
query_params: query_params,
body: body,
options: @options
config: {
query_params: query_params,
body: body,
headers: @headers,
options: @options
}
)
end

def http_patch(relative_path = '', body = nil, query_params = nil)
send_request(
proc { |path, config| self.class.patch(path, config) },
relative_path,
query_params: query_params,
body: body,
options: @options
config: {
query_params: query_params,
body: body,
headers: @headers,
options: @options
}
)
end

def http_delete(relative_path = '')
send_request(
proc { |path, config| self.class.delete(path, config) },
relative_path,
options: remove_options_header(@options, 'Content-Type')
config: {
headers: remove_headers(@headers.dup, 'Content-Type'),
options: @options
}
)
end

private

def build_default_options_headers(api_key = nil)
header = {
'Content-Type' => 'application/json'
}
header = header.merge('Authorization' => "Bearer #{api_key}") unless api_key.nil?
header
end

def merge_options(default_options, added_options = {})
default_cloned_headers = default_options[:headers].clone
merged_options = default_options.merge(added_options)
merged_options[:headers] = default_cloned_headers.merge(added_options[:headers]) if added_options.key?(:headers)
merged_options
def build_default_options_headers
{
'Content-Type' => 'application/json',
'Authorization' => ("Bearer #{@api_key}" unless @api_key.nil?)
Copy link
Member Author

@brunoocasali brunoocasali Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That I was trying to show to you @curquiza that day #273 comment 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sooooooo cool!

}.compact
end

def remove_options_header(options, key)
cloned_options = clone_options(options)
cloned_options[:headers].tap { |headers| headers.delete(key) }
cloned_options
def remove_headers(data, *keys)
data.delete_if { |k| keys.include?(k) }
end

def clone_options(options)
cloned_options = options.clone
cloned_options[:headers] = options[:headers].clone
cloned_options
end
def send_request(http_method, relative_path, config: {})
config = http_config(config[:query_params], config[:body], config[:options], config[:headers])

def send_request(http_method, relative_path, query_params: nil, body: nil, options: {})
config = http_config(query_params, body, options)
begin
response = http_method.call(@base_url + relative_path, config)
rescue Errno::ECONNREFUSED => e
raise CommunicationError, e.message
end

validate(response)
end

def http_config(query_params, body, options)
def http_config(query_params, body, options, headers)
body = body.to_json if options[:convert_body?] == true
{
headers: options[:headers],
headers: headers,
query: query_params,
body: body,
timeout: options[:timeout],
Expand Down
2 changes: 1 addition & 1 deletion lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def add_documents!(documents, primary_key = nil)
alias add_or_replace_documents! add_documents!

def add_documents_json(documents, primary_key = nil)
options = { headers: { 'Content-Type' => 'application/json' }, convert_body?: false }
options = { convert_body?: false }
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
end
alias replace_documents_json add_documents_json
Expand Down
16 changes: 6 additions & 10 deletions spec/meilisearch/index/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,19 @@

it 'supports options' do
options = { timeout: 2, max_retries: 1 }
expected_headers = {
'Authorization' => "Bearer #{MASTER_KEY}"
}

new_client = MeiliSearch::Client.new(URL, MASTER_KEY, options)
new_client.create_index!('options')
index = new_client.fetch_index('options')
expect(index.options).to eq({
headers: {
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{MASTER_KEY}"
},
max_retries: 1,
timeout: 2,
convert_body?: true
})
expect(index.options).to eq({ max_retries: 1, timeout: 2, convert_body?: true })

expect(MeiliSearch::Index).to receive(:get).with(
"#{URL}/indexes/options",
{
headers: { 'Authorization' => "Bearer #{MASTER_KEY}" },
headers: expected_headers,
body: 'null',
query: {},
max_retries: 1,
Expand Down