transport
- class opensearchpy.Transport(hosts, connection_class=None, connection_pool_class=<class 'opensearchpy.connection_pool.ConnectionPool'>, host_info_callback=<function get_host_info>, sniff_on_start=False, sniffer_timeout=None, sniff_timeout=0.1, sniff_on_connection_fail=False, serializer=<opensearchpy.serializer.JSONSerializer object>, serializers=None, default_mimetype='application/json', max_retries=3, pool_maxsize=None, retry_on_status=(502, 503, 504), retry_on_timeout=False, send_get_body_as='GET', metrics=<opensearchpy.metrics.metrics_none.MetricsNone object>, **kwargs)[source]
Bases:
object
Encapsulation of transport-related to logic. Handles instantiation of the individual connections as well as creating a connection pool to hold them.
Main interface is the perform_request method.
- Parameters:
hosts (Any) – list of dictionaries, each containing keyword arguments to create a connection_class instance
connection_class (Type[Connection]) – subclass of
Connection
to useconnection_pool_class (Any) – subclass of
ConnectionPool
to usehost_info_callback (Any) – callback responsible for taking the node information from /_cluster/nodes, along with already extracted information, and producing a list of arguments (same as hosts parameter)
sniff_on_start (bool) – flag indicating whether to obtain a list of nodes from the cluster at startup time
sniffer_timeout (float | None) – number of seconds between automatic sniffs
sniff_on_connection_fail (bool) – flag controlling if connection failure triggers a sniff
sniff_timeout (float | None) – timeout used for the sniff request - it should be a fast api call and we are talking potentially to more nodes so we want to fail quickly. Not used during initial sniffing (if
sniff_on_start
is on) when the connection still isn’t initialized.serializer (Serializer) – serializer instance
serializers (Mapping[str, Serializer] | None) – optional dict of serializer instances that will be used for deserializing data coming from the server. (key is the mimetype)
default_mimetype (str) – when no mimetype is specified by the server response assume this mimetype, defaults to ‘application/json’
max_retries (int) – maximum number of retries before an exception is propagated
retry_on_status (Collection[int]) – set of HTTP status codes on which we should retry on a different node. defaults to
(502, 503, 504)
retry_on_timeout (bool) – should timeout trigger a retry on different node? (default False)
send_get_body_as (str) – for GET requests with body this option allows you to specify an alternate way of execution for environments that don’t support passing bodies with GET requests. If you set this to ‘POST’ a POST method will be used instead, if to ‘source’ then the body will be serialized and passed as a query parameter source.
pool_maxsize (int | None) – Maximum connection pool size used by pool-manager For custom connection-pooling on current session
metrics (Metrics) – metrics is an instance of a subclass of the
Metrics
class, used for collecting and reporting metrics related to the client’s operations;hosts –
connection_class –
connection_pool_class –
host_info_callback –
sniff_on_start –
sniffer_timeout –
sniff_timeout –
sniff_on_connection_fail –
serializer –
serializers –
default_mimetype –
max_retries –
pool_maxsize –
retry_on_status –
retry_on_timeout –
send_get_body_as –
metrics –
kwargs (Any) –
Any extra keyword arguments will be passed to the connection_class when creating and instance unless overridden by that connection’s options provided as part of the hosts parameter.
- DEFAULT_CONNECTION_CLASS
alias of
Urllib3HttpConnection
- _get_sniff_data(initial=False)[source]
Perform the request to get sniffing information. Returns a list of dictionaries (one per node) containing all the information from the cluster.
It also sets the last_sniff attribute in case of a successful attempt.
In rare cases it might be possible to override this method in your custom Transport class to serve data from alternative source like configuration management.
- _resolve_request_args(method, params, body, ignore, timeout)[source]
Resolves parameters for .perform_request()
- add_connection(host)[source]
Create a new
Connection
instance and add it to the pool.- Parameters:
host (Any) – kwargs that will be used to create the instance
host –
- Return type:
None
- get_connection()[source]
Retrieve a
Connection
instance from theConnectionPool
instance.- Return type:
- mark_dead(connection)[source]
Mark a connection as dead (failed) in the connection pool. If sniffing on failure is enabled this will initiate the sniffing process.
- Parameters:
connection (Connection) – instance of
Connection
that failedconnection –
- Return type:
None
- perform_request(method, url, params=None, body=None, timeout=None, ignore=(), headers=None)[source]
Perform the actual request. Retrieve a connection from the connection pool, pass all the information to its perform_request method and return the data.
If an exception was raised, mark the connection as failed and retry (up to max_retries times).
If the operation was successful and the connection used was previously marked as dead, mark it as live, resetting its failure count.
- Parameters:
method (str) – HTTP method to use
url (str) – absolute url (without host) to target
headers (Mapping[str, str] | None) – dictionary of headers, will be handed over to the underlying
Connection
classparams (Mapping[str, Any] | None) – dictionary of query parameters, will be handed over to the underlying
Connection
class for serializationbody (Any | None) – body of the request, will be serialized using serializer and passed to the connection
timeout (int | float | None) – timeout of the request. If it is not presented as argument will be extracted from params
method –
url –
params –
body –
timeout –
ignore (Collection[int]) –
headers –
- Return type:
- set_connections(hosts)[source]
Instantiate all the connections and create new connection pool to hold them. Tries to identify unchanged hosts and re-use existing
Connection
instances.- Parameters:
hosts (Any) – same as __init__
hosts –
- Return type:
None