Skip to content

Commit 29d1f5e

Browse files
committed
[API] Generator changes for Custom HTTP Headers
1 parent 96723cf commit 29d1f5e

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

elasticsearch-api/utils/thor/generator/endpoint_specifics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def termvectors_path
8787
def ping_perform_request
8888
<<~SRC
8989
begin
90-
perform_request(method, path, params, body).status == 200 ? true : false
90+
perform_request(method, path, params, body, headers).status == 200 ? true : false
9191
rescue Exception => e
9292
if e.class.to_s =~ /NotFound|ConnectionFailed/ || e.message =~ /Not\s*Found|404|ConnectionFailed/i
9393
false

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<%# URL parameters -%>
1313
<%- @params.each do |name, info| -%>
1414
<%= docs_helper(name, info) -%>
15-
<%- end %>
15+
<%- end -%>
16+
# @option arguments [Hash] :headers Custom HTTP headers
1617
<%= ' '*(@namespace_depth+3) + '# @option arguments [Hash] :body ' + (@spec['body']['description'] ? @spec['body']['description'].strip : 'TODO: Description') + (@spec['body']['required'] ? ' (*Required*)' : '') + "\n" if @spec['body'] -%>
1718
<% if @deprecation_note -%>
1819
#
@@ -23,5 +24,5 @@
2324
<% end -%>
2425
<%= ' '*(@namespace_depth+3) -%>#
2526
<%# Documentation link -%>
26-
<%= ' '*(@namespace_depth+3) %># @see <%= @spec['documentation']['url'] ? @spec['documentation']['url'].gsub!(/\/(current|master)\//, '/7.5/') : "[TODO]" %>
27+
<%= ' '*(@namespace_depth+3) %># @see <%= @spec['documentation']['url'] ? @spec['documentation']['url'] : "[TODO]" %>
2728
<%= ' '*(@namespace_depth+3) %>#

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ when 'mtermvectors'
1616
<%- end -%>
1717
<%- if ['bulk', 'msearch', 'msearch_template'].include? @method_name -%>
1818
<%= self.send("#{@method_name}_body_helper".to_s) %>
19-
<%= ' '*(@namespace_depth+4) %>perform_request(method, path, params, payload, {"Content-Type" => "application/x-ndjson"}).body
19+
headers.merge!("Content-Type" => "application/x-ndjson")
20+
<%= ' '*(@namespace_depth+4) %>perform_request(method, path, params, payload, headers).body
2021
<%- elsif @method_name == 'ping' -%>
2122
<%= ping_perform_request %>
2223
<%- else -%>
2324
<%- if needs_ignore_404?(@endpoint_name) %>
2425
<%= __utils %>.__rescue_from_not_found do
25-
perform_request(method, path, params, body).status == 200 ? true : false
26+
perform_request(method, path, params, body, headers).status == 200 ? true : false
2627
end
2728
<%- elsif needs_complex_ignore_404?(@endpoint_name) -%>
2829
if Array(arguments[:ignore]).include?(404)
29-
<%= __utils %>.__rescue_from_not_found { perform_request(method, path, params, body).body }
30+
<%= __utils %>.__rescue_from_not_found { perform_request(method, path, params, body, headers).body }
3031
else
31-
perform_request(method, path, params, body).body
32+
perform_request(method, path, params, body, headers).body
3233
end
3334
<%- else -%>
34-
perform_request(method, path, params, body).body
35+
perform_request(method, path, params, body, headers).body
3536
<%- end -%>
3637
<%- end -%>
3738
<%= ' '*(@namespace_depth+3) %>end

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Elasticsearch
1313
<%= ' '*(@namespace_depth+2) %>module Actions
1414
<%= ERB.new(File.new("./thor/templates/_documentation_top.erb").read, trim_mode: '-').result(binding) -%>
1515
<%# Method definition -%>
16-
<%= ' '*(@namespace_depth+3) -%>def <%= @method_name %>(arguments={})
16+
<%= ' '*(@namespace_depth+3) -%>def <%= @method_name %>(arguments = {})
1717
<%- if @endpoint_name == 'create' -%>
1818
<%= ' '*(@namespace_depth+3) %>if arguments[:id]
1919
<%= ' '*(@namespace_depth+3) %> index arguments.update op_type: 'create'
@@ -30,6 +30,7 @@ module Elasticsearch
3030
<%= ' '*(@namespace_depth+3) + "raise ArgumentError, \"Required argument '#{required}' missing\" unless arguments[:#{required}]" + "\n" -%>
3131
<%- end -%>
3232
<%- end -%>
33+
headers = arguments.delete(:headers) || {}
3334
<%- # Method setup -%>
3435
<%= ERB.new(File.new("./thor/templates/_method_setup.erb").read, trim_mode: '-').result(binding) %>
3536
<%- # Perform request -%>

0 commit comments

Comments
 (0)