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

95 lines
2.8 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
from ..compat import string_types, quote_plus, PY2
2013-06-16 16:28:51 +02:00
# parts of URL to be omitted
2014-12-02 00:13:01 +01: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)):
2014-05-29 01:16:17 +02: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):
return value.encode('utf-8')
if not PY2 and isinstance(value, str):
return value.encode('utf-8')
2013-08-08 18:06:25 +02:00
return str(value)
2013-07-10 16:48:57 +02: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.
Convert lists nad tuples to comma separated values.
"""
#TODO: maybe only allow some parts to be lists/tuples ?
2013-08-08 18:06:25 +02:00
return '/' + '/'.join(
# preserve ',' and '*' in url for nicer URLs in logs
quote_plus(_escape(p), b',*') 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
2017-02-08 21:07:29 +01: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.
"""
def _wrapper(func):
@wraps(func)
def _wrapped(*args, **kwargs):
params = {}
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
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)
return _wrapped
return _wrapper
class NamespacedClient(object):
def __init__(self, client):
self.client = client
@property
def transport(self):
return self.client.transport
class AddonClient(NamespacedClient):
@classmethod
def infect_client(cls, client):
addon = cls(weakref.proxy(client))
setattr(client, cls.namespace, addon)
return client