Class: ElasticsearchServerless::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/elasticsearch-serverless.rb

Constant Summary collapse

VALID_PARAMETERS =
[:adapter, :log, :logger, :serializer_class, :trace, :tracer, :headers,
:request_timeout, :retry_on_status, :retry_on_failure, :delay_on_retry,
:opentelemetry_tracer_provider]

Constants included from API

API::API_NAMESPACES, API::HTTP_DELETE, API::HTTP_GET, API::HTTP_HEAD, API::HTTP_POST, API::HTTP_PUT, API::UPPERCASE_APIS

Instance Method Summary collapse

Methods included from API

serializer

Methods included from API::Actions

#bulk, #clear_scroll, #close_point_in_time, #count, #create, #delete, #delete_by_query, #delete_script, #exists, #exists_source, #explain, #field_caps, #get, #get_script, #get_source, #index, #info, #mget, #msearch, #msearch_template, #mtermvectors, #open_point_in_time, #ping, #put_script, #rank_eval, #reindex, #render_search_template, #scripts_painless_execute, #scroll, #search, #search_mvt, #search_template, #terms_enum, #termvector, #termvectors, #update, #update_by_query

Constructor Details

#initialize(api_key:, url:, arguments: {}) ⇒ Client

Initializes an Elasticsearch Serverless Client

Parameters:

  • api_key (String)

    Base64 String, format used to authenticate with Elasticsearch

  • url (String)

    Elasticsearch endpoint

  • arguments (Hash) (defaults to: {})

    Other optional arguments.

Options Hash (arguments:):

  • :adapter (Symbol)

    A specific adapter for Faraday (e.g. ‘:patron`)

  • :log (Boolean)

    Use the default logger (disabled by default)

  • :logger (Object)

    An instance of a Logger-compatible object

  • :request_timeout (Integer)

    The request timeout to be passed to transport in options in seconds

  • :retry_on_failure (Boolean, Number)

    Retry X times when request fails before raising and exception (false by default)

  • Array (Number)

    :retry_on_status Retry when specific status codes are returned

  • :delay_on_retry (Number)

    Delay in milliseconds between each retry (0 by default)

  • :trace (Boolean)

    Use the default tracer (disabled by default)

  • :trace (Boolean)

    Use the default tracer (disabled by default)

  • :tracer (Object)

    An instance of a Logger-compatible object

  • :serializer_class (Constant)

    A specific serializer class to use, will be initialized by the transport and passed the transport instance

  • :headers (Hash)

    Custom HTTP Request Headers



51
52
53
54
55
# File 'lib/elasticsearch-serverless.rb', line 51

def initialize(api_key:, url:, arguments: {})
  validate_arguments(arguments)
  arguments.merge!(essential_parameters(url, api_key))
  @transport = Elastic::Transport::Client.new(arguments)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Metaprogramming for integration with Transport



78
79
80
81
82
83
84
# File 'lib/elasticsearch-serverless.rb', line 78

def method_missing(name, *args, &block)
  if methods.include?(name)
    super
  else
    @transport.send(name, *args, &block)
  end
end

Instance Method Details

#essential_parameters(url, api_key) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/elasticsearch-serverless.rb', line 57

def essential_parameters(url, api_key)
  {
    host: url,
    transport_options: {
      headers: {
        'Authorization' => "ApiKey #{api_key}",
        'Elastic-Api-Version' => ElasticsearchServerless::API_VERSION,
        user_agent: user_agent
      }
    },
    compression: true
  }
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/elasticsearch-serverless.rb', line 86

def respond_to_missing?(method_name, *args)
  @transport.respond_to?(method_name) || super
end

#user_agentObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/elasticsearch-serverless.rb', line 90

def user_agent
  user_agent = [
    "elasticsearch-serverless-ruby/#{ElasticsearchServerless::VERSION}",
    "elastic-transport-ruby/#{Elastic::Transport::VERSION}",
    "RUBY_VERSION: #{RUBY_VERSION}"
  ]
  if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
    user_agent << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}"
  end
  user_agent.join('; ')
end

#validate_arguments(arguments) ⇒ Object



71
72
73
74
75
# File 'lib/elasticsearch-serverless.rb', line 71

def validate_arguments(arguments)
  arguments.map do |k, _|
    raise ArgumentError, "Parameter #{k}" unless VALID_PARAMETERS.include?(k)
  end
end