Move _bulk_body to client.utils

This commit is contained in:
Honza Král
2020-01-19 00:01:36 +00:00
parent c4d61b7bce
commit 415f744b01
3 changed files with 19 additions and 18 deletions
+4 -15
View File
@@ -14,7 +14,7 @@ from .remote import RemoteClient
from .snapshot import SnapshotClient from .snapshot import SnapshotClient
from .tasks import TasksClient from .tasks import TasksClient
from .xpack import XPackClient from .xpack import XPackClient
from .utils import query_params, _make_path, SKIP_IN_PATH from .utils import query_params, _make_path, SKIP_IN_PATH, _bulk_body
# xpack APIs # xpack APIs
from .ccr import CcrClient from .ccr import CcrClient
@@ -268,17 +268,6 @@ class Elasticsearch(object):
# probably operating on custom transport and connection_pool, ignore # probably operating on custom transport and connection_pool, ignore
return super(Elasticsearch, self).__repr__() return super(Elasticsearch, self).__repr__()
def _bulk_body(self, body):
# if not passed in a string, serialize items and join by newline
if not isinstance(body, string_types):
body = "\n".join(map(self.transport.serializer.dumps, body))
# bulk body must end with a newline
if not body.endswith("\n"):
body += "\n"
return body
# AUTO-GENERATED-API-DEFINITIONS # # AUTO-GENERATED-API-DEFINITIONS #
@query_params() @query_params()
def ping(self, params=None): def ping(self, params=None):
@@ -455,7 +444,7 @@ class Elasticsearch(object):
if body in SKIP_IN_PATH: if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.") raise ValueError("Empty value passed for a required argument 'body'.")
body = self._bulk_body(body) body = _bulk_body(self.transport.serializer, body)
return self.transport.perform_request( return self.transport.perform_request(
"POST", _make_path(index, doc_type, "_bulk"), params=params, body=body "POST", _make_path(index, doc_type, "_bulk"), params=params, body=body
) )
@@ -1141,7 +1130,7 @@ class Elasticsearch(object):
if body in SKIP_IN_PATH: if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.") raise ValueError("Empty value passed for a required argument 'body'.")
body = self._bulk_body(body) body = _bulk_body(self.transport.serializer, body)
return self.transport.perform_request( return self.transport.perform_request(
"GET", _make_path(index, doc_type, "_msearch"), params=params, body=body "GET", _make_path(index, doc_type, "_msearch"), params=params, body=body
) )
@@ -1173,7 +1162,7 @@ class Elasticsearch(object):
if body in SKIP_IN_PATH: if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.") raise ValueError("Empty value passed for a required argument 'body'.")
body = self._bulk_body(body) body = _bulk_body(self.transport.serializer, body)
return self.transport.perform_request( return self.transport.perform_request(
"GET", "GET",
_make_path(index, doc_type, "_msearch", "template"), _make_path(index, doc_type, "_msearch", "template"),
+3 -3
View File
@@ -1,4 +1,4 @@
from .utils import NamespacedClient, query_params, _make_path, SKIP_IN_PATH from .utils import NamespacedClient, query_params, _make_path, SKIP_IN_PATH, _bulk_body
class MlClient(NamespacedClient): class MlClient(NamespacedClient):
@@ -229,7 +229,7 @@ class MlClient(NamespacedClient):
if body in SKIP_IN_PATH: if body in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument 'body'.") raise ValueError("Empty value passed for a required argument 'body'.")
body = self._bulk_body(body) body = _bulk_body(self.transport.serializer, body)
return self.transport.perform_request( return self.transport.perform_request(
"POST", "/_ml/find_file_structure", params=params, body=body "POST", "/_ml/find_file_structure", params=params, body=body
) )
@@ -685,7 +685,7 @@ class MlClient(NamespacedClient):
if param in SKIP_IN_PATH: if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.") raise ValueError("Empty value passed for a required argument.")
body = self._bulk_body(body) body = _bulk_body(self.transport.serializer, body)
return self.transport.perform_request( return self.transport.perform_request(
"POST", "POST",
_make_path("_ml", "anomaly_detectors", job_id, "_data"), _make_path("_ml", "anomaly_detectors", job_id, "_data"),
+12
View File
@@ -88,6 +88,18 @@ def query_params(*es_query_params):
return _wrapper return _wrapper
def _bulk_body(serializer, body):
# 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))
# bulk body must end with a newline
if not body.endswith("\n"):
body += "\n"
return body
class NamespacedClient(object): class NamespacedClient(object):
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client