Skip to content

Commit

Permalink
# Fixed url constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed Dec 16, 2024
1 parent 2734da2 commit 3b2381d
Show file tree
Hide file tree
Showing 365 changed files with 399 additions and 368 deletions.
33 changes: 33 additions & 0 deletions api_generator/lib/components/api_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

# frozen_string_literal: true

class ApiPath
# @param [String] url
def initialize(url)
@url = url
puts url
puts "COMPONENTS: #{components}"
puts "BUILD: #{build}"
end


def build
return components.join('') if components.none? { |comp| comp.start_with?('_') }
"[#{components.join(', ')}].filter(&:present?).join('/').squeeze('/')"
end

def components
@components ||= @url.split('{').flat_map { |part| part.split('}') }.map do |comp|
next "_#{comp}" unless comp.include?('/') # path param
comp = comp.sub(%r{^/}, '').sub(%r{/$}, '') # remove leading/trailing slashes
"'#{comp}'" if comp.present?
end.compact
end

private
end
9 changes: 4 additions & 5 deletions api_generator/lib/generators/action_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

require_relative 'base_generator'
require_relative '../components/action'
require_relative '../components/api_path'

# Render an API Action via Mustache
class ActionRenderer < BaseGenerator
Expand All @@ -22,7 +23,6 @@ def initialize(gem_folder, namespace, action)
output_file = gem_folder.join('lib', 'opensearch', 'api', 'actions', namespace.name, "#{action.name.underscore}.rb")
super(output_file)
@action = action
@urls = action.urls.map { |u| u.split('/').select(&:present?) }.uniq
@api_reference = action.api_reference
@module_name = namespace.root ? 'Root' : namespace.name.camelize
@method_name = action.name.underscore
Expand All @@ -41,10 +41,9 @@ def argument_descriptions
end
end

def url_components
@urls.max_by(&:length)
.map { |e| e.starts_with?('{') ? "_#{e[/{(.+)}/, 1]}" : "'#{e}'" }
.join(', ')
def url
api_path = ApiPath.new(@action.urls.max_by(&:length))
api_path.build
end

def http_verb
Expand Down
2 changes: 1 addition & 1 deletion api_generator/lib/templates/action.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ module OpenSearch
headers = args.delete('headers') || {}
body = args.delete('body')
{{/bulk_body}}
url = [{{{url_components}}}].filter(&:present?).join('/').squeeze('/')
method = {{{http_verb}}}
url = {{{url}}}

Utils.validate_query_params args, {{valid_params_constant_name}}
{{{perform_request}}}
Expand Down
1 change: 0 additions & 1 deletion lib/opensearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# This file is generated from the OpenSearch REST API spec.
# Do not modify it by hand. Instead, modify the generator or the spec.

