Files
opensearch-pyd/opensearchpy/client/utils.py
T

223 lines
6.9 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
2020-04-23 11:22:08 -05:00
2022-10-04 00:15:18 +05:30
import base64
import weakref
2013-07-10 16:48:57 +02:00
from datetime import date, datetime
2013-06-16 16:28:51 +02:00
from functools import wraps
from typing import Any, Callable, Optional
2023-11-06 13:08:19 -05:00
from opensearchpy.serializer import Serializer
2023-10-25 08:41:50 -04:00
from ..compat import quote, string_types, to_bytes, to_str, unquote, urlparse
2013-06-16 16:28:51 +02:00
# parts of URL to be omitted
2023-11-06 13:08:19 -05:00
SKIP_IN_PATH: Any = (None, "", b"", [], ())
2019-03-29 09:25:23 -06:00
2013-06-16 16:28:51 +02:00
2023-11-06 13:08:19 -05:00
def _normalize_hosts(hosts: Any) -> Any:
2020-05-15 09:36:47 -05:00
"""
Helper function to transform hosts argument to
2021-09-16 14:59:29 +05:30
:class:`~opensearchpy.OpenSearch` to a list of dicts.
2020-05-15 09:36:47 -05:00
"""
# if hosts are empty, just defer to defaults down the line
if hosts is None:
return [{}]
# passed in just one string
if isinstance(hosts, string_types):
hosts = [hosts]
out = []
# normalize hosts to dicts
for host in hosts:
if isinstance(host, string_types):
if "://" not in host:
host = f"//{host}" # type: ignore
2020-05-15 09:36:47 -05:00
parsed_url = urlparse(host)
h = {"host": parsed_url.hostname}
if parsed_url.port:
h["port"] = parsed_url.port
if parsed_url.scheme == "https":
h["port"] = parsed_url.port or 443
h["use_ssl"] = True
if parsed_url.username or parsed_url.password:
h["http_auth"] = "{}:{}".format(
2020-05-15 09:36:47 -05:00
unquote(parsed_url.username),
unquote(parsed_url.password),
)
if parsed_url.path and parsed_url.path != "/":
h["url_prefix"] = parsed_url.path
out.append(h)
else:
out.append(host)
return out
2023-11-06 13:08:19 -05:00
def _escape(value: Any) -> Any:
2013-06-16 16:28:51 +02:00
"""
2013-08-08 18:06:25 +02:00
Escape a single value of a URL string or a query parameter. If it is a list
or tuple, turn it into a comma-separated string first.
2013-06-16 16:28:51 +02:00
"""
2013-08-08 18:06:25 +02:00
# make sequences into comma-separated stings
2013-07-10 16:48:57 +02:00
if isinstance(value, (list, tuple)):
2019-03-29 09:25:23 -06:00
value = ",".join(value)
2013-08-08 18:06:25 +02:00
# dates and datetimes into isoformat
2013-07-10 16:48:57 +02:00
elif isinstance(value, (date, datetime)):
value = value.isoformat()
2013-08-08 18:06:25 +02:00
# make bools into true/false strings
elif isinstance(value, bool):
value = str(value).lower()
2013-08-08 18:06:25 +02:00
2017-08-07 15:38:47 -06:00
# don't decode bytestrings
elif isinstance(value, bytes):
return value
2013-08-08 18:06:25 +02:00
# encode strings to utf-8
2014-02-21 16:53:56 +01:00
if isinstance(value, string_types):
2023-10-25 08:41:50 -04:00
if isinstance(value, str):
2019-03-29 09:25:23 -06:00
return value.encode("utf-8")
2013-08-08 18:06:25 +02:00
return str(value)
2013-07-10 16:48:57 +02:00
2019-03-29 09:25:23 -06:00
2023-11-06 13:08:19 -05:00
def _make_path(*parts: Any) -> str:
2013-06-16 16:28:51 +02:00
"""
Create a URL string from parts, omit all `None` values and empty strings.
2019-04-09 23:40:02 +03:00
Convert lists and tuples to comma separated values.
2013-06-16 16:28:51 +02:00
"""
2019-03-29 09:25:23 -06:00
# TODO: maybe only allow some parts to be lists/tuples ?
return "/" + "/".join(
# preserve ',' and '*' in url for nicer URLs in logs
2019-10-18 22:36:30 +00:00
quote(_escape(p), b",*")
2019-03-29 09:25:23 -06:00
for p in parts
if p not in SKIP_IN_PATH
)
2013-06-16 16:28:51 +02:00
# parameters that apply to all methods
2019-03-29 09:25:23 -06:00
GLOBAL_PARAMS = ("pretty", "human", "error_trace", "format", "filter_path")
2013-06-16 16:28:51 +02:00
2023-11-06 13:08:19 -05:00
def query_params(*opensearch_query_params: Any) -> Callable: # type: ignore
2013-06-16 16:28:51 +02:00
"""
Decorator that pops all accepted parameters from method's kwargs and puts
them in the params argument.
"""
2019-03-29 09:25:23 -06:00
2023-11-06 13:08:19 -05:00
def _wrapper(func: Any) -> Any:
2013-06-16 16:28:51 +02:00
@wraps(func)
2023-11-06 13:08:19 -05:00
def _wrapped(*args: Any, **kwargs: Any) -> Any:
params = (kwargs.pop("params", None) or {}).copy()
headers = {
k.lower(): v
for k, v in (kwargs.pop("headers", None) or {}).copy().items()
}
2020-03-11 16:33:15 -05:00
if "opaque_id" in kwargs:
headers["x-opaque-id"] = kwargs.pop("opaque_id")
http_auth = kwargs.pop("http_auth", None)
api_key = kwargs.pop("api_key", None)
if http_auth is not None and api_key is not None:
raise ValueError(
"Only one of 'http_auth' and 'api_key' may be passed at a time"
)
elif http_auth is not None:
headers["authorization"] = f"Basic {_base64_auth_header(http_auth)}"
elif api_key is not None:
headers["authorization"] = f"ApiKey {_base64_auth_header(api_key)}"
# don't escape ignore, request_timeout, or timeout
for p in ("ignore", "request_timeout", "timeout"):
if p in kwargs:
params[p] = kwargs.pop(p)
for p in opensearch_query_params + GLOBAL_PARAMS:
2013-06-16 16:28:51 +02:00
if p in kwargs:
v = kwargs.pop(p)
if v is not None:
params[p] = _escape(v)
2013-09-25 23:09:50 +02:00
2020-03-11 16:33:15 -05:00
return func(*args, params=params, headers=headers, **kwargs)
2019-03-29 09:25:23 -06:00
2013-06-16 16:28:51 +02:00
return _wrapped
2019-03-29 09:25:23 -06:00
2013-06-16 16:28:51 +02:00
return _wrapper
def _bulk_body(serializer: Optional[Serializer], body: Any) -> Any:
2020-01-19 00:01:36 +00:00
# if not passed in a string, serialize items and join by newline
if not isinstance(body, string_types):
body = "\n".join(map(serializer.dumps, body)) # type: ignore
2020-01-19 00:01:36 +00:00
# bulk body must end with a newline
2020-04-08 10:49:22 -05:00
if isinstance(body, bytes):
if not body.endswith(b"\n"):
body += b"\n"
elif isinstance(body, string_types) and not body.endswith("\n"): # type: ignore
body += "\n" # type: ignore
2020-01-19 00:01:36 +00:00
return body
2023-11-06 13:08:19 -05:00
def _base64_auth_header(auth_value: Any) -> str:
"""Takes either a 2-tuple or a base64-encoded string
and returns a base64-encoded string to be used
as an HTTP authorization header.
"""
if isinstance(auth_value, (list, tuple)):
auth_value = base64.b64encode(to_bytes(":".join(auth_value)))
return to_str(auth_value)
class NamespacedClient:
2023-11-06 13:08:19 -05:00
def __init__(self, client: Any) -> None:
2013-06-16 16:28:51 +02:00
self.client = client
@property
2023-11-06 13:08:19 -05:00
def transport(self) -> Any:
2013-06-16 16:28:51 +02:00
return self.client.transport
2019-03-29 09:25:23 -06:00
class AddonClient(NamespacedClient):
@classmethod
2023-11-06 13:08:19 -05:00
def infect_client(cls: Any, client: NamespacedClient) -> NamespacedClient:
addon = cls(weakref.proxy(client))
setattr(client, cls.namespace, addon)
return client