Skip to content

Commit bbbcfbf

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 e30e1f0 commit bbbcfbf

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

+78
-1550
lines changed

elasticsearch-api/lib/elasticsearch/api.rb

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,102 @@
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 = [:async_search,
54+
:cat,
55+
:cross_cluster_replication,
56+
:cluster,
57+
:connector,
58+
:dangling_indices,
59+
:enrich,
60+
:eql,
61+
:esql,
62+
:features,
63+
:fleet,
64+
:graph,
65+
:index_lifecycle_management,
66+
:indices,
67+
:inference,
68+
:ingest,
69+
:license,
70+
:logstash,
71+
:migration,
72+
:machine_learning,
73+
:nodes,
74+
:query_rules,
75+
:search_application,
76+
:searchable_snapshots,
77+
:security,
78+
:simulate,
79+
:snapshot_lifecycle_management,
80+
:snapshot,
81+
:sql,
82+
:ssl,
83+
:synonyms,
84+
:tasks,
85+
:text_structure,
86+
:transform,
87+
:watcher,
88+
:xpack].freeze
89+
90+
UPPERCASE_APIS = ['sql', 'ssl'].freeze
91+
API_NAMESPACES.each do |namespace|
92+
name = namespace.to_s
93+
module_name = if UPPERCASE_APIS.include?(name)
94+
name.upcase
95+
elsif name == 'xpack'
96+
'XPack'
97+
else
98+
name.split('_').map(&:capitalize).join
99+
end
100+
class_name = "#{module_name}Client"
101+
102+
klass = Class.new(Object) do
103+
include CommonClient, Object.const_get("Elasticsearch::API::#{module_name}::Actions")
104+
end
105+
Object.const_set(class_name, klass)
106+
define_method(name) do
107+
instance_variable_set("@#{name}", klass.new(self))
108+
end
89109
end
90110

111+
alias ml machine_learning
112+
alias ilm index_lifecycle_management
113+
91114
# The serializer class
92115
#
93116
def self.serializer

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 0 additions & 42 deletions
This file was deleted.

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)