Class: OpenSearch::Transport::Client
- Inherits:
-
Object
- Object
- OpenSearch::Transport::Client
- Defined in:
- lib/opensearch/transport/client.rb
Overview
Handles communication with an OpenSearch cluster.
See README for usage and code examples.
Constant Summary collapse
- DEFAULT_TRANSPORT_CLASS =
Transport::HTTP::Faraday
- DEFAULT_LOGGER =
lambda do require 'logger' logger = Logger.new($stderr) logger.progname = 'opensearch' logger.formatter = proc { |_severity, datetime, _progname, msg| "#{datetime}: #{msg}\n" } logger end
- DEFAULT_TRACER =
lambda do require 'logger' logger = Logger.new($stderr) logger.progname = 'opensearch.tracer' logger.formatter = proc { |_severity, _datetime, _progname, msg| "#{msg}\n" } logger end
- DEFAULT_HOST =
The default host and port to use if not otherwise specified.
'localhost:9200'.freeze
- DEFAULT_CLOUD_PORT =
The default port to use if connecting using a Cloud ID. Updated from 9243 to 443 in client version 7.10.1
443
- DEFAULT_PORT =
The default port to use if not otherwise specified.
9200
Instance Attribute Summary collapse
-
#transport ⇒ Object
Returns the transport object.
Instance Method Summary collapse
-
#initialize(arguments = {}) {|faraday| ... } ⇒ Client
constructor
Create a client connected to an OpenSearch cluster.
-
#perform_request(method, path, params = {}, body = nil, headers = nil) ⇒ Object
Performs a request through delegation to #transport.
Constructor Details
#initialize(arguments = {}) {|faraday| ... } ⇒ Client
Create a client connected to an OpenSearch cluster.
Specify the URL via arguments or set the ‘OPENSEARCH_URL` environment variable.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/opensearch/transport/client.rb', line 135 def initialize(arguments = {}, &block) @options = arguments.transform_keys(&:to_sym) @arguments = @options @arguments[:logger] ||= @arguments[:log] ? DEFAULT_LOGGER.call : nil @arguments[:tracer] ||= @arguments[:trace] ? DEFAULT_TRACER.call : nil @arguments[:reload_connections] ||= false @arguments[:retry_on_failure] ||= false @arguments[:reload_on_failure] ||= false @arguments[:randomize_hosts] ||= false @arguments[:transport_options] ||= {} @arguments[:http] ||= {} @options[:http] ||= {} set_api_key if (@api_key = @arguments[:api_key]) set_compatibility_header if ENV['ELASTIC_CLIENT_APIVERSIONING'] @seeds = extract_cloud_creds(@arguments) @seeds ||= __extract_hosts(@arguments[:hosts] || @arguments[:host] || @arguments[:url] || @arguments[:urls] || ENV.fetch('OPENSEARCH_URL', nil) || DEFAULT_HOST) @send_get_body_as = @arguments[:send_get_body_as] || 'GET' @opaque_id_prefix = @arguments[:opaque_id_prefix] || nil if @arguments[:request_timeout] @arguments[:transport_options][:request] = { timeout: @arguments[:request_timeout] } end if @arguments[:transport] @transport = @arguments[:transport] else @transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS @transport = if @transport_class == Transport::HTTP::Faraday @arguments[:adapter] ||= __auto_detect_adapter @transport_class.new(hosts: @seeds, options: @arguments) do |faraday| faraday.adapter(@arguments[:adapter]) block&.call faraday end else @transport_class.new(hosts: @seeds, options: @arguments) end end end |
Instance Attribute Details
#transport ⇒ Object
Returns the transport object.
73 74 75 |
# File 'lib/opensearch/transport/client.rb', line 73 def transport @transport end |
Instance Method Details
#perform_request(method, path, params = {}, body = nil, headers = nil) ⇒ Object
Performs a request through delegation to #transport.
184 185 186 187 188 189 190 191 192 |
# File 'lib/opensearch/transport/client.rb', line 184 def perform_request(method, path, params = {}, body = nil, headers = nil) method = @send_get_body_as if method == 'GET' && body if (opaque_id = params.delete(:opaque_id)) headers = {} if headers.nil? opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id headers.merge!('X-Opaque-Id' => opaque_id) end transport.perform_request(method, path, params, body, headers) end |