Skip to content

Commit

Permalink
Merge #253
Browse files Browse the repository at this point in the history
253: Remove Content-Type from non-body HTTP methods r=curquiza a=thicolares

- The non-body HTTP methods: 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 multiple optional ones.
- Extract the non-body headers to a classes property to convey its intention with its name and reduce duplication.
- Duly update related test.

Closes the topic 1/3 of #243. It's a small step towards the other topics. Maybe they could even be bundled into a single PR, but I don't think it's mandatory.
>  Currently, the SDKs always send Content-Type: application/json to every request. Only the POST and PUT requests should send the Content-Type: application/json and not the DELETE and GET ones.

Co-authored-by: Thiago Colares <thicolares@gmail.com>
  • Loading branch information
bors[bot] and thicolares committed Oct 24, 2021
2 parents 396b8d8 + 20b6302 commit 836d83b
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
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
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 836d83b

Please sign in to comment.