require 'active_support/all'
require 'opensearch/transport'
require 'opensearch/api'
Dir[File.expand_path('opensearch/api/**/*.rb', __dir__)].sort.each { |f| require f }
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/asynchronous_search/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def delete(args = {})
ignore = args.delete('ignore') || []
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_plugins', '_asynchronous_search', _id].filter(&:present?).join('/').squeeze('/')
method = 'DELETE'
url = ['_plugins/_asynchronous_search', _id].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, DELETE_QUERY_PARAMS
transport.perform_delete_request method, url, args, body, headers, ignore.include?(404)
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/asynchronous_search/get.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def get(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_plugins', '_asynchronous_search', _id].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_plugins/_asynchronous_search', _id].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, GET_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/asynchronous_search/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def search(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_plugins', '_asynchronous_search'].filter(&:present?).join('/').squeeze('/')
method = 'POST'
url = '_plugins/_asynchronous_search'

Utils.validate_query_params args, SEARCH_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/asynchronous_search/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def stats(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_plugins', '_asynchronous_search', 'stats'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_plugins/_asynchronous_search/stats'

Utils.validate_query_params args, STATS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def bulk(args = {})

headers = (args.delete('headers') || {}).merge('Content-Type' => 'application/x-ndjson')
body = Utils.bulkify(args.delete('body'))
url = [_index, '_bulk'].filter(&:present?).join('/').squeeze('/')
method = _index.present? ? 'PUT' : 'POST'
url = [_index, '_bulk'].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, BULK_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/bulk_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def bulk_stream(args = {})

headers = (args.delete('headers') || {}).merge('Content-Type' => 'application/x-ndjson')
body = Utils.bulkify(args.delete('body'))
url = [_index, '_bulk', 'stream'].filter(&:present?).join('/').squeeze('/')
method = _index.present? ? 'PUT' : 'POST'
url = [_index, '_bulk/stream'].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, BULK_STREAM_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def aliases(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'aliases', _name].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/aliases', _name].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, ALIASES_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/all_pit_segments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def all_pit_segments(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'pit_segments', '_all'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/pit_segments/_all'

Utils.validate_query_params args, ALL_PIT_SEGMENTS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def allocation(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'allocation', _node_id].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/allocation', _node_id].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, ALLOCATION_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/cluster_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def cluster_manager(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'cluster_manager'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/cluster_manager'

Utils.validate_query_params args, CLUSTER_MANAGER_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def count(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'count', _index].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/count', _index].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, COUNT_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/fielddata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def fielddata(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'fielddata', _fields].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/fielddata', _fields].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, FIELDDATA_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/health.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def health(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'health'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/health'

Utils.validate_query_params args, HEALTH_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def help(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat'

Utils.validate_query_params args, HELP_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/indices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def indices(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'indices', _index].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/indices', _index].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, INDICES_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def master(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'master'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/master'

Utils.validate_query_params args, MASTER_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/nodeattrs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def nodeattrs(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'nodeattrs'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/nodeattrs'

Utils.validate_query_params args, NODEATTRS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def nodes(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'nodes'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/nodes'

Utils.validate_query_params args, NODES_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/pending_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def pending_tasks(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'pending_tasks'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/pending_tasks'

Utils.validate_query_params args, PENDING_TASKS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/pit_segments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def pit_segments(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'pit_segments'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/pit_segments'

Utils.validate_query_params args, PIT_SEGMENTS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def plugins(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'plugins'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/plugins'

Utils.validate_query_params args, PLUGINS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/recovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def recovery(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'recovery', _index].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/recovery', _index].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, RECOVERY_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def repositories(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'repositories'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/repositories'

Utils.validate_query_params args, REPOSITORIES_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/segment_replication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def segment_replication(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'segment_replication', _index].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/segment_replication', _index].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, SEGMENT_REPLICATION_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/segments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def segments(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'segments', _index].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/segments', _index].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, SEGMENTS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/shards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def shards(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'shards', _index].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/shards', _index].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, SHARDS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/snapshots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def snapshots(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'snapshots', _repository].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/snapshots', _repository].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, SNAPSHOTS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def tasks(args = {})
args = Utils.normalize_arguments(args)
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'tasks'].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = '_cat/tasks'

Utils.validate_query_params args, TASKS_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def templates(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'templates', _name].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/templates', _name].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, TEMPLATES_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/cat/thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def thread_pool(args = {})

headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_cat', 'thread_pool', _thread_pool_patterns].filter(&:present?).join('/').squeeze('/')
method = 'GET'
url = ['_cat/thread_pool', _thread_pool_patterns].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, THREAD_POOL_QUERY_PARAMS
transport.perform_request(method, url, args, body, headers).body
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/api/actions/clear_scroll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def clear_scroll(args = {})
ignore = args.delete('ignore') || []
headers = args.delete('headers') || {}
body = args.delete('body')
url = ['_search', 'scroll', _scroll_id].filter(&:present?).join('/').squeeze('/')
method = 'DELETE'
url = ['_search/scroll', _scroll_id].filter(&:present?).join('/').squeeze('/')

Utils.validate_query_params args, CLEAR_SCROLL_QUERY_PARAMS
transport.perform_delete_request method, url, args, body, headers, ignore.include?(404)
Expand Down
Loading

0 comments on commit 3b2381d

Please sign in to comment.