Skip to content

Commit cded040

Browse files
committed
Add request opts to perform_request method args
1 parent 325448e commit cded040

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

elasticsearch-api/lib/elasticsearch/api/namespace/common.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def initialize(client)
3434

3535
# Delegates the `perform_request` method to the wrapped client
3636
#
37-
def perform_request(method, path, params={}, body=nil, headers=nil)
38-
client.perform_request method, path, params, body, headers
37+
def perform_request(method, path, params={}, body=nil, headers=nil, request_opts={})
38+
client.perform_request method, path, params, body, headers, request_opts
3939
end
4040
end
4141

elasticsearch-api/utils/endpoint_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
# encoding: UTF-8
1919

20+
require_relative 'endpoint_specifics'
21+
2022
module Elasticsearch
2123
module API
2224
class EndpointSpec
@@ -36,6 +38,8 @@ def initialize(filepath)
3638
@path_parts = parse_endpoint_parts(@spec)
3739
@params = @spec['params'] || {}
3840
@paths = @spec['url']['paths'].map { |b| b['path'] } if @spec['url']
41+
@path_params = path_variables.flatten.uniq.collect(&:to_sym)
42+
@perform_request_opts = { endpoint: @endpoint_name }
3943
@http_method = parse_http_method(@spec)
4044
@deprecation_note = @spec['url']['paths'].last&.[]('deprecated')
4145
@http_path = parse_http_path(@paths)
@@ -53,7 +57,9 @@ def initialize(filepath)
5357
:http_path,
5458
:required_parts,
5559
:http_method,
56-
:namespace_depth
60+
:namespace_depth,
61+
:path_params,
62+
:perform_request_opts
5763

5864
def body
5965
@spec['body']

elasticsearch-api/utils/endpoint_specifics.rb

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

elasticsearch-api/utils/thor/generate_source.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
require 'coderay'
2424
require 'pry'
2525
require_relative 'generator/files_helper'
26-
require_relative '../endpoint_specifics'
2726
require_relative '../endpoint_spec'
2827

2928
module Elasticsearch

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,30 @@
2020
<%= self.send("#{@spec.method_name}_body_helper".to_s) %>
2121
headers.merge!("Content-Type" => "application/x-ndjson")
2222
Elasticsearch::API::Response.new(
23-
perform_request(method, path, params, payload, headers)
23+
perform_request(method, path, params, payload, headers, request_opts)
2424
)
2525
<%- elsif @spec.method_name == 'ping' -%>
2626
<%= ping_perform_request %>
2727
<%- else -%>
2828
<%- if needs_ignore_404?(@spec.endpoint_name) %>
2929
Utils.__rescue_from_not_found do
30-
perform_request(method, path, params, body, headers).status == 200 ? true : false
30+
perform_request(method, path, params, body, headers, request_opts).status == 200 ? true : false
3131
end
3232
<%- elsif needs_complex_ignore_404?(@spec.endpoint_name) -%>
3333
if Array(arguments[:ignore]).include?(404)
3434
Utils.__rescue_from_not_found {
3535
Elasticsearch::API::Response.new(
36-
perform_request(method, path, params, body, headers)
36+
perform_request(method, path, params, body, headers, request_opts)
3737
)
3838
}
3939
else
4040
Elasticsearch::API::Response.new(
41-
perform_request(method, path, params, body, headers)
41+
perform_request(method, path, params, body, headers, request_opts)
4242
)
4343
end
4444
<%- else -%>
4545
Elasticsearch::API::Response.new(
46-
perform_request(method, path, params, body, headers)
46+
perform_request(method, path, params, body, headers, request_opts)
4747
)
4848
<%- end -%>
4949
<%- end -%>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ module Elasticsearch
2424
<%= ERB.new(File.new("./thor/templates/_documentation_top.erb").read, trim_mode: '-').result(binding) -%>
2525
<%# Method definition -%>
2626
<%= ' '*(@spec.namespace_depth+3) -%>def <%= @spec.method_name %>(arguments = {})
27+
request_opts = <%= @spec.perform_request_opts %>
28+
<%- unless @spec.path_params.empty? %>
29+
defined_params =<%= @spec.path_params %>.inject({}) do |set_variables, variable|
30+
set_variables[variable] = arguments[variable] if arguments.key?(variable)
31+
set_variables
32+
end
33+
request_opts[:defined_params] = defined_params unless defined_params.empty?
34+
<%- end %>
35+
<%- %>
2736
<%- if @spec.endpoint_name == 'create' -%>
2837
<%= ' '*(@spec.namespace_depth+3) %>if arguments[:id]
2938
<%= ' '*(@spec.namespace_depth+3) %> index arguments.update op_type: 'create'

0 commit comments

Comments
 (0)