Skip to content

Commit 836d83b

Browse files
Merge #253
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 <[email protected]>
2 parents 396b8d8 + 20b6302 commit 836d83b

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lib/meilisearch/http_request.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,54 @@ def initialize(url, api_key = nil, options = {})
1717
'Content-Type' => 'application/json',
1818
'X-Meili-API-Key' => api_key
1919
}.compact
20+
@headers_no_body = {
21+
'X-Meili-API-Key' => api_key
22+
}.compact
2023
end
2124

2225
def http_get(relative_path = '', query_params = {})
2326
send_request(
2427
proc { |path, config| self.class.get(path, config) },
2528
relative_path,
26-
query_params
29+
query_params: query_params,
30+
headers: @headers_no_body
2731
)
2832
end
2933

3034
def http_post(relative_path = '', body = nil, query_params = nil)
3135
send_request(
3236
proc { |path, config| self.class.post(path, config) },
3337
relative_path,
34-
query_params,
35-
body
38+
query_params: query_params,
39+
body: body,
40+
headers: @headers
3641
)
3742
end
3843

3944
def http_put(relative_path = '', body = nil, query_params = nil)
4045
send_request(
4146
proc { |path, config| self.class.put(path, config) },
4247
relative_path,
43-
query_params,
44-
body
48+
query_params: query_params,
49+
body: body,
50+
headers: @headers
4551
)
4652
end
4753

4854
def http_delete(relative_path = '')
4955
send_request(
5056
proc { |path, config| self.class.delete(path, config) },
51-
relative_path
57+
relative_path,
58+
headers: @headers_no_body
5259
)
5360
end
5461

5562
private
5663

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

59-
def send_request(http_method, relative_path, query_params = nil, body = nil)
60-
config = http_config(query_params, body)
66+
def send_request(http_method, relative_path, query_params: nil, body: nil, headers: nil)
67+
config = http_config(query_params, body, headers)
6168
begin
6269
response = http_method.call(@base_url + relative_path, config)
6370
rescue Errno::ECONNREFUSED => e
@@ -66,11 +73,10 @@ def send_request(http_method, relative_path, query_params = nil, body = nil)
6673
validate(response)
6774
end
6875

69-
def http_config(query_params, body)
76+
def http_config(query_params, body, headers)
7077
body = transform_attributes(body).to_json
71-
7278
{
73-
headers: @headers,
79+
headers: headers,
7480
query: query_params,
7581
body: body,
7682
timeout: @options[:timeout] || 1,

spec/meilisearch/index/base_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
expect(MeiliSearch::Index).to receive(:get).with(
6363
"#{URL}/indexes/options",
6464
{
65-
headers: { 'Content-Type' => 'application/json', 'X-Meili-API-Key' => MASTER_KEY },
65+
headers: { 'X-Meili-API-Key' => MASTER_KEY },
6666
body: 'null',
6767
query: {},
6868
max_retries: 1,

0 commit comments

Comments
 (0)