ElasticSearch client class
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
from .transport import Transport
|
||||||
|
|
||||||
|
def _normalize_hosts(hosts):
|
||||||
|
# if hosts are empty, just defer to defaults down the line
|
||||||
|
if hosts is None:
|
||||||
|
return [{}]
|
||||||
|
|
||||||
|
out = []
|
||||||
|
# normalize hosts to dicts
|
||||||
|
for i, host in enumerate(hosts):
|
||||||
|
if isinstance(host, (type(''), type(u''))):
|
||||||
|
h = {"host": host}
|
||||||
|
if ':' in host:
|
||||||
|
# TODO: detect auth urls
|
||||||
|
host, port = host.rsplit(':', 1)
|
||||||
|
if port.isdigit():
|
||||||
|
port = int(port)
|
||||||
|
h = {"host": host, "port": port}
|
||||||
|
out.append(h)
|
||||||
|
else:
|
||||||
|
out.append(host)
|
||||||
|
return out
|
||||||
|
|
||||||
|
class ElasticSearch(object):
|
||||||
|
def __init__(self, hosts=None, **kwargs):
|
||||||
|
self.transport = Transport(_normalize_hosts(hosts), **kwargs)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from elasticsearch.client import _normalize_hosts
|
||||||
|
|
||||||
|
class TestNormalizeHosts(TestCase):
|
||||||
|
def test_none_uses_defaults(self):
|
||||||
|
self.assertEquals([{}], _normalize_hosts(None))
|
||||||
|
|
||||||
|
def test_strings_are_used_as_hostnames(self):
|
||||||
|
self.assertEquals([{"host": "elasticsearch.org"}], _normalize_hosts(["elasticsearch.org"]))
|
||||||
|
|
||||||
|
def test_strings_are_parsed_for_port(self):
|
||||||
|
self.assertEquals(
|
||||||
|
[{"host": "elasticsearch.org", "port": 42}, {"host": "user:[email protected]"}],
|
||||||
|
_normalize_hosts(["elasticsearch.org:42", u"user:[email protected]"])
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_dicts_are_left_unchanged(self):
|
||||||
|
self.assertEquals([{"host": "local", "extra": 123}], _normalize_hosts([{"host": "local", "extra": 123}]))
|
||||||
Reference in New Issue
Block a user