Skip to content

Commit

Permalink
Remove Content-Type from non-body HTTP methods
Browse files Browse the repository at this point in the history
- GET and DELETE.
- Expose the headers as arg to make it explicit and move the decision/concern to each method instead of send_request.
- Use keywords args as there're 3 optional ones.
- Extract the non-body headers to a property to convey its intention and reduce duplication.
- Duly update related test.
  • Loading branch information
thicolares committed Oct 22, 2021
1 parent 8e54bd1 commit 20b6302
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,54 @@ def initialize(url, api_key = nil, options = {})
'Content-Type' => 'application/json',
'X-Meili-API-Key' => api_key
}.compact
@headers_no_body = {
'X-Meili-API-Key' => api_key
}.compact
end

def http_get(relative_path = '', query_params = {})
send_request(
proc { |path, config| self.class.get(path, config) },
relative_path,
query_params
query_params: query_params,
headers: @headers_no_body
)
end

def http_post(relative_path = '', body = nil, query_params = nil)
send_request(
proc { |path, config| self.class.post(path, config) },
relative_path,
query_params,
body
query_params: query_params,
body: body,
headers: @headers
)
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,
body
query_params: query_params,
body: body,
headers: @headers
)
end

def http_delete(relative_path = '')
send_request(
proc { |path, config| self.class.delete(path, config) },
relative_path
relative_path,
headers: @headers_no_body
)
end

private

SNAKE_CASE = /[^a-zA-Z0-9]+(.)/.freeze

def send_request(http_method, relative_path, query_params = nil, body = nil)
config = http_config(query_params, body)
def send_request(http_method, relative_path, query_params: nil, body: nil, headers: nil)
config = http_config(query_params, body, headers)
begin
response = http_method.call(@base_url + relative_path, config)
rescue Errno::ECONNREFUSED => e
Expand All @@ -66,11 +73,10 @@ def send_request(http_method, relative_path, query_params = nil, body = nil)
validate(response)
end

def http_config(query_params, body)
def http_config(query_params, body, headers)
body = transform_attributes(body).to_json

{
headers: @headers,
headers: headers,
query: query_params,
body: body,
timeout: @options[:timeout] || 1,
Expand Down
2 changes: 1 addition & 1 deletion spec/meilisearch/index/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
expect(MeiliSearch::Index).to receive(:get).with(
"#{URL}/indexes/options",
{
headers: { 'Content-Type' => 'application/json', 'X-Meili-API-Key' => MASTER_KEY },
headers: { 'X-Meili-API-Key' => MASTER_KEY },
body: 'null',
query: {},
max_retries: 1,
Expand Down

0 comments on commit 20b6302

Please sign in to comment.