Skip to content

Commit 5b5ad35

Browse files
committed
[API] Refactors namespace and requiring API code
Based on the code written in the Elasticsearch Serverless client, this change makes the creation of the namespace clients dynamic, removing all the files that were under `namespace` before. Some outdated and deprecated code was removed too, like unnecessary namespaces.
1 parent 2f15b38 commit 5b5ad35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+80
-1550
lines changed

elasticsearch-api/lib/elasticsearch/api.rb

+80-55
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,104 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
require "cgi"
19-
require "multi_json"
20-
21-
require "elasticsearch/api/version"
22-
require "elasticsearch/api/namespace/common"
23-
require "elasticsearch/api/utils"
18+
require 'cgi'
19+
require 'multi_json'
20+
require 'elasticsearch/api/version'
21+
require 'elasticsearch/api/utils'
2422
require 'elasticsearch/api/response'
2523

26-
Dir[ File.expand_path('../api/actions/**/*.rb', __FILE__) ].each { |f| require f }
27-
Dir[ File.expand_path('../api/namespace/**/*.rb', __FILE__) ].each { |f| require f }
24+
Dir[File.expand_path('api/actions/**/*.rb', __dir__)].each { |f| require f }
2825

2926
module Elasticsearch
3027
# This is the main module for including all API endpoint functions
3128
# It includes the namespace modules from ./api/actions
3229
module API
30+
include Elasticsearch::API::Actions
3331
DEFAULT_SERIALIZER = MultiJson
3432

3533
HTTP_GET = 'GET'.freeze
3634
HTTP_HEAD = 'HEAD'.freeze
3735
HTTP_POST = 'POST'.freeze
3836
HTTP_PUT = 'PUT'.freeze
3937
HTTP_DELETE = 'DELETE'.freeze
40-
UNDERSCORE_SEARCH = '_search'.freeze
41-
UNDERSCORE_ALL = '_all'.freeze
42-
DEFAULT_DOC = '_doc'.freeze
4338

44-
# Auto-include all namespaces in the receiver
39+
module CommonClient
40+
attr_reader :client
41+
42+
def initialize(client)
43+
@client = client
44+
end
45+
46+
def perform_request(method, path, params = {}, body = nil, headers = nil, request_opts = {})
47+
client.perform_request(method, path, params, body, headers, request_opts)
48+
end
49+
end
50+
51+
# Add new namespaces to this constant
4552
#
46-
def self.included(base)
47-
base.send :include,
48-
Elasticsearch::API::Common,
49-
Elasticsearch::API::Actions,
50-
Elasticsearch::API::Cluster,
51-
Elasticsearch::API::Nodes,
52-
Elasticsearch::API::Indices,
53-
Elasticsearch::API::Ingest,
54-
Elasticsearch::API::Snapshot,
55-
Elasticsearch::API::Tasks,
56-
Elasticsearch::API::Cat,
57-
Elasticsearch::API::Remote,
58-
Elasticsearch::API::DanglingIndices,
59-
Elasticsearch::API::Features,
60-
Elasticsearch::API::AsyncSearch,
61-
Elasticsearch::API::Autoscaling,
62-
Elasticsearch::API::CrossClusterReplication,
63-
Elasticsearch::API::DataFrameTransformDeprecated,
64-
Elasticsearch::API::Enrich,
65-
Elasticsearch::API::Eql,
66-
Elasticsearch::API::Fleet,
67-
Elasticsearch::API::Graph,
68-
Elasticsearch::API::IndexLifecycleManagement,
69-
Elasticsearch::API::License,
70-
Elasticsearch::API::Logstash,
71-
Elasticsearch::API::Migration,
72-
Elasticsearch::API::MachineLearning,
73-
Elasticsearch::API::SearchableSnapshots,
74-
Elasticsearch::API::Security,
75-
Elasticsearch::API::SnapshotLifecycleManagement,
76-
Elasticsearch::API::SQL,
77-
Elasticsearch::API::SSL,
78-
Elasticsearch::API::TextStructure,
79-
Elasticsearch::API::Transform,
80-
Elasticsearch::API::Watcher,
81-
Elasticsearch::API::XPack,
82-
Elasticsearch::API::SearchApplication,
83-
Elasticsearch::API::Synonyms,
84-
Elasticsearch::API::Esql,
85-
Elasticsearch::API::Inference,
86-
Elasticsearch::API::Simulate,
87-
Elasticsearch::API::Connector,
88-
Elasticsearch::API::QueryRules
53+
API_NAMESPACES = %i[
54+
async_search
55+
cat
56+
cluster
57+
connector
58+
cross_cluster_replication
59+
dangling_indices
60+
enrich
61+
eql
62+
esql
63+
features
64+
fleet
65+
graph
66+
index_lifecycle_management
67+
indices
68+
inference
69+
ingest
70+
license
71+
logstash
72+
machine_learning
73+
migration
74+
nodes
75+
query_rules
76+
sql
77+
ssl
78+
search_application
79+
searchable_snapshots
80+
security
81+
simulate
82+
snapshot
83+
snapshot_lifecycle_management
84+
synonyms
85+
tasks
86+
text_structure
87+
transform
88+
watcher
89+
xpack
90+
].freeze
91+
UPPERCASE_APIS = %w[sql ssl].freeze
92+
93+
API_NAMESPACES.each do |namespace|
94+
name = namespace.to_s
95+
module_name = if UPPERCASE_APIS.include?(name)
96+
name.upcase
97+
elsif name == 'xpack'
98+
'XPack'
99+
else
100+
name.split('_').map(&:capitalize).join
101+
end
102+
class_name = "#{module_name}Client"
103+
104+
klass = Class.new(Object) do
105+
include CommonClient, Object.const_get("Elasticsearch::API::#{module_name}::Actions")
106+
end
107+
Object.const_set(class_name, klass)
108+
define_method(name) do
109+
instance_variable_set("@#{name}", klass.new(self))
110+
end
89111
end
90112

113+
alias ml machine_learning
114+
alias ilm index_lifecycle_management
115+
91116
# The serializer class
92117
#
93118
def self.serializer

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

-36
This file was deleted.

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

-36
This file was deleted.

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

-36
This file was deleted.

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

-37
This file was deleted.

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

-42
This file was deleted.

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

-36
This file was deleted.

0 commit comments

Comments
 (0)