Skip to content

Commit 56aa1b4

Browse files
committed
[API] Update and refactors Content-Type headers for ndjson APIs
1 parent 2f29b68 commit 56aa1b4

File tree

8 files changed

+12
-9
lines changed

8 files changed

+12
-9
lines changed

elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def bulk(arguments = {})
6565
payload = body
6666
end
6767

68-
headers.merge!("Content-Type" => "application/x-ndjson")
68+
headers = Utils.ndjson_headers(headers)
6969
perform_request(method, path, params, payload, headers).body
7070
end
7171

elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def msearch(arguments = {})
8383
payload = body
8484
end
8585

86-
headers.merge!("Content-Type" => "application/x-ndjson")
86+
headers = Utils.ndjson_headers(headers)
8787
perform_request(method, path, params, payload, headers).body
8888
end
8989

elasticsearch-api/lib/elasticsearch/api/actions/msearch_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def msearch_template(arguments = {})
6969
payload = body
7070
end
7171

72-
headers.merge!("Content-Type" => "application/x-ndjson")
72+
headers = Utils.ndjson_headers(headers)
7373
perform_request(method, path, params, payload, headers).body
7474
end
7575

elasticsearch-api/lib/elasticsearch/api/utils.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717

1818
module Elasticsearch
1919
module API
20-
2120
# Generic utility methods
2221
#
2322
module Utils
24-
2523
# URL-escape a string
2624
#
2725
# @example
@@ -282,6 +280,11 @@ def __report_unsupported_method(name)
282280
end
283281
end
284282

283+
def ndjson_headers(headers)
284+
headers.merge!('Content-Type' => 'application/x-ndjson') unless ['1', 'true'].include?(ENV['ELASTIC_CLIENT_APIVERSIONING'])
285+
headers
286+
end
287+
285288
extend self
286289
end
287290
end

elasticsearch-api/utils/thor/templates/_perform_request.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ when 'mtermvectors'
3434
<%- end -%>
3535
<%- if ['bulk', 'msearch', 'msearch_template', 'find_structure'].include? @method_name -%>
3636
<%= self.send("#{@method_name}_body_helper".to_s) %>
37-
headers.merge!("Content-Type" => "application/x-ndjson")
37+
headers = <%= __utils %>.ndjson_headers(headers)
3838
<%= ' '*(@namespace_depth+4) %>perform_request(method, path, params, payload, headers).body
3939
<%- elsif @method_name == 'ping' -%>
4040
<%= ping_perform_request %>

elasticsearch-xpack/lib/elasticsearch/xpack/api/actions/fleet/msearch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def msearch(arguments = {})
7272
payload = body
7373
end
7474

75-
headers.merge!("Content-Type" => "application/x-ndjson")
75+
headers = Elasticsearch::API::Utils.ndjson_headers(headers)
7676
perform_request(method, path, params, payload, headers).body
7777
end
7878
end

elasticsearch-xpack/lib/elasticsearch/xpack/api/actions/monitoring/bulk.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def bulk(arguments = {})
6161
payload = body
6262
end
6363

64-
headers.merge!("Content-Type" => "application/x-ndjson")
64+
headers = Elasticsearch::API::Utils.ndjson_headers(headers)
6565
perform_request(method, path, params, payload, headers).body
6666
end
6767

elasticsearch-xpack/lib/elasticsearch/xpack/api/actions/text_structure/find_structure.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def find_structure(arguments = {})
5959
payload = body
6060
end
6161

62-
headers.merge!("Content-Type" => "application/x-ndjson")
62+
headers = Elasticsearch::API::Utils.ndjson_headers(headers)
6363
perform_request(method, path, params, payload, headers).body
6464
end
6565

0 commit comments

Comments
 (0)