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

106 lines
2.9 KiB
Python
Raw Normal View History

2014-05-29 01:16:17 +02:00
from __future__ import unicode_literals
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
2019-10-18 22:36:30 +00:00
from ..compat import string_types, quote, PY2
2013-06-16 16:28:51 +02:00
# parts of URL to be omitted
2019-03-29 09:25:23 -06:00
SKIP_IN_PATH = (None, "", b"", [], ())
2013-06-16 16:28:51 +02:00
2013-08-08 18:06:25 +02:00
def _escape(value):
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
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
elif isinstance(value, (date, datetime)):
value = value.isoformat()
# make bools into true/false strings
elif isinstance(value, bool):
value = str(value).lower()
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):
if PY2 and isinstance(value, unicode):
2019-03-29 09:25:23 -06:00
return value.encode("utf-8")
if not PY2 and 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
2013-06-16 16:28:51 +02:00
def _make_path(*parts):
"""
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
def query_params(*es_query_params):
"""
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
2013-06-16 16:28:51 +02:00
def _wrapper(func):
@wraps(func)
def _wrapped(*args, **kwargs):
params = {}
2019-03-29 09:25:23 -06:00
if "params" in kwargs:
params = kwargs.pop("params").copy()
2013-06-16 16:28:51 +02:00
for p in es_query_params + GLOBAL_PARAMS:
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
# don't treat ignore and request_timeout as other params to avoid escaping
2019-03-29 09:25:23 -06:00
for p in ("ignore", "request_timeout"):
if p in kwargs:
params[p] = kwargs.pop(p)
2013-06-16 16:28:51 +02:00
return func(*args, params=params, **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
class NamespacedClient(object):
def __init__(self, client):
self.client = client
@property
def transport(self):
return self.client.transport
2019-03-29 09:25:23 -06:00
class AddonClient(NamespacedClient):
@classmethod
def infect_client(cls, client):
addon = cls(weakref.proxy(client))
setattr(client, cls.namespace, addon)
return client